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