Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <getopt.h> |
| 6 | #include <errno.h> |
| 7 | #include <signal.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 8 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | #include <sys/socket.h> |
| 11 | #include <netinet/sctp.h> |
| 12 | |
| 13 | #include <osmocom/core/application.h> |
| 14 | #include <osmocom/core/talloc.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 15 | #include <osmocom/core/select.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 16 | #include <osmocom/core/logging.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 17 | #include <osmocom/core/socket.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 18 | #include <osmocom/core/msgb.h> |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 19 | #include <osmocom/core/write_queue.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 20 | |
| 21 | #include <osmocom/vty/telnet_interface.h> |
| 22 | #include <osmocom/vty/logging.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 23 | |
| 24 | #include "hnbgw.h" |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 25 | #include "hnbgw_hnbap.h" |
Harald Welte | 318e4d5 | 2015-09-10 18:47:08 +0200 | [diff] [blame] | 26 | #include "hnbgw_rua.h" |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 27 | |
| 28 | static void *tall_hnb_ctx; |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame^] | 29 | static void *tall_ue_ctx; |
Harald Welte | 08de638 | 2015-08-31 09:54:45 +0200 | [diff] [blame] | 30 | void *talloc_asn1_ctx; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 31 | |
| 32 | struct hnb_gw g_hnb_gw = { |
| 33 | .config = { |
| 34 | .iuh_listen_port = IUH_DEFAULT_SCTP_PORT, |
| 35 | }, |
| 36 | }; |
| 37 | |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame^] | 38 | struct ue_context *ue_context_by_id(uint32_t id) |
| 39 | { |
| 40 | struct ue_context *ue; |
| 41 | |
| 42 | llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) { |
| 43 | if (ue->context_id == id) |
| 44 | return ue; |
| 45 | } |
| 46 | return NULL; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | struct ue_context *ue_context_by_imsi(const char *imsi) |
| 51 | { |
| 52 | struct ue_context *ue; |
| 53 | |
| 54 | llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) { |
| 55 | if (!strcmp(ue->imsi, imsi)) |
| 56 | return ue; |
| 57 | } |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | static uint32_t get_next_ue_ctx_id(void) |
| 62 | { |
| 63 | uint32_t id; |
| 64 | |
| 65 | do { |
| 66 | id = g_hnb_gw.next_ue_ctx_id++; |
| 67 | } while (ue_context_by_id(id)); |
| 68 | |
| 69 | return id; |
| 70 | } |
| 71 | |
| 72 | struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi) |
| 73 | { |
| 74 | struct ue_context *ue; |
| 75 | |
| 76 | ue = talloc_zero(tall_hnb_ctx, struct ue_context); |
| 77 | if (!ue) |
| 78 | return NULL; |
| 79 | |
| 80 | ue->hnb = hnb; |
| 81 | strncpy(ue->imsi, imsi, sizeof(ue->imsi)); |
| 82 | ue->context_id = get_next_ue_ctx_id(); |
| 83 | llist_add_tail(&g_hnb_gw.ue_list, &ue->list); |
| 84 | |
| 85 | return ue; |
| 86 | } |
| 87 | |
| 88 | void ue_context_free(struct ue_context *ue) |
| 89 | { |
| 90 | llist_del(&ue->list); |
| 91 | talloc_free(ue); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 96 | static int hnb_read_cb(struct osmo_fd *fd) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 97 | { |
| 98 | struct hnb_context *hnb = fd->data; |
| 99 | struct sctp_sndrcvinfo sinfo; |
| 100 | struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx"); |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 101 | int flags = 0; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 102 | int rc; |
| 103 | |
| 104 | if (!msg) |
| 105 | return -ENOMEM; |
| 106 | |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 107 | /* we store a reference to the HomeNodeB in the msg->dest for the |
| 108 | * benefit of varoius downstream processing functions */ |
| 109 | msg->dst = hnb; |
| 110 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 111 | rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg), |
| 112 | NULL, NULL, &sinfo, &flags); |
| 113 | if (rc < 0) { |
| 114 | LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n"); |
Harald Welte | ee77cff | 2015-08-30 16:57:53 +0200 | [diff] [blame] | 115 | /* FIXME: clean up after disappeared HNB */ |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 116 | return rc; |
| 117 | } else |
| 118 | msgb_put(msg, rc); |
| 119 | |
Harald Welte | 17878e2 | 2015-09-08 00:09:13 +0200 | [diff] [blame] | 120 | if (flags & MSG_NOTIFICATION) { |
| 121 | LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n"); |
| 122 | msgb_free(msg); |
| 123 | return 0; |
| 124 | } |
| 125 | |
Harald Welte | 5c11c94 | 2015-09-07 19:54:49 +0200 | [diff] [blame] | 126 | sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid); |
| 127 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 128 | switch (sinfo.sinfo_ppid) { |
| 129 | case IUH_PPI_HNBAP: |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 130 | hnb->hnbap_stream = sinfo.sinfo_stream; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 131 | rc = hnbgw_hnbap_rx(hnb, msg); |
| 132 | break; |
| 133 | case IUH_PPI_RUA: |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 134 | hnb->rua_stream = sinfo.sinfo_stream; |
Harald Welte | 318e4d5 | 2015-09-10 18:47:08 +0200 | [diff] [blame] | 135 | rc = hnbgw_rua_rx(hnb, msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 136 | break; |
| 137 | case IUH_PPI_SABP: |
| 138 | case IUH_PPI_RNA: |
| 139 | case IUH_PPI_PUA: |
| 140 | LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n", |
| 141 | sinfo.sinfo_ppid); |
| 142 | rc = 0; |
| 143 | break; |
| 144 | default: |
| 145 | LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n", |
| 146 | sinfo.sinfo_ppid); |
| 147 | rc = 0; |
| 148 | break; |
| 149 | } |
| 150 | |
Harald Welte | f2f3000 | 2015-09-08 00:09:23 +0200 | [diff] [blame] | 151 | msgb_free(msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 152 | return rc; |
| 153 | } |
| 154 | |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 155 | static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg) |
| 156 | { |
| 157 | struct hnb_context *ctx = fd->data; |
| 158 | struct sctp_sndrcvinfo sinfo = { |
Harald Welte | cfcc1e6 | 2015-09-07 22:39:56 +0200 | [diff] [blame] | 159 | .sinfo_ppid = htonl(msgb_ppid(msg)), |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 160 | .sinfo_stream = ctx->hnbap_stream, |
| 161 | }; |
| 162 | int rc; |
| 163 | |
| 164 | rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg), |
| 165 | &sinfo, 0); |
Harald Welte | 9e270b4 | 2015-09-07 22:41:26 +0200 | [diff] [blame] | 166 | /* 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] | 167 | return rc; |
| 168 | } |
| 169 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 170 | /*! call-back when the listen FD has something to read */ |
| 171 | static int listen_fd_cb(struct osmo_fd *fd, unsigned int what) |
| 172 | { |
| 173 | struct hnb_gw *gw = fd->data; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 174 | struct hnb_context *ctx; |
| 175 | struct sockaddr_storage sockaddr; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 176 | socklen_t len = sizeof(sockaddr); |
| 177 | |
| 178 | int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len); |
| 179 | if (new_fd < 0) { |
| 180 | LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n"); |
| 181 | return new_fd; |
| 182 | } |
| 183 | |
| 184 | LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n"); |
| 185 | |
| 186 | ctx = talloc_zero(tall_hnb_ctx, struct hnb_context); |
| 187 | if (!ctx) |
| 188 | return -ENOMEM; |
| 189 | |
| 190 | ctx->gw = gw; |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 191 | osmo_wqueue_init(&ctx->wqueue, 16); |
| 192 | ctx->wqueue.bfd.data = ctx; |
| 193 | ctx->wqueue.bfd.fd = new_fd; |
| 194 | ctx->wqueue.bfd.when = BSC_FD_READ; |
| 195 | ctx->wqueue.read_cb = hnb_read_cb; |
| 196 | ctx->wqueue.write_cb = hnb_write_cb; |
| 197 | osmo_fd_register(&ctx->wqueue.bfd); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 198 | |
| 199 | llist_add_tail(&ctx->list, &gw->hnb_list); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 204 | static const struct log_info_cat log_cat[] = { |
| 205 | [DMAIN] = { |
| 206 | .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 207 | .color = "", |
| 208 | .description = "Main program", |
| 209 | }, |
| 210 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 211 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 212 | static const struct log_info hnbgw_log_info = { |
| 213 | .cat = log_cat, |
| 214 | .num_cat = ARRAY_SIZE(log_cat), |
| 215 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 216 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 217 | static struct vty_app_info vty_info = { |
| 218 | .name = "OsmoHNBGW", |
| 219 | .version = "0", |
| 220 | }; |
| 221 | |
| 222 | static int daemonize = 0; |
| 223 | |
Harald Welte | 5c11c94 | 2015-09-07 19:54:49 +0200 | [diff] [blame] | 224 | static int sctp_sock_init(int fd) |
| 225 | { |
| 226 | struct sctp_event_subscribe event; |
| 227 | int rc; |
| 228 | |
| 229 | /* subscribe for all events */ |
| 230 | memset((uint8_t *)&event, 1, sizeof(event)); |
| 231 | rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, |
| 232 | &event, sizeof(event)); |
| 233 | |
| 234 | return rc; |
| 235 | } |
| 236 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 237 | int main(int argc, char **argv) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 238 | { |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 239 | int rc; |
| 240 | |
| 241 | tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context"); |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame^] | 242 | tall_ue_ctx = talloc_named_const(NULL, 0, "ue_context"); |
Harald Welte | 08de638 | 2015-08-31 09:54:45 +0200 | [diff] [blame] | 243 | talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context"); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 244 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 245 | g_hnb_gw.listen_fd.cb = listen_fd_cb; |
| 246 | g_hnb_gw.listen_fd.when = BSC_FD_READ; |
| 247 | g_hnb_gw.listen_fd.data = &g_hnb_gw; |
Harald Welte | 08a793b | 2015-09-07 19:53:46 +0200 | [diff] [blame] | 248 | INIT_LLIST_HEAD(&g_hnb_gw.hnb_list); |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame^] | 249 | INIT_LLIST_HEAD(&g_hnb_gw.ue_list); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 250 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 251 | rc = osmo_init_logging(&hnbgw_log_info); |
| 252 | if (rc < 0) |
| 253 | exit(1); |
| 254 | |
| 255 | vty_init(&vty_info); |
| 256 | |
| 257 | rc = telnet_init(NULL, &g_hnb_gw, 2323); |
| 258 | if (rc < 0) { |
| 259 | perror("Error binding VTY port"); |
| 260 | exit(1); |
| 261 | } |
| 262 | |
| 263 | rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM, |
Harald Welte | 1c0f538 | 2015-09-07 18:46:58 +0200 | [diff] [blame] | 264 | IPPROTO_SCTP, NULL, |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 265 | g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 266 | if (rc < 0) { |
| 267 | perror("Error binding Iuh port"); |
| 268 | exit(1); |
| 269 | } |
Harald Welte | 5c11c94 | 2015-09-07 19:54:49 +0200 | [diff] [blame] | 270 | sctp_sock_init(g_hnb_gw.listen_fd.fd); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 271 | |
| 272 | if (daemonize) { |
| 273 | rc = osmo_daemonize(); |
| 274 | if (rc < 0) { |
| 275 | perror("Error during daemonize"); |
| 276 | exit(1); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | while (1) { |
| 281 | rc = osmo_select_main(0); |
| 282 | if (rc < 0) |
| 283 | exit(3); |
| 284 | } |
| 285 | |
| 286 | /* not reached */ |
| 287 | exit(0); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 288 | } |