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