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" |
| 26 | |
| 27 | static void *tall_hnb_ctx; |
Harald Welte | 08de638 | 2015-08-31 09:54:45 +0200 | [diff] [blame] | 28 | void *talloc_asn1_ctx; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 29 | |
| 30 | struct hnb_gw g_hnb_gw = { |
| 31 | .config = { |
| 32 | .iuh_listen_port = IUH_DEFAULT_SCTP_PORT, |
| 33 | }, |
| 34 | }; |
| 35 | |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 36 | static int hnb_read_cb(struct osmo_fd *fd) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 37 | { |
| 38 | struct hnb_context *hnb = fd->data; |
| 39 | struct sctp_sndrcvinfo sinfo; |
| 40 | struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx"); |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 41 | int flags = 0; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 42 | int rc; |
| 43 | |
| 44 | if (!msg) |
| 45 | return -ENOMEM; |
| 46 | |
| 47 | rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg), |
| 48 | NULL, NULL, &sinfo, &flags); |
| 49 | if (rc < 0) { |
| 50 | LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n"); |
Harald Welte | ee77cff | 2015-08-30 16:57:53 +0200 | [diff] [blame] | 51 | /* FIXME: clean up after disappeared HNB */ |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 52 | return rc; |
| 53 | } else |
| 54 | msgb_put(msg, rc); |
| 55 | |
Harald Welte | 17878e2 | 2015-09-08 00:09:13 +0200 | [diff] [blame] | 56 | if (flags & MSG_NOTIFICATION) { |
| 57 | LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n"); |
| 58 | msgb_free(msg); |
| 59 | return 0; |
| 60 | } |
| 61 | |
Harald Welte | 5c11c94 | 2015-09-07 19:54:49 +0200 | [diff] [blame] | 62 | sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid); |
| 63 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 64 | switch (sinfo.sinfo_ppid) { |
| 65 | case IUH_PPI_HNBAP: |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 66 | hnb->hnbap_stream = sinfo.sinfo_stream; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 67 | rc = hnbgw_hnbap_rx(hnb, msg); |
| 68 | break; |
| 69 | case IUH_PPI_RUA: |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 70 | hnb->rua_stream = sinfo.sinfo_stream; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 71 | //rc = hnbgw_rua_rx(hnb, msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 72 | break; |
| 73 | case IUH_PPI_SABP: |
| 74 | case IUH_PPI_RNA: |
| 75 | case IUH_PPI_PUA: |
| 76 | LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n", |
| 77 | sinfo.sinfo_ppid); |
| 78 | rc = 0; |
| 79 | break; |
| 80 | default: |
| 81 | LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n", |
| 82 | sinfo.sinfo_ppid); |
| 83 | rc = 0; |
| 84 | break; |
| 85 | } |
| 86 | |
Harald Welte | f2f3000 | 2015-09-08 00:09:23 +0200 | [diff] [blame] | 87 | msgb_free(msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 88 | return rc; |
| 89 | } |
| 90 | |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 91 | static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg) |
| 92 | { |
| 93 | struct hnb_context *ctx = fd->data; |
| 94 | struct sctp_sndrcvinfo sinfo = { |
Harald Welte | cfcc1e6 | 2015-09-07 22:39:56 +0200 | [diff] [blame] | 95 | .sinfo_ppid = htonl(msgb_ppid(msg)), |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 96 | .sinfo_stream = ctx->hnbap_stream, |
| 97 | }; |
| 98 | int rc; |
| 99 | |
| 100 | rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg), |
| 101 | &sinfo, 0); |
Harald Welte | 9e270b4 | 2015-09-07 22:41:26 +0200 | [diff] [blame] | 102 | /* 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] | 103 | return rc; |
| 104 | } |
| 105 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 106 | /*! call-back when the listen FD has something to read */ |
| 107 | static int listen_fd_cb(struct osmo_fd *fd, unsigned int what) |
| 108 | { |
| 109 | struct hnb_gw *gw = fd->data; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 110 | struct hnb_context *ctx; |
| 111 | struct sockaddr_storage sockaddr; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 112 | socklen_t len = sizeof(sockaddr); |
| 113 | |
| 114 | int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len); |
| 115 | if (new_fd < 0) { |
| 116 | LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n"); |
| 117 | return new_fd; |
| 118 | } |
| 119 | |
| 120 | LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n"); |
| 121 | |
| 122 | ctx = talloc_zero(tall_hnb_ctx, struct hnb_context); |
| 123 | if (!ctx) |
| 124 | return -ENOMEM; |
| 125 | |
| 126 | ctx->gw = gw; |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 127 | osmo_wqueue_init(&ctx->wqueue, 16); |
| 128 | ctx->wqueue.bfd.data = ctx; |
| 129 | ctx->wqueue.bfd.fd = new_fd; |
| 130 | ctx->wqueue.bfd.when = BSC_FD_READ; |
| 131 | ctx->wqueue.read_cb = hnb_read_cb; |
| 132 | ctx->wqueue.write_cb = hnb_write_cb; |
| 133 | osmo_fd_register(&ctx->wqueue.bfd); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 134 | |
| 135 | llist_add_tail(&ctx->list, &gw->hnb_list); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 140 | static const struct log_info_cat log_cat[] = { |
| 141 | [DMAIN] = { |
| 142 | .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 143 | .color = "", |
| 144 | .description = "Main program", |
| 145 | }, |
| 146 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 147 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 148 | static const struct log_info hnbgw_log_info = { |
| 149 | .cat = log_cat, |
| 150 | .num_cat = ARRAY_SIZE(log_cat), |
| 151 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 152 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 153 | static struct vty_app_info vty_info = { |
| 154 | .name = "OsmoHNBGW", |
| 155 | .version = "0", |
| 156 | }; |
| 157 | |
| 158 | static int daemonize = 0; |
| 159 | |
Harald Welte | 5c11c94 | 2015-09-07 19:54:49 +0200 | [diff] [blame] | 160 | static int sctp_sock_init(int fd) |
| 161 | { |
| 162 | struct sctp_event_subscribe event; |
| 163 | int rc; |
| 164 | |
| 165 | /* subscribe for all events */ |
| 166 | memset((uint8_t *)&event, 1, sizeof(event)); |
| 167 | rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, |
| 168 | &event, sizeof(event)); |
| 169 | |
| 170 | return rc; |
| 171 | } |
| 172 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 173 | int main(int argc, char **argv) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 174 | { |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 175 | int rc; |
| 176 | |
| 177 | tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context"); |
Harald Welte | 08de638 | 2015-08-31 09:54:45 +0200 | [diff] [blame] | 178 | talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context"); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 179 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 180 | g_hnb_gw.listen_fd.cb = listen_fd_cb; |
| 181 | g_hnb_gw.listen_fd.when = BSC_FD_READ; |
| 182 | g_hnb_gw.listen_fd.data = &g_hnb_gw; |
Harald Welte | 08a793b | 2015-09-07 19:53:46 +0200 | [diff] [blame] | 183 | INIT_LLIST_HEAD(&g_hnb_gw.hnb_list); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 184 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 185 | rc = osmo_init_logging(&hnbgw_log_info); |
| 186 | if (rc < 0) |
| 187 | exit(1); |
| 188 | |
| 189 | vty_init(&vty_info); |
| 190 | |
| 191 | rc = telnet_init(NULL, &g_hnb_gw, 2323); |
| 192 | if (rc < 0) { |
| 193 | perror("Error binding VTY port"); |
| 194 | exit(1); |
| 195 | } |
| 196 | |
| 197 | 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] | 198 | IPPROTO_SCTP, NULL, |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 199 | g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 200 | if (rc < 0) { |
| 201 | perror("Error binding Iuh port"); |
| 202 | exit(1); |
| 203 | } |
Harald Welte | 5c11c94 | 2015-09-07 19:54:49 +0200 | [diff] [blame] | 204 | sctp_sock_init(g_hnb_gw.listen_fd.fd); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 205 | |
| 206 | if (daemonize) { |
| 207 | rc = osmo_daemonize(); |
| 208 | if (rc < 0) { |
| 209 | perror("Error during daemonize"); |
| 210 | exit(1); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | while (1) { |
| 215 | rc = osmo_select_main(0); |
| 216 | if (rc < 0) |
| 217 | exit(3); |
| 218 | } |
| 219 | |
| 220 | /* not reached */ |
| 221 | exit(0); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 222 | } |