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