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> |
Neels Hofmeyr | 1d03f19 | 2016-08-18 03:15:14 +0200 | [diff] [blame^] | 28 | #include <stdbool.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 29 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <sys/socket.h> |
Harald Welte | 1d2c39d | 2015-09-11 17:49:37 +0200 | [diff] [blame] | 32 | #include <netinet/in.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 33 | #include <netinet/sctp.h> |
Harald Welte | 1d2c39d | 2015-09-11 17:49:37 +0200 | [diff] [blame] | 34 | #include <arpa/inet.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 35 | |
| 36 | #include <osmocom/core/application.h> |
| 37 | #include <osmocom/core/talloc.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 38 | #include <osmocom/core/select.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 39 | #include <osmocom/core/logging.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 40 | #include <osmocom/core/socket.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 41 | #include <osmocom/core/msgb.h> |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 42 | #include <osmocom/core/write_queue.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 43 | |
| 44 | #include <osmocom/vty/telnet_interface.h> |
| 45 | #include <osmocom/vty/logging.h> |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 46 | #include <osmocom/vty/command.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 47 | |
Harald Welte | ffa7c0a | 2015-12-23 00:03:41 +0100 | [diff] [blame] | 48 | #include <osmocom/netif/stream.h> |
| 49 | |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 50 | #include <osmocom/sigtran/sua.h> |
| 51 | #include <osmocom/sigtran/protocol/sua.h> |
| 52 | #include <osmocom/sigtran/sccp_sap.h> |
| 53 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 54 | #include "hnbgw.h" |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 55 | #include "hnbgw_hnbap.h" |
Harald Welte | 318e4d5 | 2015-09-10 18:47:08 +0200 | [diff] [blame] | 56 | #include "hnbgw_rua.h" |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 57 | #include "hnbgw_cn.h" |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 58 | #include "context_map.h" |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 59 | |
Neels Hofmeyr | 4c45d2b | 2016-08-18 02:10:57 +0200 | [diff] [blame] | 60 | static const char * const osmo_hnbgw_copyright = |
| 61 | "OsmoHNBGW - Osmocom Home Node B Gateway implementation\r\n" |
| 62 | "Copyright (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>\r\n" |
| 63 | "Contributions by Daniel Willmann, Harald Welte, Neels Hofmeyr\r\n" |
| 64 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
| 65 | "This is free software: you are free to change and redistribute it.\r\n" |
| 66 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
| 67 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 68 | static void *tall_hnb_ctx; |
Harald Welte | 08de638 | 2015-08-31 09:54:45 +0200 | [diff] [blame] | 69 | void *talloc_asn1_ctx; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 70 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 71 | static struct hnb_gw *g_hnb_gw; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 72 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 73 | static int listen_fd_cb(struct osmo_fd *fd, unsigned int what); |
| 74 | |
| 75 | static struct hnb_gw *hnb_gw_create(void *ctx) |
| 76 | { |
| 77 | struct hnb_gw *gw = talloc_zero(ctx, struct hnb_gw); |
| 78 | |
| 79 | gw->config.iuh_listen_port = IUH_DEFAULT_SCTP_PORT; |
| 80 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 81 | gw->next_ue_ctx_id = 23; |
| 82 | INIT_LLIST_HEAD(&gw->hnb_list); |
| 83 | INIT_LLIST_HEAD(&gw->ue_list); |
| 84 | INIT_LLIST_HEAD(&gw->cn_list); |
| 85 | |
| 86 | context_map_init(gw); |
| 87 | |
| 88 | return gw; |
| 89 | } |
| 90 | |
| 91 | 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] | 92 | { |
| 93 | struct ue_context *ue; |
| 94 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 95 | llist_for_each_entry(ue, &gw->ue_list, list) { |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 96 | if (ue->context_id == id) |
| 97 | return ue; |
| 98 | } |
| 99 | return NULL; |
| 100 | |
| 101 | } |
| 102 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 103 | 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] | 104 | { |
| 105 | struct ue_context *ue; |
| 106 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 107 | llist_for_each_entry(ue, &gw->ue_list, list) { |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 108 | if (!strcmp(ue->imsi, imsi)) |
| 109 | return ue; |
| 110 | } |
| 111 | return NULL; |
| 112 | } |
| 113 | |
Daniel Willmann | 1ee089f | 2016-01-06 18:07:02 +0100 | [diff] [blame] | 114 | void ue_context_free_by_hnb(struct hnb_gw *gw, const struct hnb_context *hnb) |
| 115 | { |
| 116 | struct ue_context *ue, *tmp; |
| 117 | |
| 118 | llist_for_each_entry_safe(ue, tmp, &gw->ue_list, list) { |
| 119 | if (ue->hnb == hnb) |
| 120 | ue_context_free(ue); |
| 121 | } |
| 122 | } |
| 123 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 124 | static uint32_t get_next_ue_ctx_id(struct hnb_gw *gw) |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 125 | { |
| 126 | uint32_t id; |
| 127 | |
| 128 | do { |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 129 | id = gw->next_ue_ctx_id++; |
| 130 | } while (ue_context_by_id(gw, id)); |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 131 | |
| 132 | return id; |
| 133 | } |
| 134 | |
| 135 | struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi) |
| 136 | { |
| 137 | struct ue_context *ue; |
| 138 | |
| 139 | ue = talloc_zero(tall_hnb_ctx, struct ue_context); |
| 140 | if (!ue) |
| 141 | return NULL; |
| 142 | |
| 143 | ue->hnb = hnb; |
| 144 | strncpy(ue->imsi, imsi, sizeof(ue->imsi)); |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 145 | ue->context_id = get_next_ue_ctx_id(hnb->gw); |
| 146 | llist_add_tail(&ue->list, &hnb->gw->ue_list); |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 147 | |
| 148 | return ue; |
| 149 | } |
| 150 | |
| 151 | void ue_context_free(struct ue_context *ue) |
| 152 | { |
| 153 | llist_del(&ue->list); |
| 154 | talloc_free(ue); |
| 155 | } |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 156 | static int hnb_close_cb(struct osmo_stream_srv *conn) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 157 | { |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static int hnb_read_cb(struct osmo_stream_srv *conn) |
| 161 | { |
| 162 | struct hnb_context *hnb = osmo_stream_srv_get_data(conn); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 163 | struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx"); |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 164 | int flags = 0; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 165 | int rc; |
| 166 | |
| 167 | if (!msg) |
| 168 | return -ENOMEM; |
| 169 | |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 170 | /* we store a reference to the HomeNodeB in the msg->dest for the |
| 171 | * benefit of varoius downstream processing functions */ |
| 172 | msg->dst = hnb; |
| 173 | |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 174 | rc = osmo_stream_srv_recv(conn, msg); |
| 175 | if (rc == -EAGAIN) { |
| 176 | /* Notification received */ |
| 177 | msgb_free(msg); |
| 178 | return 0; |
| 179 | } else if (rc < 0) { |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 180 | LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n"); |
Harald Welte | ee77cff | 2015-08-30 16:57:53 +0200 | [diff] [blame] | 181 | /* FIXME: clean up after disappeared HNB */ |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 182 | hnb_context_release(hnb); |
Daniel Willmann | 269b8ac | 2015-12-22 16:26:48 +0100 | [diff] [blame] | 183 | goto out; |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 184 | } else if (rc == 0) { |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 185 | hnb_context_release(hnb); |
Daniel Willmann | 269b8ac | 2015-12-22 16:26:48 +0100 | [diff] [blame] | 186 | rc = -1; |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 187 | |
Daniel Willmann | 269b8ac | 2015-12-22 16:26:48 +0100 | [diff] [blame] | 188 | goto out; |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 189 | } else { |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 190 | msgb_put(msg, rc); |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 191 | } |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 192 | |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 193 | switch (msgb_sctp_ppid(msg)) { |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 194 | case IUH_PPI_HNBAP: |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 195 | hnb->hnbap_stream = msgb_sctp_stream(msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 196 | rc = hnbgw_hnbap_rx(hnb, msg); |
| 197 | break; |
| 198 | case IUH_PPI_RUA: |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 199 | hnb->rua_stream = msgb_sctp_stream(msg); |
Harald Welte | 318e4d5 | 2015-09-10 18:47:08 +0200 | [diff] [blame] | 200 | rc = hnbgw_rua_rx(hnb, msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 201 | break; |
| 202 | case IUH_PPI_SABP: |
| 203 | case IUH_PPI_RNA: |
| 204 | case IUH_PPI_PUA: |
| 205 | LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n", |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 206 | msgb_sctp_ppid(msg)); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 207 | rc = 0; |
| 208 | break; |
| 209 | default: |
| 210 | LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n", |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 211 | msgb_sctp_ppid(msg)); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 212 | rc = 0; |
| 213 | break; |
| 214 | } |
| 215 | |
Daniel Willmann | 269b8ac | 2015-12-22 16:26:48 +0100 | [diff] [blame] | 216 | out: |
Harald Welte | f2f3000 | 2015-09-08 00:09:23 +0200 | [diff] [blame] | 217 | msgb_free(msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 218 | return rc; |
| 219 | } |
| 220 | |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 221 | static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg) |
| 222 | { |
| 223 | struct hnb_context *ctx = fd->data; |
| 224 | struct sctp_sndrcvinfo sinfo = { |
Harald Welte | ffa7c0a | 2015-12-23 00:03:41 +0100 | [diff] [blame] | 225 | .sinfo_ppid = htonl(msgb_sctp_ppid(msg)), |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 226 | .sinfo_stream = ctx->hnbap_stream, |
| 227 | }; |
| 228 | int rc; |
| 229 | |
| 230 | rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg), |
| 231 | &sinfo, 0); |
Harald Welte | 9e270b4 | 2015-09-07 22:41:26 +0200 | [diff] [blame] | 232 | /* 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] | 233 | return rc; |
| 234 | } |
| 235 | |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 236 | 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] | 237 | { |
| 238 | struct hnb_context *ctx; |
| 239 | |
| 240 | ctx = talloc_zero(tall_hnb_ctx, struct hnb_context); |
| 241 | if (!ctx) |
| 242 | return NULL; |
Neels Hofmeyr | 44beab9 | 2016-02-18 01:26:59 +0100 | [diff] [blame] | 243 | INIT_LLIST_HEAD(&ctx->map_list); |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 244 | |
| 245 | ctx->gw = gw; |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 246 | ctx->conn = osmo_stream_srv_create(tall_hnb_ctx, link, new_fd, hnb_read_cb, hnb_close_cb, ctx); |
| 247 | if (!ctx->conn) { |
| 248 | LOGP(DMAIN, LOGL_INFO, "error while creating connection\n"); |
Neels Hofmeyr | 44beab9 | 2016-02-18 01:26:59 +0100 | [diff] [blame] | 249 | talloc_free(ctx); |
| 250 | return NULL; |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 251 | } |
| 252 | |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 253 | llist_add_tail(&ctx->list, &gw->hnb_list); |
Neels Hofmeyr | 44beab9 | 2016-02-18 01:26:59 +0100 | [diff] [blame] | 254 | return ctx; |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void hnb_context_release(struct hnb_context *ctx) |
| 258 | { |
| 259 | struct hnbgw_context_map *map, *map2; |
| 260 | |
| 261 | /* remove from the list of HNB contexts */ |
| 262 | llist_del(&ctx->list); |
| 263 | |
| 264 | /* deactivate all context maps */ |
| 265 | llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) { |
| 266 | /* remove it from list, as HNB context will soon be |
Neels Hofmeyr | 525a69e | 2016-04-19 17:54:25 +0200 | [diff] [blame] | 267 | * gone. Let's hope the second osmo_llist_del in the |
| 268 | * map garbage collector works fine? */ |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 269 | llist_del(&map->hnb_list); |
Neels Hofmeyr | 0a46156 | 2016-04-23 13:53:28 +0200 | [diff] [blame] | 270 | llist_del(&map->cn_list); |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 271 | context_map_deactivate(map); |
| 272 | } |
Daniel Willmann | 1ee089f | 2016-01-06 18:07:02 +0100 | [diff] [blame] | 273 | ue_context_free_by_hnb(ctx->gw, ctx); |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 274 | osmo_stream_srv_destroy(ctx->conn); |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 275 | |
| 276 | talloc_free(ctx); |
| 277 | } |
| 278 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 279 | /*! call-back when the listen FD has something to read */ |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 280 | static int accept_cb(struct osmo_stream_srv_link *srv, int fd) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 281 | { |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 282 | struct hnb_gw *gw = osmo_stream_srv_link_get_data(srv); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 283 | struct hnb_context *ctx; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 284 | |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 285 | ctx = hnb_context_alloc(gw, srv, fd); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 286 | if (!ctx) |
| 287 | return -ENOMEM; |
| 288 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 292 | static const struct log_info_cat log_cat[] = { |
| 293 | [DMAIN] = { |
Neels Hofmeyr | 6341f4d | 2016-04-04 18:02:49 +0200 | [diff] [blame] | 294 | .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1, |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 295 | .color = "", |
| 296 | .description = "Main program", |
| 297 | }, |
Daniel Willmann | bded984 | 2015-12-17 11:51:17 +0100 | [diff] [blame] | 298 | [DHNBAP] = { |
| 299 | .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 300 | .color = "", |
| 301 | .description = "Home Node B Application Part", |
| 302 | }, |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 303 | [DSUA] = { |
| 304 | .name = "DSUA", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 305 | .color = "", |
| 306 | .description = "SCCP User Adaptation", |
| 307 | }, |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 308 | [DRUA] = { |
| 309 | .name = "DRUA", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 310 | .color = "", |
| 311 | .description = "RANAP User Adaptation", |
| 312 | }, |
| 313 | [DRANAP] = { |
| 314 | .name = "DRANAP", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 315 | .color = "", |
| 316 | .description = "RAN Application Part", |
| 317 | }, |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 318 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 319 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 320 | static const struct log_info hnbgw_log_info = { |
| 321 | .cat = log_cat, |
| 322 | .num_cat = ARRAY_SIZE(log_cat), |
| 323 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 324 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 325 | static struct vty_app_info vty_info = { |
| 326 | .name = "OsmoHNBGW", |
| 327 | .version = "0", |
| 328 | }; |
| 329 | |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 330 | static void vty_dump_hnb_info(struct vty *vty, struct hnb_context *hnb) |
| 331 | { |
Daniel Willmann | 5f8c718 | 2016-01-14 15:42:07 +0100 | [diff] [blame] | 332 | struct hnbgw_context_map *map; |
| 333 | |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 334 | vty_out(vty, "HNB \"%s\" MCC %u MNC %u LAC %u RAC %u SAC %u CID %u%s", hnb->identity_info, |
| 335 | hnb->id.mcc, hnb->id.mnc, hnb->id.lac, hnb->id.rac, hnb->id.sac, hnb->id.cid, |
| 336 | VTY_NEWLINE); |
| 337 | 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] | 338 | |
| 339 | llist_for_each_entry(map, &hnb->map_list, hnb_list) { |
| 340 | vty_out(vty, " Map %u->%u (RUA->SUA) cnlink=%p state=%u%s", map->rua_ctx_id, map->scu_conn_id, |
| 341 | map->cn_link, map->state, VTY_NEWLINE); |
| 342 | |
| 343 | } |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static void vty_dump_ue_info(struct vty *vty, struct ue_context *ue) |
| 347 | { |
| 348 | vty_out(vty, "UE IMSI \"%s\" context ID %u%s", ue->imsi, ue->context_id, VTY_NEWLINE); |
| 349 | } |
| 350 | |
| 351 | DEFUN(show_hnb, show_hnb_cmd, "show hnb all", SHOW_STR "Display information about a HNB") |
| 352 | { |
| 353 | struct hnb_context *hnb; |
| 354 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 355 | llist_for_each_entry(hnb, &g_hnb_gw->hnb_list, list) { |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 356 | vty_dump_hnb_info(vty, hnb); |
| 357 | } |
| 358 | |
| 359 | return CMD_SUCCESS; |
| 360 | } |
| 361 | |
| 362 | DEFUN(show_ue, show_ue_cmd, "show ue all", SHOW_STR "Display information about a UE") |
| 363 | { |
| 364 | struct ue_context *ue; |
| 365 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 366 | llist_for_each_entry(ue, &g_hnb_gw->ue_list, list) { |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 367 | vty_dump_ue_info(vty, ue); |
| 368 | } |
| 369 | |
| 370 | return CMD_SUCCESS; |
| 371 | } |
| 372 | |
Daniel Willmann | 28b9ec1 | 2015-12-17 18:02:37 +0100 | [diff] [blame] | 373 | DEFUN(show_talloc, show_talloc_cmd, "show talloc", SHOW_STR "Display talloc info") |
| 374 | { |
| 375 | talloc_report_full(tall_hnb_ctx, stderr); |
Daniel Willmann | 28b9ec1 | 2015-12-17 18:02:37 +0100 | [diff] [blame] | 376 | talloc_report_full(talloc_asn1_ctx, stderr); |
| 377 | |
| 378 | return CMD_SUCCESS; |
| 379 | } |
| 380 | |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 381 | static void hnbgw_vty_init(void) |
| 382 | { |
| 383 | install_element_ve(&show_hnb_cmd); |
| 384 | install_element_ve(&show_ue_cmd); |
Daniel Willmann | 28b9ec1 | 2015-12-17 18:02:37 +0100 | [diff] [blame] | 385 | install_element_ve(&show_talloc_cmd); |
Neels Hofmeyr | 110bb5c | 2016-02-11 17:51:11 +0100 | [diff] [blame] | 386 | |
| 387 | logging_vty_add_cmds(&hnbgw_log_info); |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 390 | |
| 391 | static struct { |
| 392 | int daemonize; |
Neels Hofmeyr | 085cd67 | 2016-08-17 13:54:00 +0200 | [diff] [blame] | 393 | const char *config_file; |
Neels Hofmeyr | 1d03f19 | 2016-08-18 03:15:14 +0200 | [diff] [blame^] | 394 | bool log_disable_color; |
| 395 | bool log_enable_timestamp; |
| 396 | int log_level; |
| 397 | const char *log_category_mask; |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 398 | } hnbgw_cmdline_config = { |
Neels Hofmeyr | 085cd67 | 2016-08-17 13:54:00 +0200 | [diff] [blame] | 399 | 0, |
Neels Hofmeyr | 1d03f19 | 2016-08-18 03:15:14 +0200 | [diff] [blame^] | 400 | "osmo-hnbgw.cfg", |
| 401 | true, |
| 402 | false, |
| 403 | 0, |
| 404 | NULL, |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | static void print_usage() |
| 408 | { |
| 409 | printf("Usage: osmo-hnbgw\n"); |
| 410 | } |
| 411 | |
| 412 | static void print_help() |
| 413 | { |
| 414 | printf(" -h --help This text.\n"); |
| 415 | printf(" -d option --debug=DHNBAP:DSUA:DRUA:DRANAP:DMAIN Enable debugging.\n"); |
| 416 | printf(" -D --daemonize Fork the process into a background daemon.\n"); |
Neels Hofmeyr | 085cd67 | 2016-08-17 13:54:00 +0200 | [diff] [blame] | 417 | printf(" -c --config-file filename The config file to use.\n"); |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 418 | printf(" -s --disable-color\n"); |
| 419 | printf(" -T --timestamp Prefix every log line with a timestamp.\n"); |
| 420 | printf(" -V --version Print the version of OsmoHNBGW.\n"); |
| 421 | printf(" -e --log-level number Set a global loglevel.\n"); |
| 422 | } |
| 423 | |
| 424 | static void handle_options(int argc, char **argv) |
| 425 | { |
| 426 | while (1) { |
| 427 | int option_index = 0, c; |
| 428 | static struct option long_options[] = { |
| 429 | {"help", 0, 0, 'h'}, |
| 430 | {"debug", 1, 0, 'd'}, |
| 431 | {"daemonize", 0, 0, 'D'}, |
Neels Hofmeyr | 085cd67 | 2016-08-17 13:54:00 +0200 | [diff] [blame] | 432 | {"config-file", 1, 0, 'c'}, |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 433 | {"disable-color", 0, 0, 's'}, |
| 434 | {"timestamp", 0, 0, 'T'}, |
| 435 | {"version", 0, 0, 'V' }, |
| 436 | {"log-level", 1, 0, 'e'}, |
| 437 | {0, 0, 0, 0} |
| 438 | }; |
| 439 | |
Neels Hofmeyr | 085cd67 | 2016-08-17 13:54:00 +0200 | [diff] [blame] | 440 | c = getopt_long(argc, argv, "hd:Dc:sTVe:", |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 441 | long_options, &option_index); |
| 442 | if (c == -1) |
| 443 | break; |
| 444 | |
| 445 | switch (c) { |
| 446 | case 'h': |
| 447 | print_usage(); |
| 448 | print_help(); |
| 449 | exit(0); |
| 450 | case 's': |
Neels Hofmeyr | 1d03f19 | 2016-08-18 03:15:14 +0200 | [diff] [blame^] | 451 | hnbgw_cmdline_config.log_disable_color = true; |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 452 | break; |
| 453 | case 'd': |
Neels Hofmeyr | 1d03f19 | 2016-08-18 03:15:14 +0200 | [diff] [blame^] | 454 | hnbgw_cmdline_config.log_category_mask = optarg; |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 455 | break; |
| 456 | case 'D': |
| 457 | hnbgw_cmdline_config.daemonize = 1; |
| 458 | break; |
Neels Hofmeyr | 085cd67 | 2016-08-17 13:54:00 +0200 | [diff] [blame] | 459 | case 'c': |
| 460 | hnbgw_cmdline_config.config_file = optarg; |
| 461 | break; |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 462 | case 'T': |
Neels Hofmeyr | 1d03f19 | 2016-08-18 03:15:14 +0200 | [diff] [blame^] | 463 | hnbgw_cmdline_config.log_enable_timestamp = true; |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 464 | break; |
| 465 | case 'e': |
Neels Hofmeyr | 1d03f19 | 2016-08-18 03:15:14 +0200 | [diff] [blame^] | 466 | hnbgw_cmdline_config.log_level = atoi(optarg); |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 467 | break; |
| 468 | case 'V': |
| 469 | print_version(1); |
| 470 | exit(0); |
| 471 | break; |
| 472 | default: |
| 473 | /* catch unknown options *as well as* missing arguments. */ |
| 474 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 475 | exit(-1); |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 482 | int main(int argc, char **argv) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 483 | { |
Neels Hofmeyr | 0a43722 | 2016-07-06 15:58:48 +0200 | [diff] [blame] | 484 | struct osmo_sccp_user *sua_user; |
| 485 | struct osmo_sccp_link *sua_link; |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 486 | struct osmo_stream_srv_link *srv; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 487 | int rc; |
| 488 | |
| 489 | tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context"); |
Harald Welte | 08de638 | 2015-08-31 09:54:45 +0200 | [diff] [blame] | 490 | talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context"); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 491 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 492 | g_hnb_gw = hnb_gw_create(tall_hnb_ctx); |
Neels Hofmeyr | a4540be | 2016-04-05 14:27:48 +0200 | [diff] [blame] | 493 | g_hnb_gw->config.rnc_id = 23; |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 494 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 495 | rc = osmo_init_logging(&hnbgw_log_info); |
| 496 | if (rc < 0) |
| 497 | exit(1); |
| 498 | |
Neels Hofmeyr | 4c45d2b | 2016-08-18 02:10:57 +0200 | [diff] [blame] | 499 | vty_info.copyright = osmo_hnbgw_copyright; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 500 | vty_init(&vty_info); |
Neels Hofmeyr | 4c45d2b | 2016-08-18 02:10:57 +0200 | [diff] [blame] | 501 | |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 502 | hnbgw_vty_init(); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 503 | |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 504 | /* Handle options after vty_init(), for --version */ |
| 505 | handle_options(argc, argv); |
| 506 | |
Neels Hofmeyr | 085cd67 | 2016-08-17 13:54:00 +0200 | [diff] [blame] | 507 | rc = vty_read_config_file(hnbgw_cmdline_config.config_file, NULL); |
| 508 | if (rc < 0) { |
| 509 | LOGP(DMAIN, LOGL_FATAL, "Failed to parse the config file: '%s'\n", |
| 510 | hnbgw_cmdline_config.config_file); |
| 511 | return 1; |
| 512 | } |
| 513 | |
Neels Hofmeyr | 1d03f19 | 2016-08-18 03:15:14 +0200 | [diff] [blame^] | 514 | /* |
| 515 | * cmdline options take precedence over config file, but if no options |
| 516 | * were passed we must not override the config file. |
| 517 | */ |
| 518 | if (hnbgw_cmdline_config.log_disable_color) |
| 519 | log_set_use_color(osmo_stderr_target, 0); |
| 520 | if (hnbgw_cmdline_config.log_category_mask) |
| 521 | log_parse_category_mask(osmo_stderr_target, |
| 522 | hnbgw_cmdline_config.log_category_mask); |
| 523 | if (hnbgw_cmdline_config.log_enable_timestamp) |
| 524 | log_set_print_timestamp(osmo_stderr_target, 1); |
| 525 | if (hnbgw_cmdline_config.log_level) |
| 526 | log_set_log_level(osmo_stderr_target, |
| 527 | hnbgw_cmdline_config.log_level); |
| 528 | |
Neels Hofmeyr | a0d2147 | 2016-02-24 20:50:31 +0100 | [diff] [blame] | 529 | LOGP(DMAIN, LOGL_NOTICE, "VTY at %s %d\n", |
| 530 | vty_get_bind_addr(), 2323); |
| 531 | 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] | 532 | if (rc < 0) { |
| 533 | perror("Error binding VTY port"); |
| 534 | exit(1); |
| 535 | } |
| 536 | |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 537 | osmo_sua_set_log_area(DSUA); |
Harald Welte | bdf3fd1 | 2016-01-03 17:04:09 +0100 | [diff] [blame] | 538 | ranap_set_log_area(DRANAP); |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 539 | |
Daniel Willmann | 4deab94 | 2016-01-14 15:35:11 +0100 | [diff] [blame] | 540 | g_hnb_gw->cnlink_cs = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.1", SUA_PORT, 0); |
| 541 | 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] | 542 | |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 543 | srv = osmo_stream_srv_link_create(tall_hnb_ctx); |
| 544 | if (!srv) { |
| 545 | perror("cannot create server"); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 546 | exit(1); |
| 547 | } |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 548 | osmo_stream_srv_link_set_data(srv, g_hnb_gw); |
| 549 | osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP); |
| 550 | osmo_stream_srv_link_set_addr(srv, "0.0.0.0"); |
| 551 | osmo_stream_srv_link_set_port(srv, g_hnb_gw->config.iuh_listen_port); |
| 552 | osmo_stream_srv_link_set_accept_cb(srv, accept_cb); |
| 553 | |
| 554 | if (osmo_stream_srv_link_open(srv) < 0) { |
Neels Hofmeyr | de111bc | 2016-01-13 11:54:40 +0100 | [diff] [blame] | 555 | perror("Cannot open server"); |
Daniel Willmann | 6480cad | 2016-01-06 18:06:26 +0100 | [diff] [blame] | 556 | exit(1); |
| 557 | } |
| 558 | g_hnb_gw->iuh = srv; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 559 | |
Neels Hofmeyr | 4d675a7 | 2016-08-17 13:53:18 +0200 | [diff] [blame] | 560 | if (hnbgw_cmdline_config.daemonize) { |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 561 | rc = osmo_daemonize(); |
| 562 | if (rc < 0) { |
| 563 | perror("Error during daemonize"); |
| 564 | exit(1); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | while (1) { |
| 569 | rc = osmo_select_main(0); |
| 570 | if (rc < 0) |
| 571 | exit(3); |
| 572 | } |
| 573 | |
| 574 | /* not reached */ |
| 575 | exit(0); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 576 | } |