Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame] | 1 | /* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Harald Welte | 31c9eca | 2018-10-03 21:03:34 +0200 | [diff] [blame] | 23 | #define _GNU_SOURCE |
| 24 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdint.h> |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 28 | #include <signal.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 29 | #include <unistd.h> |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 30 | #include <errno.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 31 | |
| 32 | #include <pthread.h> |
| 33 | |
| 34 | #include <wintypes.h> |
| 35 | #include <winscard.h> |
| 36 | #include <pcsclite.h> |
| 37 | |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 38 | #include <sys/socket.h> |
| 39 | #include <netdb.h> |
| 40 | |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 41 | #include <osmocom/core/socket.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 42 | #include <osmocom/core/linuxlist.h> |
Harald Welte | f94b9ee | 2018-09-25 15:04:21 +0200 | [diff] [blame] | 43 | #include <osmocom/core/logging.h> |
| 44 | #include <osmocom/core/application.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 45 | |
| 46 | #include <osmocom/gsm/ipa.h> |
| 47 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 48 | |
Kévin Redon | ff5db6e | 2018-10-11 17:16:18 +0200 | [diff] [blame] | 49 | #include <asn_application.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 50 | #include <osmocom/rspro/RsproPDU.h> |
| 51 | |
| 52 | #include "bankd.h" |
Harald Welte | 3cded63 | 2019-03-09 12:59:41 +0100 | [diff] [blame] | 53 | #include "rspro_client_fsm.h" |
Harald Welte | 61d98e9 | 2019-03-03 15:43:07 +0100 | [diff] [blame] | 54 | #include "debug.h" |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 55 | #include "rspro_util.h" |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 56 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 57 | /* signal indicates to worker thread that its map has been deleted */ |
| 58 | #define SIGMAPDEL SIGRTMIN+1 |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 59 | |
| 60 | static void handle_sig_usr1(int sig); |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 61 | static void handle_sig_mapdel(int sig); |
| 62 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 63 | __thread void *talloc_asn1_ctx; |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 64 | struct bankd *g_bankd; |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 65 | static void *g_tall_ctx; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 66 | |
| 67 | static void *worker_main(void *arg); |
| 68 | |
| 69 | /*********************************************************************** |
| 70 | * bankd core / main thread |
| 71 | ***********************************************************************/ |
| 72 | |
Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 73 | int asn_debug; |
| 74 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 75 | static void bankd_init(struct bankd *bankd) |
| 76 | { |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 77 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
Harald Welte | f94b9ee | 2018-09-25 15:04:21 +0200 | [diff] [blame] | 78 | osmo_init_logging2(g_tall_ctx, &log_info); |
| 79 | |
Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 80 | asn_debug = 0; |
| 81 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 82 | /* intialize members of 'bankd' */ |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 83 | bankd->slotmaps = slotmap_init(bankd); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 84 | INIT_LLIST_HEAD(&bankd->workers); |
| 85 | pthread_mutex_init(&bankd->workers_mutex, NULL); |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 86 | |
Harald Welte | a0f3950 | 2019-03-09 20:59:34 +0100 | [diff] [blame] | 87 | /* set some defaults, overridden by commandline/config */ |
| 88 | bankd->cfg.bank_id = 1; |
| 89 | bankd->cfg.num_slots = 8; |
| 90 | |
Harald Welte | f1dd162 | 2018-09-24 14:54:23 +0200 | [diff] [blame] | 91 | bankd->comp_id.type = ComponentType_remsimBankd; |
| 92 | OSMO_STRLCPY_ARRAY(bankd->comp_id.name, "fixme-name"); |
| 93 | OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd"); |
| 94 | OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION); |
| 95 | /* FIXME: other members of app_comp_id */ |
| 96 | |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 97 | /* Np lock or mutex required for the pcsc_slot_names list, as this is only |
| 98 | * read once during bankd initialization, when the worker threads haven't |
| 99 | * started yet */ |
| 100 | INIT_LLIST_HEAD(&bankd->pcsc_slot_names); |
| 101 | OSMO_ASSERT(bankd_pcsc_read_slotnames(bankd, "bankd_pcsc_slots.csv") == 0); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* create + start a new bankd_worker thread */ |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 105 | static struct bankd_worker *bankd_create_worker(struct bankd *bankd, unsigned int i) |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 106 | { |
| 107 | struct bankd_worker *worker; |
| 108 | int rc; |
| 109 | |
| 110 | worker = talloc_zero(bankd, struct bankd_worker); |
| 111 | if (!worker) |
| 112 | return NULL; |
| 113 | |
| 114 | worker->bankd = bankd; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 115 | worker->num = i; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 116 | |
| 117 | /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */ |
| 118 | |
| 119 | rc = pthread_create(&worker->thread, NULL, worker_main, worker); |
| 120 | if (rc != 0) { |
| 121 | talloc_free(worker); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | pthread_mutex_lock(&bankd->workers_mutex); |
| 126 | llist_add_tail(&worker->list, &bankd->workers); |
| 127 | pthread_mutex_unlock(&bankd->workers_mutex); |
| 128 | |
| 129 | return worker; |
| 130 | } |
| 131 | |
| 132 | static bool terminate = false; |
| 133 | |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 134 | /* handle incoming messages from server */ |
| 135 | static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu) |
| 136 | { |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 137 | const CreateMappingReq_t *creq = NULL; |
| 138 | const RemoveMappingReq_t *rreq = NULL; |
| 139 | struct slot_mapping *map; |
| 140 | struct bank_slot bs; |
| 141 | struct client_slot cs; |
| 142 | RsproPDU_t *resp; |
| 143 | |
| 144 | LOGPFSM(srvc->fi, "Rx RSPRO %s\n", rspro_msgt_name(pdu)); |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 145 | |
| 146 | switch (pdu->msg.present) { |
| 147 | case RsproPDUchoice_PR_connectBankRes: |
| 148 | /* Store 'identity' of server in srvc->peer_comp_id */ |
| 149 | rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity); |
| 150 | osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu); |
| 151 | break; |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 152 | case RsproPDUchoice_PR_createMappingReq: |
| 153 | creq = &pdu->msg.choice.createMappingReq; |
| 154 | if (creq->bank.bankId != g_bankd->cfg.bank_id) |
| 155 | resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId); |
| 156 | else if (creq->bank.slotNr >= g_bankd->cfg.num_slots) |
| 157 | resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId); |
| 158 | else { |
| 159 | rspro2bank_slot(&bs, &creq->bank); |
| 160 | rspro2client_slot(&cs, &creq->client); |
| 161 | /* Add a new mapping */ |
| 162 | map = slotmap_add(g_bankd->slotmaps, &bs, &cs); |
| 163 | if (!map) |
| 164 | resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId); |
| 165 | else |
| 166 | resp = rspro_gen_CreateMappingRes(ResultCode_ok); |
| 167 | } |
| 168 | server_conn_send_rspro(srvc, resp); |
| 169 | break; |
| 170 | case RsproPDUchoice_PR_removeMappingReq: |
| 171 | rreq = &pdu->msg.choice.removeMappingReq; |
| 172 | if (rreq->bank.bankId != g_bankd->cfg.bank_id) |
| 173 | resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId); |
| 174 | else if (rreq->bank.slotNr >= g_bankd->cfg.num_slots) |
| 175 | resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId); |
| 176 | else { |
| 177 | rspro2bank_slot(&bs, &rreq->bank); |
| 178 | /* Remove a mapping */ |
| 179 | map = slotmap_by_bank(g_bankd->slotmaps, &bs); |
| 180 | if (!map) |
| 181 | resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap); |
| 182 | else { |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 183 | slotmap_del(g_bankd->slotmaps, map); |
| 184 | resp = rspro_gen_RemoveMappingRes(ResultCode_ok); |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 185 | |
| 186 | /* kill/reset the respective worker, if any! */ |
| 187 | struct bankd_worker *worker; |
| 188 | pthread_mutex_lock(&g_bankd->workers_mutex); |
| 189 | llist_for_each_entry(worker, &g_bankd->workers, list) { |
| 190 | if (bs.bank_id == worker->slot.bank_id && |
| 191 | bs.slot_nr == worker->slot.slot_nr) { |
| 192 | pthread_kill(worker->thread, SIGMAPDEL); |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | pthread_mutex_unlock(&g_bankd->workers_mutex); |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 197 | } |
| 198 | } |
Harald Welte | 942f1ff | 2019-03-09 21:49:08 +0100 | [diff] [blame] | 199 | server_conn_send_rspro(srvc, resp); |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 200 | break; |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 201 | default: |
| 202 | fprintf(stderr, "Unknown/Unsupported RSPRO PDU type: %u\n", pdu->msg.present); |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 209 | |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 210 | void handle_options(int argc, char **argv) |
| 211 | { |
| 212 | } |
| 213 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 214 | int main(int argc, char **argv) |
| 215 | { |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 216 | struct rspro_server_conn *srvc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 217 | int i, rc; |
| 218 | |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 219 | g_bankd = talloc_zero(NULL, struct bankd); |
| 220 | OSMO_ASSERT(g_bankd); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 221 | |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 222 | bankd_init(g_bankd); |
| 223 | |
| 224 | srvc = &g_bankd->srvc; |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 225 | srvc->server_host = "localhost"; |
| 226 | srvc->server_port = 9998; |
| 227 | srvc->handle_rx = bankd_srvc_handle_rx; |
| 228 | srvc->own_comp_id.type = ComponentType_remsimBankd; |
| 229 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "fixme-name"); |
| 230 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd"); |
| 231 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION); |
| 232 | |
| 233 | handle_options(argc, argv); |
| 234 | |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 235 | g_bankd->main = pthread_self(); |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 236 | signal(SIGMAPDEL, handle_sig_mapdel); |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 237 | signal(SIGUSR1, handle_sig_usr1); |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 238 | |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 239 | /* Connection towards remsim-server */ |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 240 | rc = server_conn_fsm_alloc(g_bankd, srvc); |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 241 | if (rc < 0) { |
| 242 | fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno)); |
| 243 | exit(1); |
| 244 | } |
| 245 | |
| 246 | /* create listening socket for inbound client connections */ |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 247 | rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 9999, OSMO_SOCK_F_BIND); |
| 248 | if (rc < 0) |
| 249 | exit(1); |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 250 | g_bankd->accept_fd = rc; |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 251 | |
Harald Welte | a0f3950 | 2019-03-09 20:59:34 +0100 | [diff] [blame] | 252 | /* create worker threads: One per reader/slot! */ |
| 253 | for (i = 0; i < g_bankd->cfg.num_slots; i++) { |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 254 | struct bankd_worker *w; |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 255 | w = bankd_create_worker(g_bankd, i); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 256 | if (!w) |
| 257 | exit(21); |
| 258 | } |
| 259 | |
| 260 | while (1) { |
| 261 | if (terminate) |
| 262 | break; |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 263 | osmo_select_main(0); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 266 | talloc_free(g_bankd); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 267 | exit(0); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | /*********************************************************************** |
| 273 | * bankd worker thread |
| 274 | ***********************************************************************/ |
| 275 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 276 | static __thread struct bankd_worker *g_worker; |
| 277 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 278 | struct value_string worker_state_names[] = { |
| 279 | { BW_ST_INIT, "INIT" }, |
| 280 | { BW_ST_ACCEPTING, "ACCEPTING" }, |
| 281 | { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" }, |
| 282 | { BW_ST_CONN_CLIENT, "CONN_CLIENT" }, |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 283 | { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" }, |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 284 | { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" }, |
| 285 | { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" }, |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 286 | { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" }, |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 287 | { 0, NULL } |
| 288 | }; |
| 289 | |
Harald Welte | ceb3e68 | 2018-08-16 14:47:11 +0200 | [diff] [blame] | 290 | #define LOGW(w, fmt, args...) \ |
| 291 | printf("[%03u %s] %s:%u " fmt, (w)->num, get_value_string(worker_state_names, (w)->state), \ |
| 292 | __FILE__, __LINE__, ## args) |
| 293 | |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 294 | #define PCSC_ERROR(w, rv, text) \ |
| 295 | if (rv != SCARD_S_SUCCESS) { \ |
| 296 | LOGW((w), text ": %s (0x%lX)\n", pcsc_stringify_error(rv), rv); \ |
| 297 | goto end; \ |
| 298 | } else { \ |
Harald Welte | 7b41d9c | 2018-10-03 23:15:10 +0200 | [diff] [blame] | 299 | LOGW((w), ": OK\n"); \ |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 302 | static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state) |
| 303 | { |
| 304 | LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state)); |
| 305 | worker->state = new_state; |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 306 | worker->timeout = 0; |
| 307 | } |
| 308 | |
| 309 | static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state, |
| 310 | unsigned int timeout_secs) |
| 311 | { |
| 312 | LOGW(worker, "Changing state to %s (timeout=%u)\n", |
| 313 | get_value_string(worker_state_names, new_state), timeout_secs); |
| 314 | worker->state = new_state; |
| 315 | worker->timeout = timeout_secs; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 316 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 317 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 318 | /* signal handler for receiving SIGMAPDEL from main thread */ |
| 319 | static void handle_sig_mapdel(int sig) |
| 320 | { |
| 321 | LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n"); |
| 322 | OSMO_ASSERT(sig == SIGMAPDEL); |
| 323 | worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED); |
| 324 | } |
| 325 | |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 326 | static void handle_sig_usr1(int sig) |
| 327 | { |
| 328 | OSMO_ASSERT(sig == SIGUSR1); |
| 329 | |
| 330 | if (pthread_equal(g_bankd->main, pthread_self())) { |
| 331 | struct bankd_worker *worker; |
| 332 | /* main thread */ |
| 333 | fprintf(stderr, "=== Talloc Report of main thread:\n"); |
| 334 | talloc_report(g_tall_ctx, stderr); |
| 335 | |
| 336 | /* iterate over worker threads and ask them to dump their talloc state */ |
| 337 | pthread_mutex_lock(&g_bankd->workers_mutex); |
| 338 | llist_for_each_entry(worker, &g_bankd->workers, list) { |
| 339 | pthread_kill(worker->thread, SIGUSR1); |
| 340 | } |
| 341 | pthread_mutex_unlock(&g_bankd->workers_mutex); |
| 342 | } else { |
| 343 | /* worker thread */ |
| 344 | fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name); |
| 345 | talloc_report(g_worker->tall_ctx, stderr); |
| 346 | } |
| 347 | } |
| 348 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 349 | static void worker_cleanup(void *arg) |
| 350 | { |
| 351 | struct bankd_worker *worker = (struct bankd_worker *) arg; |
| 352 | struct bankd *bankd = worker->bankd; |
| 353 | |
| 354 | /* FIXME: should we still do this? in the thread ?!? */ |
| 355 | pthread_mutex_lock(&bankd->workers_mutex); |
| 356 | llist_del(&worker->list); |
| 357 | talloc_free(worker); /* FIXME: is this safe? */ |
| 358 | pthread_mutex_unlock(&bankd->workers_mutex); |
| 359 | } |
| 360 | |
| 361 | |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 362 | static int worker_open_card(struct bankd_worker *worker) |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 363 | { |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 364 | long rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 365 | |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 366 | OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED); |
| 367 | |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 368 | if (!worker->reader.name) { |
| 369 | /* resolve PC/SC reader name from slot_id -> name map */ |
| 370 | worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot); |
| 371 | if (!worker->reader.name) { |
| 372 | LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n", |
| 373 | worker->slot.bank_id, worker->slot.slot_nr); |
| 374 | rc = -1; |
| 375 | goto end; |
| 376 | } |
| 377 | } |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 378 | OSMO_ASSERT(worker->reader.name); |
| 379 | |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 380 | if (!worker->reader.pcsc.hContext) { |
| 381 | LOGW(worker, "Attempting to open PC/SC context\n"); |
| 382 | /* The PC/SC context must be created inside the thread where we'll later use it */ |
| 383 | rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &worker->reader.pcsc.hContext); |
| 384 | PCSC_ERROR(worker, rc, "SCardEstablishContext") |
| 385 | } |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 386 | |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 387 | if (!worker->reader.pcsc.hCard) { |
| 388 | LOGW(worker, "Attempting to open card/slot '%s'\n", worker->reader.name); |
| 389 | DWORD dwActiveProtocol; |
| 390 | rc = SCardConnect(worker->reader.pcsc.hContext, worker->reader.name, SCARD_SHARE_SHARED, |
| 391 | SCARD_PROTOCOL_T0, &worker->reader.pcsc.hCard, &dwActiveProtocol); |
| 392 | PCSC_ERROR(worker, rc, "SCardConnect") |
| 393 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 394 | |
Harald Welte | 57593f0 | 2018-09-23 19:30:31 +0200 | [diff] [blame] | 395 | worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD); |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 396 | /* FIXME: notify client about this state change */ |
Harald Welte | 57593f0 | 2018-09-23 19:30:31 +0200 | [diff] [blame] | 397 | |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 398 | return 0; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 399 | end: |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 400 | return rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 401 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 402 | |
| 403 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 404 | static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size) |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 405 | { |
| 406 | struct ipaccess_head *hh; |
| 407 | uint16_t len; |
| 408 | int needed, rc; |
| 409 | |
| 410 | if (buf_size < sizeof(*hh)) |
| 411 | return -1; |
| 412 | |
| 413 | hh = (struct ipaccess_head *) buf; |
| 414 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 415 | /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR |
| 416 | * in case of a signal being received */ |
| 417 | |
| 418 | restart_hdr: |
| 419 | /* 1) blocking recv from the socket (IPA header) */ |
| 420 | rc = recv(worker->client.fd, buf, sizeof(*hh), 0); |
| 421 | if (rc == -1 && errno == EINTR) { |
| 422 | if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED) |
| 423 | return -23; |
| 424 | goto restart_hdr; |
| 425 | } else if (rc < 0) |
| 426 | return rc; |
| 427 | else if (rc < sizeof(*hh)) |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 428 | return -2; |
| 429 | |
| 430 | len = ntohs(hh->len); |
| 431 | needed = len; //- sizeof(*hh); |
| 432 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 433 | restart_body: |
| 434 | /* 2) blocking recv from the socket (payload) */ |
| 435 | rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0); |
| 436 | if (rc == -1 && errno == EINTR) { |
| 437 | if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED) |
| 438 | return -23; |
| 439 | goto restart_body; |
| 440 | } else if (rc < 0) |
| 441 | return rc; |
| 442 | else if (rc < needed) |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 443 | return -3; |
| 444 | |
| 445 | return len; |
| 446 | } |
| 447 | |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 448 | static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu) |
| 449 | { |
| 450 | struct msgb *msg = rspro_enc_msg(pdu); |
| 451 | int rc; |
| 452 | |
| 453 | if (!msg) { |
| 454 | LOGW(worker, "error encoding RSPRO\n"); |
| 455 | return -1; |
| 456 | } |
| 457 | |
Harald Welte | fd47119 | 2018-09-24 14:51:14 +0200 | [diff] [blame] | 458 | msg->l2h = msg->data; |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 459 | /* prepend the header */ |
| 460 | ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO); |
Harald Welte | fd47119 | 2018-09-24 14:51:14 +0200 | [diff] [blame] | 461 | ipa_prepend_header(msg, IPAC_PROTO_OSMO); |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 462 | |
| 463 | /* actually send it through the socket */ |
| 464 | rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg)); |
| 465 | if (rc == msgb_length(msg)) |
| 466 | rc = 0; |
| 467 | else { |
| 468 | LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg)); |
| 469 | rc = -1; |
| 470 | } |
| 471 | |
| 472 | msgb_free(msg); |
| 473 | |
| 474 | return rc; |
| 475 | } |
| 476 | |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 477 | /* attempt to obtain slot-map */ |
| 478 | static int worker_try_slotmap(struct bankd_worker *worker) |
| 479 | { |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 480 | struct slot_mapping *slmap; |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 481 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 482 | slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot); |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 483 | if (!slmap) { |
| 484 | LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n", |
| 485 | worker->client.clslot.client_id, worker->client.clslot.slot_nr); |
| 486 | /* check in 10s if the map has been installed meanwhile by main thread */ |
| 487 | worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10); |
| 488 | return -1; |
| 489 | } else { |
| 490 | LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n", |
| 491 | slmap->client.client_id, slmap->client.slot_nr, |
| 492 | slmap->bank.bank_id, slmap->bank.slot_nr); |
| 493 | worker->slot = slmap->bank; |
| 494 | worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10); |
| 495 | return worker_open_card(worker); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 500 | static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 501 | { |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 502 | const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity; |
Harald Welte | 458e01b | 2019-03-10 11:14:43 +0100 | [diff] [blame] | 503 | RsproPDU_t *resp = NULL; |
Harald Welte | 3e68987 | 2018-09-24 14:52:56 +0200 | [diff] [blame] | 504 | e_ResultCode res; |
Harald Welte | 458e01b | 2019-03-10 11:14:43 +0100 | [diff] [blame] | 505 | int rc; |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 506 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 507 | OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq); |
| 508 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 509 | LOGW(worker, "connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n", |
| 510 | cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf); |
| 511 | /* FIXME: store somewhere? */ |
| 512 | |
| 513 | if (worker->state != BW_ST_CONN_WAIT_ID) { |
| 514 | LOGW(worker, "Unexpected connectClientReq\n"); |
Harald Welte | 458e01b | 2019-03-10 11:14:43 +0100 | [diff] [blame] | 515 | rc = -102; |
| 516 | goto respond_and_err; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 517 | } |
| 518 | |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 519 | if (!pdu->msg.choice.connectClientReq.clientSlot) { |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 520 | LOGW(worker, "missing clientID, aborting\n"); |
Harald Welte | 458e01b | 2019-03-10 11:14:43 +0100 | [diff] [blame] | 521 | res = ResultCode_illegalClientId; |
| 522 | rc = -103; |
| 523 | goto respond_and_err; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 524 | } |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 525 | worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId; |
| 526 | worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 527 | worker_set_state(worker, BW_ST_CONN_CLIENT); |
| 528 | |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 529 | if (worker_try_slotmap(worker) >= 0) |
| 530 | res = ResultCode_ok; |
| 531 | else |
Harald Welte | 3e68987 | 2018-09-24 14:52:56 +0200 | [diff] [blame] | 532 | res = ResultCode_cardNotPresent; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 533 | |
Harald Welte | 3e68987 | 2018-09-24 14:52:56 +0200 | [diff] [blame] | 534 | resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res); |
| 535 | return worker_send_rspro(worker, resp); |
Harald Welte | 458e01b | 2019-03-10 11:14:43 +0100 | [diff] [blame] | 536 | |
| 537 | respond_and_err: |
| 538 | if (res) { |
| 539 | resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res); |
| 540 | worker_send_rspro(worker, resp); |
| 541 | } |
| 542 | return rc; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 543 | } |
| 544 | |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 545 | static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 546 | { |
| 547 | const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard; |
| 548 | const SCARD_IO_REQUEST *pioSendPci = SCARD_PCI_T0; |
| 549 | SCARD_IO_REQUEST pioRecvPci; |
| 550 | uint8_t rx_buf[1024]; |
| 551 | DWORD rx_buf_len = sizeof(rx_buf); |
| 552 | RsproPDU_t *pdu_resp; |
Harald Welte | e1d3289 | 2019-03-27 20:47:42 +0100 | [diff] [blame^] | 553 | struct client_slot clslot; |
| 554 | struct bank_slot bslot; |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 555 | long rc; |
| 556 | |
| 557 | LOGW(worker, "tpduModemToCard(%s)\n", osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size)); |
| 558 | |
| 559 | if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) { |
| 560 | LOGW(worker, "Unexpected tpduModemToCaard\n"); |
| 561 | return -104; |
| 562 | } |
| 563 | |
Harald Welte | e1d3289 | 2019-03-27 20:47:42 +0100 | [diff] [blame^] | 564 | /* Validate that toBankSlot / fromClientSlot match our expectations */ |
| 565 | rspro2client_slot(&clslot, &mdm2sim->fromClientSlot); |
| 566 | rspro2bank_slot(&bslot, &mdm2sim->toBankSlot); |
| 567 | if (!bank_slot_equals(&worker->slot, &bslot)) { |
| 568 | LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n", |
| 569 | bslot.bank_id, bslot.slot_nr); |
| 570 | return -105; |
| 571 | } |
| 572 | if (!client_slot_equals(&worker->client.clslot, &clslot)) { |
| 573 | LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n", |
| 574 | clslot.client_id, clslot.slot_nr); |
| 575 | return -106; |
| 576 | } |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 577 | |
| 578 | rc = SCardTransmit(worker->reader.pcsc.hCard, |
| 579 | pioSendPci, mdm2sim->data.buf, mdm2sim->data.size, |
| 580 | &pioRecvPci, rx_buf, &rx_buf_len); |
| 581 | PCSC_ERROR(worker, rc, "SCardTransmit"); |
| 582 | |
| 583 | /* encode response PDU and send it */ |
| 584 | pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot, |
| 585 | rx_buf, rx_buf_len); |
| 586 | worker_send_rspro(worker, pdu_resp); |
| 587 | |
| 588 | return 0; |
| 589 | end: |
| 590 | return rc; |
| 591 | } |
| 592 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 593 | /* handle one incoming RSPRO message from a client inside a worker thread */ |
| 594 | static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 595 | { |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 596 | int rc = -100; |
| 597 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 598 | switch (pdu->msg.present) { |
| 599 | case RsproPDUchoice_PR_connectClientReq: |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 600 | rc = worker_handle_connectClientReq(worker, pdu); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 601 | break; |
| 602 | case RsproPDUchoice_PR_tpduModemToCard: |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 603 | rc = worker_handle_tpduModemToCard(worker, pdu); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 604 | break; |
| 605 | case RsproPDUchoice_PR_clientSlotStatusInd: |
| 606 | /* FIXME */ |
| 607 | break; |
| 608 | default: |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 609 | rc = -101; |
| 610 | break; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 613 | return rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 614 | } |
| 615 | |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 616 | static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs) |
| 617 | { |
| 618 | struct timeval tout = { timeout_secs, 0 }; |
| 619 | fd_set readset; |
| 620 | |
| 621 | FD_ZERO(&readset); |
| 622 | FD_SET(fd, &readset); |
| 623 | return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL); |
| 624 | } |
| 625 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 626 | /* body of the main transceive loop */ |
| 627 | static int worker_transceive_loop(struct bankd_worker *worker) |
| 628 | { |
| 629 | struct ipaccess_head *hh; |
| 630 | struct ipaccess_head_ext *hh_ext; |
| 631 | uint8_t buf[65536]; /* maximum length expressed in 16bit length field */ |
| 632 | asn_dec_rval_t rval; |
| 633 | int data_len, rc; |
Harald Welte | 9ebbacc | 2018-09-24 17:43:39 +0200 | [diff] [blame] | 634 | RsproPDU_t *pdu = NULL; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 635 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 636 | restart_wait: |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 637 | rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout); |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 638 | if (rc == -1 && errno == EINTR) { |
| 639 | if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED) |
| 640 | return -23; |
| 641 | goto restart_wait; |
| 642 | } else if (rc < 0) |
| 643 | return rc; |
| 644 | else if (rc == 0) { |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 645 | /* TIMEOUT case */ |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 646 | switch (worker->state) { |
| 647 | case BW_ST_CONN_CLIENT_WAIT_MAP: |
| 648 | /* re-check if mapping exists meanwhile? */ |
| 649 | worker_try_slotmap(worker); |
| 650 | break; |
| 651 | case BW_ST_CONN_CLIENT_MAPPED: |
| 652 | /* re-check if reader/card can be opened meanwhile? */ |
| 653 | worker_open_card(worker); |
| 654 | break; |
| 655 | default: |
| 656 | OSMO_ASSERT(0); |
| 657 | } |
| 658 | /* return early, so we do another select rather than the blocking read below */ |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 659 | return 0; |
| 660 | }; |
| 661 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 662 | /* 1) blocking read of entire IPA message from the socket */ |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 663 | rc = blocking_ipa_read(worker, buf, sizeof(buf)); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 664 | if (rc < 0) |
| 665 | return rc; |
| 666 | data_len = rc; |
| 667 | |
| 668 | hh = (struct ipaccess_head *) buf; |
Harald Welte | 5a3613a | 2018-10-11 12:56:21 +0200 | [diff] [blame] | 669 | if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) { |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 670 | LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 671 | return -4; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 672 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 673 | |
Harald Welte | 5a3613a | 2018-10-11 12:56:21 +0200 | [diff] [blame] | 674 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
Harald Welte | 19f881a | 2019-03-11 18:39:13 +0100 | [diff] [blame] | 675 | switch (hh->data[0]) { |
| 676 | case IPAC_MSGT_PING: |
| 677 | return ipa_ccm_send_pong(worker->client.fd); |
| 678 | default: |
| 679 | LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]); |
| 680 | break; |
| 681 | } |
Harald Welte | 5a3613a | 2018-10-11 12:56:21 +0200 | [diff] [blame] | 682 | return 0; |
| 683 | } |
| 684 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 685 | hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh); |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 686 | if (data_len < sizeof(*hh_ext)) { |
| 687 | LOGW(worker, "Received short message\n"); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 688 | return -5; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 689 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 690 | data_len -= sizeof(*hh_ext); |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 691 | if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) { |
| 692 | LOGW(worker, "Received unsupported IPA EXT protocol != RSPRO: 0x%02x\n", hh_ext->proto); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 693 | return -6; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 694 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 695 | |
| 696 | /* 2) ASN1 BER decode of the message */ |
| 697 | rval = ber_decode(NULL, &asn_DEF_RsproPDU, (void **) &pdu, hh_ext->data, data_len); |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 698 | if (rval.code != RC_OK) { |
| 699 | LOGW(worker, "Error during BER decode of RSPRO\n"); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 700 | return -7; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 701 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 702 | |
| 703 | /* 3) handling of the message, possibly resulting in PCSC commands */ |
| 704 | rc = worker_handle_rspro(worker, pdu); |
| 705 | ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu); |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 706 | if (rc < 0) { |
| 707 | LOGW(worker, "Error handling RSPRO\n"); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 708 | return rc; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 709 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 710 | |
| 711 | /* everything OK if we reach here */ |
| 712 | return 0; |
| 713 | } |
| 714 | |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 715 | /* obtain an ascii representation of the client IP/port */ |
| 716 | static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker) |
| 717 | { |
| 718 | char hostbuf[32], portbuf[32]; |
| 719 | int rc; |
| 720 | |
| 721 | rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr, |
| 722 | worker->client.peer_addr_len, hostbuf, sizeof(hostbuf), |
| 723 | portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV); |
| 724 | if (rc != 0) { |
| 725 | out[0] = '\0'; |
| 726 | return -1; |
| 727 | } |
| 728 | snprintf(out, outlen, "%s:%s", hostbuf, portbuf); |
| 729 | return 0; |
| 730 | } |
| 731 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 732 | /* worker thread main function */ |
| 733 | static void *worker_main(void *arg) |
| 734 | { |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 735 | void *top_ctx; |
| 736 | int rc; |
Harald Welte | 31c9eca | 2018-10-03 21:03:34 +0200 | [diff] [blame] | 737 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 738 | g_worker = (struct bankd_worker *) arg; |
| 739 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 740 | worker_set_state(g_worker, BW_ST_INIT); |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 741 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 742 | /* not permitted in multithreaded environment */ |
| 743 | talloc_disable_null_tracking(); |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 744 | g_worker->tall_ctx = talloc_named_const(NULL, 0, "top"); |
| 745 | talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1"); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 746 | |
Harald Welte | 286a2be | 2019-03-11 17:36:58 +0100 | [diff] [blame] | 747 | /* set the thread name */ |
| 748 | g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num); |
| 749 | pthread_setname_np(pthread_self(), g_worker->name); |
| 750 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 751 | /* push cleanup helper */ |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 752 | pthread_cleanup_push(&worker_cleanup, g_worker); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 753 | |
| 754 | /* we continuously perform the same loop here, recycling the worker thread |
| 755 | * once the client connection is gone or we have some trouble with the card/reader */ |
| 756 | while (1) { |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 757 | char buf[128]; |
| 758 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 759 | g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 760 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 761 | worker_set_state(g_worker, BW_ST_ACCEPTING); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 762 | /* first wait for an incoming TCP connection */ |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 763 | rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr, |
| 764 | &g_worker->client.peer_addr_len); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 765 | if (rc < 0) { |
| 766 | continue; |
| 767 | } |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 768 | g_worker->client.fd = rc; |
| 769 | worker_client_addrstr(buf, sizeof(buf), g_worker); |
| 770 | LOGW(g_worker, "Accepted connection from %s\n", buf); |
| 771 | worker_set_state(g_worker, BW_ST_CONN_WAIT_ID); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 772 | |
| 773 | /* run the main worker transceive loop body until there was some error */ |
| 774 | while (1) { |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 775 | rc = worker_transceive_loop(g_worker); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 776 | if (rc < 0) |
| 777 | break; |
Harald Welte | 653d6a0 | 2019-03-11 18:38:44 +0100 | [diff] [blame] | 778 | if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED) |
| 779 | break; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 780 | } |
| 781 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 782 | LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc); |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 783 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 784 | /* clean-up: reset to sane state */ |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 785 | if (g_worker->reader.pcsc.hCard) { |
| 786 | SCardDisconnect(g_worker->reader.pcsc.hCard, SCARD_UNPOWER_CARD); |
| 787 | g_worker->reader.pcsc.hCard = 0; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 788 | } |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 789 | if (g_worker->reader.pcsc.hContext) { |
| 790 | SCardReleaseContext(g_worker->reader.pcsc.hContext); |
| 791 | g_worker->reader.pcsc.hContext = 0; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 792 | } |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 793 | if (g_worker->reader.name) |
| 794 | g_worker->reader.name = NULL; |
| 795 | if (g_worker->client.fd >= 0) |
| 796 | close(g_worker->client.fd); |
| 797 | memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr)); |
| 798 | g_worker->client.fd = -1; |
| 799 | g_worker->client.clslot.client_id = g_worker->client.clslot.slot_nr = 0; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | pthread_cleanup_pop(1); |
| 803 | talloc_free(top_ctx); |
| 804 | pthread_exit(NULL); |
| 805 | } |