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