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