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