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 | |
| 55 | switch (sinfo.sinfo_ppid) { |
| 56 | case IUH_PPI_HNBAP: |
| 57 | rc = hnbgw_hnbap_rx(hnb, msg); |
| 58 | break; |
| 59 | case IUH_PPI_RUA: |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 60 | //rc = hnbgw_rua_rx(hnb, msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 61 | break; |
| 62 | case IUH_PPI_SABP: |
| 63 | case IUH_PPI_RNA: |
| 64 | case IUH_PPI_PUA: |
| 65 | LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n", |
| 66 | sinfo.sinfo_ppid); |
| 67 | rc = 0; |
| 68 | break; |
| 69 | default: |
| 70 | LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n", |
| 71 | sinfo.sinfo_ppid); |
| 72 | rc = 0; |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | return rc; |
| 77 | } |
| 78 | |
| 79 | /*! call-back when the listen FD has something to read */ |
| 80 | static int listen_fd_cb(struct osmo_fd *fd, unsigned int what) |
| 81 | { |
| 82 | struct hnb_gw *gw = fd->data; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 83 | struct hnb_context *ctx; |
| 84 | struct sockaddr_storage sockaddr; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 85 | socklen_t len = sizeof(sockaddr); |
| 86 | |
| 87 | int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len); |
| 88 | if (new_fd < 0) { |
| 89 | LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n"); |
| 90 | return new_fd; |
| 91 | } |
| 92 | |
| 93 | LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n"); |
| 94 | |
| 95 | ctx = talloc_zero(tall_hnb_ctx, struct hnb_context); |
| 96 | if (!ctx) |
| 97 | return -ENOMEM; |
| 98 | |
| 99 | ctx->gw = gw; |
| 100 | ctx->socket.data = ctx; |
| 101 | ctx->socket.fd = new_fd; |
| 102 | ctx->socket.when = BSC_FD_READ; |
| 103 | ctx->socket.cb = hnb_socket_cb; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 104 | osmo_fd_register(&ctx->socket); |
| 105 | |
| 106 | llist_add_tail(&ctx->list, &gw->hnb_list); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 111 | static const struct log_info_cat log_cat[] = { |
| 112 | [DMAIN] = { |
| 113 | .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 114 | .color = "", |
| 115 | .description = "Main program", |
| 116 | }, |
| 117 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 118 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 119 | static const struct log_info hnbgw_log_info = { |
| 120 | .cat = log_cat, |
| 121 | .num_cat = ARRAY_SIZE(log_cat), |
| 122 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 123 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 124 | static struct vty_app_info vty_info = { |
| 125 | .name = "OsmoHNBGW", |
| 126 | .version = "0", |
| 127 | }; |
| 128 | |
| 129 | static int daemonize = 0; |
| 130 | |
| 131 | int main(int argc, char **argv) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 132 | { |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 133 | int rc; |
| 134 | |
| 135 | tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context"); |
Harald Welte | 08de638 | 2015-08-31 09:54:45 +0200 | [diff] [blame] | 136 | talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context"); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 137 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 138 | g_hnb_gw.listen_fd.cb = listen_fd_cb; |
| 139 | g_hnb_gw.listen_fd.when = BSC_FD_READ; |
| 140 | g_hnb_gw.listen_fd.data = &g_hnb_gw; |
Harald Welte | 08a793b | 2015-09-07 19:53:46 +0200 | [diff] [blame^] | 141 | INIT_LLIST_HEAD(&g_hnb_gw.hnb_list); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 142 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 143 | rc = osmo_init_logging(&hnbgw_log_info); |
| 144 | if (rc < 0) |
| 145 | exit(1); |
| 146 | |
| 147 | vty_init(&vty_info); |
| 148 | |
| 149 | rc = telnet_init(NULL, &g_hnb_gw, 2323); |
| 150 | if (rc < 0) { |
| 151 | perror("Error binding VTY port"); |
| 152 | exit(1); |
| 153 | } |
| 154 | |
| 155 | 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] | 156 | IPPROTO_SCTP, NULL, |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 157 | g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 158 | if (rc < 0) { |
| 159 | perror("Error binding Iuh port"); |
| 160 | exit(1); |
| 161 | } |
| 162 | |
| 163 | if (daemonize) { |
| 164 | rc = osmo_daemonize(); |
| 165 | if (rc < 0) { |
| 166 | perror("Error during daemonize"); |
| 167 | exit(1); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | while (1) { |
| 172 | rc = osmo_select_main(0); |
| 173 | if (rc < 0) |
| 174 | exit(3); |
| 175 | } |
| 176 | |
| 177 | /* not reached */ |
| 178 | exit(0); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 179 | } |