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 | * |
Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame] | 17 | */ |
| 18 | |
Harald Welte | 31c9eca | 2018-10-03 21:03:34 +0200 | [diff] [blame] | 19 | #define _GNU_SOURCE |
| 20 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdint.h> |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 24 | #include <signal.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 25 | #include <unistd.h> |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 26 | #include <errno.h> |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 27 | #include <getopt.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 28 | |
| 29 | #include <pthread.h> |
| 30 | |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 31 | #include <sys/socket.h> |
| 32 | #include <netdb.h> |
| 33 | |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 34 | #include <osmocom/core/socket.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 35 | #include <osmocom/core/linuxlist.h> |
Harald Welte | f94b9ee | 2018-09-25 15:04:21 +0200 | [diff] [blame] | 36 | #include <osmocom/core/logging.h> |
| 37 | #include <osmocom/core/application.h> |
Harald Welte | 73bbd54 | 2021-12-08 20:39:55 +0100 | [diff] [blame] | 38 | #include <osmocom/core/fsm.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 39 | |
| 40 | #include <osmocom/gsm/ipa.h> |
| 41 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 42 | |
Kévin Redon | ff5db6e | 2018-10-11 17:16:18 +0200 | [diff] [blame] | 43 | #include <asn_application.h> |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 44 | #include <osmocom/rspro/RsproPDU.h> |
| 45 | |
| 46 | #include "bankd.h" |
Harald Welte | 3cded63 | 2019-03-09 12:59:41 +0100 | [diff] [blame] | 47 | #include "rspro_client_fsm.h" |
Harald Welte | 61d98e9 | 2019-03-03 15:43:07 +0100 | [diff] [blame] | 48 | #include "debug.h" |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 49 | #include "rspro_util.h" |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 50 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 51 | /* signal indicates to worker thread that its map has been deleted */ |
| 52 | #define SIGMAPDEL SIGRTMIN+1 |
Harald Welte | 0fd77a5 | 2019-12-05 22:05:39 +0100 | [diff] [blame] | 53 | #define SIGMAPADD SIGRTMIN+2 |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 54 | |
| 55 | static void handle_sig_usr1(int sig); |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 56 | static void handle_sig_mapdel(int sig); |
Harald Welte | 0fd77a5 | 2019-12-05 22:05:39 +0100 | [diff] [blame] | 57 | static void handle_sig_mapadd(int sig); |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 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 | eea631b | 2022-05-03 15:14:19 +0200 | [diff] [blame] | 62 | static char g_hostname[256]; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 63 | |
| 64 | static void *worker_main(void *arg); |
| 65 | |
| 66 | /*********************************************************************** |
| 67 | * bankd core / main thread |
| 68 | ***********************************************************************/ |
| 69 | |
Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 70 | int asn_debug; |
| 71 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 72 | static void bankd_init(struct bankd *bankd) |
| 73 | { |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 74 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
Harald Welte | f94b9ee | 2018-09-25 15:04:21 +0200 | [diff] [blame] | 75 | osmo_init_logging2(g_tall_ctx, &log_info); |
Harald Welte | 73bbd54 | 2021-12-08 20:39:55 +0100 | [diff] [blame] | 76 | log_set_print_level(osmo_stderr_target, 1); |
| 77 | log_set_print_category(osmo_stderr_target, 1); |
| 78 | log_set_print_category_hex(osmo_stderr_target, 0); |
| 79 | osmo_fsm_log_addr(0); |
Harald Welte | a9d7ad1 | 2021-12-08 21:09:12 +0100 | [diff] [blame] | 80 | log_set_print_tid(osmo_stderr_target, 1); |
| 81 | log_enable_multithread(); |
Harald Welte | f94b9ee | 2018-09-25 15:04:21 +0200 | [diff] [blame] | 82 | |
Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 83 | asn_debug = 0; |
| 84 | |
Harald Welte | a9bb9a9 | 2022-05-03 15:28:02 +0200 | [diff] [blame] | 85 | /* initialize members of 'bankd' */ |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 86 | bankd->slotmaps = slotmap_init(bankd); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 87 | INIT_LLIST_HEAD(&bankd->workers); |
| 88 | pthread_mutex_init(&bankd->workers_mutex, NULL); |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 89 | |
Harald Welte | a0f3950 | 2019-03-09 20:59:34 +0100 | [diff] [blame] | 90 | /* set some defaults, overridden by commandline/config */ |
Harald Welte | 2513d81 | 2019-04-01 21:03:02 +0200 | [diff] [blame] | 91 | bankd->srvc.bankd.bank_id = 1; |
| 92 | bankd->srvc.bankd.num_slots = 8; |
Harald Welte | a0f3950 | 2019-03-09 20:59:34 +0100 | [diff] [blame] | 93 | |
Harald Welte | f1dd162 | 2018-09-24 14:54:23 +0200 | [diff] [blame] | 94 | bankd->comp_id.type = ComponentType_remsimBankd; |
Harald Welte | eea631b | 2022-05-03 15:14:19 +0200 | [diff] [blame] | 95 | OSMO_STRLCPY_ARRAY(bankd->comp_id.name, g_hostname); |
Harald Welte | f1dd162 | 2018-09-24 14:54:23 +0200 | [diff] [blame] | 96 | OSMO_STRLCPY_ARRAY(bankd->comp_id.software, "remsim-bankd"); |
| 97 | OSMO_STRLCPY_ARRAY(bankd->comp_id.sw_version, PACKAGE_VERSION); |
| 98 | /* FIXME: other members of app_comp_id */ |
| 99 | |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 100 | INIT_LLIST_HEAD(&bankd->pcsc_slot_names); |
Harald Welte | 628672b | 2022-07-13 15:20:13 +0200 | [diff] [blame] | 101 | |
| 102 | bankd->cfg.permit_shared_pcsc = false; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* create + start a new bankd_worker thread */ |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 106 | 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] | 107 | { |
| 108 | struct bankd_worker *worker; |
| 109 | int rc; |
| 110 | |
| 111 | worker = talloc_zero(bankd, struct bankd_worker); |
| 112 | if (!worker) |
| 113 | return NULL; |
| 114 | |
| 115 | worker->bankd = bankd; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 116 | worker->num = i; |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 117 | worker->ops = &pcsc_driver_ops; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 118 | |
| 119 | /* in the initial state, the worker has no client.fd, bank_slot or pcsc handle yet */ |
| 120 | |
| 121 | rc = pthread_create(&worker->thread, NULL, worker_main, worker); |
| 122 | if (rc != 0) { |
| 123 | talloc_free(worker); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | pthread_mutex_lock(&bankd->workers_mutex); |
| 128 | llist_add_tail(&worker->list, &bankd->workers); |
| 129 | pthread_mutex_unlock(&bankd->workers_mutex); |
| 130 | |
| 131 | return worker; |
| 132 | } |
| 133 | |
| 134 | static bool terminate = false; |
| 135 | |
Harald Welte | a496783 | 2019-12-05 22:04:57 +0100 | [diff] [blame] | 136 | /* deliver given signal 'sig' to the firts worker matching bs and cs (if given) */ |
| 137 | static void send_signal_to_worker(const struct bank_slot *bs, const struct client_slot *cs, int sig) |
| 138 | { |
| 139 | struct bankd_worker *worker; |
| 140 | pthread_mutex_lock(&g_bankd->workers_mutex); |
| 141 | llist_for_each_entry(worker, &g_bankd->workers, list) { |
| 142 | if (bs && (bs->bank_id != worker->slot.bank_id || bs->slot_nr != worker->slot.slot_nr)) |
| 143 | continue; |
| 144 | if (cs && (cs->client_id != worker->client.clslot.client_id || |
| 145 | cs->slot_nr != worker->client.clslot.slot_nr)) |
| 146 | continue; |
| 147 | |
| 148 | pthread_kill(worker->thread, sig); |
| 149 | break; |
| 150 | } |
| 151 | pthread_mutex_unlock(&g_bankd->workers_mutex); |
| 152 | } |
| 153 | |
Harald Welte | c650a4d | 2019-12-04 15:02:27 +0100 | [diff] [blame] | 154 | /* Remove a mapping */ |
| 155 | static void bankd_srvc_remove_mapping(struct slot_mapping *map) |
| 156 | { |
| 157 | struct bank_slot bs = map->bank; |
| 158 | |
| 159 | slotmap_del(g_bankd->slotmaps, map); |
| 160 | |
| 161 | /* kill/reset the respective worker, if any! */ |
Harald Welte | a496783 | 2019-12-05 22:04:57 +0100 | [diff] [blame] | 162 | send_signal_to_worker(&bs, NULL, SIGMAPDEL); |
Harald Welte | c650a4d | 2019-12-04 15:02:27 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 165 | /* handle incoming messages from server */ |
| 166 | static int bankd_srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu) |
| 167 | { |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 168 | const CreateMappingReq_t *creq = NULL; |
| 169 | const RemoveMappingReq_t *rreq = NULL; |
Harald Welte | f34fb79 | 2019-12-04 19:51:32 +0100 | [diff] [blame] | 170 | struct bankd_worker *worker; |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 171 | struct slot_mapping *map; |
| 172 | struct bank_slot bs; |
| 173 | struct client_slot cs; |
| 174 | RsproPDU_t *resp; |
| 175 | |
Harald Welte | 079c068 | 2022-04-27 09:26:52 +0200 | [diff] [blame] | 176 | LOGPFSML(srvc->fi, LOGL_DEBUG, "Rx RSPRO %s\n", rspro_msgt_name(pdu)); |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 177 | |
| 178 | switch (pdu->msg.present) { |
| 179 | case RsproPDUchoice_PR_connectBankRes: |
Harald Welte | 5ae0f31 | 2022-04-25 15:53:04 +0200 | [diff] [blame] | 180 | if (pdu->msg.choice.connectBankRes.identity.type != ComponentType_remsimServer) { |
| 181 | LOGPFSML(srvc->fi, LOGL_ERROR, "Server connection to a ComponentType(%ld) != RemsimServer? " |
| 182 | "Check your IP/Port configuration\n", |
| 183 | pdu->msg.choice.connectBankRes.identity.type); |
| 184 | osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_DISCONNECT, NULL); |
| 185 | return -1; |
| 186 | } |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 187 | /* Store 'identity' of server in srvc->peer_comp_id */ |
| 188 | rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectBankRes.identity); |
| 189 | osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu); |
| 190 | break; |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 191 | case RsproPDUchoice_PR_createMappingReq: |
| 192 | creq = &pdu->msg.choice.createMappingReq; |
Harald Welte | 2513d81 | 2019-04-01 21:03:02 +0200 | [diff] [blame] | 193 | if (creq->bank.bankId != g_bankd->srvc.bankd.bank_id) { |
Harald Welte | 94ba99b | 2019-03-27 22:42:11 +0100 | [diff] [blame] | 194 | LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Bank ID %lu " |
Harald Welte | 2513d81 | 2019-04-01 21:03:02 +0200 | [diff] [blame] | 195 | "(we are %u)\n", creq->bank.bankId, g_bankd->srvc.bankd.bank_id); |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 196 | resp = rspro_gen_CreateMappingRes(ResultCode_illegalBankId); |
Harald Welte | 2513d81 | 2019-04-01 21:03:02 +0200 | [diff] [blame] | 197 | } else if (creq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) { |
Harald Welte | 94ba99b | 2019-03-27 22:42:11 +0100 | [diff] [blame] | 198 | LOGPFSML(srvc->fi, LOGL_ERROR, "createMapping specifies invalid Slot Nr %lu " |
Harald Welte | 2513d81 | 2019-04-01 21:03:02 +0200 | [diff] [blame] | 199 | "(we have %u)\n", creq->bank.slotNr, g_bankd->srvc.bankd.num_slots); |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 200 | resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId); |
Harald Welte | 94ba99b | 2019-03-27 22:42:11 +0100 | [diff] [blame] | 201 | } else { |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 202 | rspro2bank_slot(&bs, &creq->bank); |
| 203 | rspro2client_slot(&cs, &creq->client); |
Harald Welte | e6fa46a | 2019-12-04 15:06:33 +0100 | [diff] [blame] | 204 | /* check if map exists */ |
| 205 | map = slotmap_by_bank(g_bankd->slotmaps, &bs); |
| 206 | if (map) { |
| 207 | if (client_slot_equals(&map->client, &cs)) { |
| 208 | LOGPFSML(srvc->fi, LOGL_ERROR, "ignoring identical slotmap\n"); |
| 209 | resp = rspro_gen_CreateMappingRes(ResultCode_ok); |
| 210 | goto send_resp; |
| 211 | } else { |
Harald Welte | 079c068 | 2022-04-27 09:26:52 +0200 | [diff] [blame] | 212 | LOGPFSML(srvc->fi, LOGL_NOTICE, "implicitly removing slotmap\n"); |
Harald Welte | e6fa46a | 2019-12-04 15:06:33 +0100 | [diff] [blame] | 213 | bankd_srvc_remove_mapping(map); |
| 214 | } |
| 215 | } |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 216 | /* Add a new mapping */ |
| 217 | map = slotmap_add(g_bankd->slotmaps, &bs, &cs); |
Harald Welte | 94ba99b | 2019-03-27 22:42:11 +0100 | [diff] [blame] | 218 | if (!map) { |
| 219 | LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n"); |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 220 | resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId); |
Harald Welte | 0fd77a5 | 2019-12-05 22:05:39 +0100 | [diff] [blame] | 221 | } else { |
| 222 | send_signal_to_worker(NULL, &cs, SIGMAPADD); |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 223 | resp = rspro_gen_CreateMappingRes(ResultCode_ok); |
Harald Welte | 0fd77a5 | 2019-12-05 22:05:39 +0100 | [diff] [blame] | 224 | } |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 225 | } |
Harald Welte | e6fa46a | 2019-12-04 15:06:33 +0100 | [diff] [blame] | 226 | send_resp: |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 227 | server_conn_send_rspro(srvc, resp); |
| 228 | break; |
| 229 | case RsproPDUchoice_PR_removeMappingReq: |
| 230 | rreq = &pdu->msg.choice.removeMappingReq; |
Harald Welte | 2513d81 | 2019-04-01 21:03:02 +0200 | [diff] [blame] | 231 | if (rreq->bank.bankId != g_bankd->srvc.bankd.bank_id) { |
Harald Welte | 94ba99b | 2019-03-27 22:42:11 +0100 | [diff] [blame] | 232 | LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Bank ID %lu " |
Harald Welte | 550b295 | 2019-12-04 21:22:54 +0100 | [diff] [blame] | 233 | "(we are %u)\n", rreq->bank.bankId, g_bankd->srvc.bankd.bank_id); |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 234 | resp = rspro_gen_RemoveMappingRes(ResultCode_illegalBankId); |
Harald Welte | 2513d81 | 2019-04-01 21:03:02 +0200 | [diff] [blame] | 235 | } else if (rreq->bank.slotNr >= g_bankd->srvc.bankd.num_slots) { |
Harald Welte | 94ba99b | 2019-03-27 22:42:11 +0100 | [diff] [blame] | 236 | LOGPFSML(srvc->fi, LOGL_ERROR, "removeMapping specifies invalid Slot Nr %lu " |
Harald Welte | 550b295 | 2019-12-04 21:22:54 +0100 | [diff] [blame] | 237 | "(we have %u)\n", rreq->bank.slotNr, g_bankd->srvc.bankd.num_slots); |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 238 | resp = rspro_gen_RemoveMappingRes(ResultCode_illegalSlotId); |
Harald Welte | 94ba99b | 2019-03-27 22:42:11 +0100 | [diff] [blame] | 239 | } else { |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 240 | rspro2bank_slot(&bs, &rreq->bank); |
| 241 | /* Remove a mapping */ |
| 242 | map = slotmap_by_bank(g_bankd->slotmaps, &bs); |
Harald Welte | 94ba99b | 2019-03-27 22:42:11 +0100 | [diff] [blame] | 243 | if (!map) { |
| 244 | 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] | 245 | resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap); |
Harald Welte | 94ba99b | 2019-03-27 22:42:11 +0100 | [diff] [blame] | 246 | } else { |
Harald Welte | d9fb939 | 2019-12-04 19:05:01 +0100 | [diff] [blame] | 247 | rspro2client_slot(&cs, &rreq->client); |
| 248 | if (!client_slot_equals(&map->client, &cs)) { |
Harald Welte | 079c068 | 2022-04-27 09:26:52 +0200 | [diff] [blame] | 249 | LOGPFSML(srvc->fi, LOGL_NOTICE, "ClientId in removeMappingReq != map\n"); |
Harald Welte | d9fb939 | 2019-12-04 19:05:01 +0100 | [diff] [blame] | 250 | resp = rspro_gen_RemoveMappingRes(ResultCode_unknownSlotmap); |
| 251 | } else { |
Harald Welte | 079c068 | 2022-04-27 09:26:52 +0200 | [diff] [blame] | 252 | LOGPFSML(srvc->fi, LOGL_INFO, "removing slotmap\n"); |
Harald Welte | d9fb939 | 2019-12-04 19:05:01 +0100 | [diff] [blame] | 253 | bankd_srvc_remove_mapping(map); |
| 254 | resp = rspro_gen_RemoveMappingRes(ResultCode_ok); |
| 255 | } |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 256 | } |
| 257 | } |
Harald Welte | 942f1ff | 2019-03-09 21:49:08 +0100 | [diff] [blame] | 258 | server_conn_send_rspro(srvc, resp); |
Harald Welte | 454f5e2 | 2019-03-09 21:38:34 +0100 | [diff] [blame] | 259 | break; |
Harald Welte | f34fb79 | 2019-12-04 19:51:32 +0100 | [diff] [blame] | 260 | case RsproPDUchoice_PR_resetStateReq: |
| 261 | /* delete all slotmaps */ |
| 262 | slotmap_del_all(g_bankd->slotmaps); |
| 263 | /* notify all workers about maps having disappeared */ |
| 264 | pthread_mutex_lock(&g_bankd->workers_mutex); |
| 265 | llist_for_each_entry(worker, &g_bankd->workers, list) { |
| 266 | pthread_kill(worker->thread, SIGMAPDEL); |
| 267 | } |
| 268 | pthread_mutex_unlock(&g_bankd->workers_mutex); |
| 269 | /* send response to server */ |
| 270 | resp = rspro_gen_ResetStateRes(ResultCode_ok); |
| 271 | server_conn_send_rspro(srvc, resp); |
| 272 | break; |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 273 | default: |
Harald Welte | eb971b5 | 2019-03-27 22:41:45 +0100 | [diff] [blame] | 274 | LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %u\n", |
| 275 | pdu->msg.present); |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 282 | static void printf_help() |
| 283 | { |
| 284 | printf( |
| 285 | " -h --help Print this help message\n" |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 286 | " -V --version Print the version of the program\n" |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 287 | " -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n" |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 288 | " -i --server-host A.B.C.D remsim-server IP address (default: 127.0.0.1)\n" |
| 289 | " -p --server-port <1-65535> remsim-server TCP port (default: 9998)\n" |
Harald Welte | 55f12b8 | 2022-01-16 14:34:12 +0100 | [diff] [blame] | 290 | " -b --bank-id <1-1023> Bank Identifier of this SIM bank (default: 1)\n" |
| 291 | " -n --num-slots <1-1023> Number of Slots in this SIM bank (default: 8)\n" |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 292 | " -I --bind-ip A.B.C.D Local IP address to bind for incoming client\n" |
| 293 | " connections (default: INADDR_ANY)\n" |
| 294 | " -P --bind-port <1-65535> Local TCP port to bind for incoming client\n" |
| 295 | " connectionss (default: 9999)\n" |
Harald Welte | 628672b | 2022-07-13 15:20:13 +0200 | [diff] [blame] | 296 | " -s --permit-shared-pcsc Permit SHARED access to PC/SC readers (default: exclusive)\n" |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 297 | ); |
| 298 | } |
| 299 | |
| 300 | static int g_bind_port = 9999; |
| 301 | static char *g_bind_ip = NULL; |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 302 | |
Harald Welte | f8c6eeb | 2021-12-08 20:33:00 +0100 | [diff] [blame] | 303 | static void handle_options(int argc, char **argv) |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 304 | { |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 305 | while (1) { |
| 306 | int option_index = 0, c; |
| 307 | static const struct option long_options[] = { |
| 308 | { "help", 0, 0, 'h' }, |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 309 | { "version", 0, 0, 'V' }, |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 310 | { "debug", 1, 0, 'd' }, |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 311 | { "server-host", 1, 0, 'i' }, |
| 312 | { "server-port", 1, 0, 'p' }, |
| 313 | { "bank-id", 1, 0, 'b' }, |
Harald Welte | 2513d81 | 2019-04-01 21:03:02 +0200 | [diff] [blame] | 314 | { "num-slots", 1, 0, 'n' }, |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 315 | { "component-name", 1, 0, 'N' }, |
| 316 | { "bind-ip", 1, 0, 'I' }, |
| 317 | { "bind-port", 1, 0, 'P' }, |
Harald Welte | 628672b | 2022-07-13 15:20:13 +0200 | [diff] [blame] | 318 | { "permit-shared-pcsc", 0, 0, 's' }, |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 319 | { 0, 0, 0, 0 } |
| 320 | }; |
| 321 | |
Harald Welte | 628672b | 2022-07-13 15:20:13 +0200 | [diff] [blame] | 322 | c = getopt_long(argc, argv, "hVd:i:o:b:n:N:I:P:s", long_options, &option_index); |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 323 | if (c == -1) |
| 324 | break; |
| 325 | |
| 326 | switch (c) { |
| 327 | case 'h': |
| 328 | printf_help(); |
| 329 | exit(0); |
| 330 | break; |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 331 | case 'V': |
| 332 | printf("osmo-remsim-bankd version %s\n", VERSION); |
| 333 | exit(0); |
| 334 | break; |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 335 | case 'd': |
| 336 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 337 | break; |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 338 | case 'i': |
| 339 | g_bankd->srvc.server_host = optarg; |
| 340 | break; |
| 341 | case 'p': |
| 342 | g_bankd->srvc.server_port = atoi(optarg); |
| 343 | break; |
| 344 | case 'b': |
Harald Welte | 2513d81 | 2019-04-01 21:03:02 +0200 | [diff] [blame] | 345 | g_bankd->srvc.bankd.bank_id = atoi(optarg); |
| 346 | break; |
| 347 | case 'n': |
| 348 | g_bankd->srvc.bankd.num_slots = atoi(optarg); |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 349 | break; |
| 350 | case 'N': |
| 351 | OSMO_STRLCPY_ARRAY(g_bankd->srvc.own_comp_id.name, optarg); |
| 352 | break; |
| 353 | case 'I': |
| 354 | g_bind_ip = optarg; |
| 355 | break; |
| 356 | case 'P': |
| 357 | g_bind_port = atoi(optarg); |
| 358 | break; |
Harald Welte | 628672b | 2022-07-13 15:20:13 +0200 | [diff] [blame] | 359 | case 's': |
| 360 | g_bankd->cfg.permit_shared_pcsc = true; |
| 361 | break; |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 362 | } |
| 363 | } |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 366 | int main(int argc, char **argv) |
| 367 | { |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 368 | struct rspro_server_conn *srvc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 369 | int i, rc; |
| 370 | |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 371 | g_bankd = talloc_zero(NULL, struct bankd); |
| 372 | OSMO_ASSERT(g_bankd); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 373 | |
Harald Welte | eea631b | 2022-05-03 15:14:19 +0200 | [diff] [blame] | 374 | if (gethostname(g_hostname, sizeof(g_hostname)) < 0) |
| 375 | OSMO_STRLCPY_ARRAY(g_hostname, "unknown"); |
| 376 | |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 377 | bankd_init(g_bankd); |
| 378 | |
| 379 | srvc = &g_bankd->srvc; |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 380 | srvc->server_host = "localhost"; |
| 381 | srvc->server_port = 9998; |
| 382 | srvc->handle_rx = bankd_srvc_handle_rx; |
| 383 | srvc->own_comp_id.type = ComponentType_remsimBankd; |
Harald Welte | eea631b | 2022-05-03 15:14:19 +0200 | [diff] [blame] | 384 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, g_hostname); |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 385 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-bankd"); |
| 386 | OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION); |
| 387 | |
| 388 | handle_options(argc, argv); |
| 389 | |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 390 | g_bankd->main = pthread_self(); |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 391 | signal(SIGMAPDEL, handle_sig_mapdel); |
Harald Welte | 0fd77a5 | 2019-12-05 22:05:39 +0100 | [diff] [blame] | 392 | signal(SIGMAPADD, handle_sig_mapadd); |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 393 | signal(SIGUSR1, handle_sig_usr1); |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 394 | |
Harald Welte | e89fecd | 2019-04-04 09:23:13 +0200 | [diff] [blame] | 395 | /* Np lock or mutex required for the pcsc_slot_names list, as this is only |
| 396 | * read once during bankd initialization, when the worker threads haven't |
| 397 | * started yet */ |
Harald Welte | e9e505c | 2022-05-03 11:58:01 +0200 | [diff] [blame] | 398 | rc = bankd_pcsc_read_slotnames(g_bankd, "bankd_pcsc_slots.csv"); |
| 399 | if (rc) |
| 400 | exit(1); |
Harald Welte | e89fecd | 2019-04-04 09:23:13 +0200 | [diff] [blame] | 401 | |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 402 | /* Connection towards remsim-server */ |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 403 | rc = server_conn_fsm_alloc(g_bankd, srvc); |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 404 | if (rc < 0) { |
| 405 | fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno)); |
| 406 | exit(1); |
| 407 | } |
Harald Welte | d2192e2 | 2019-11-07 18:10:57 +0100 | [diff] [blame] | 408 | osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL); |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 409 | |
| 410 | /* create listening socket for inbound client connections */ |
Harald Welte | 5bae20b | 2019-04-01 09:36:42 +0200 | [diff] [blame] | 411 | rc = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP, g_bind_ip, g_bind_port, OSMO_SOCK_F_BIND); |
Harald Welte | e9e505c | 2022-05-03 11:58:01 +0200 | [diff] [blame] | 412 | if (rc < 0) { |
| 413 | fprintf(stderr, "Unable to create TCP socket at %s:%d: %s\n", |
| 414 | g_bind_ip ? g_bind_ip : "INADDR_ANY", g_bind_port, strerror(errno)); |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 415 | exit(1); |
Harald Welte | e9e505c | 2022-05-03 11:58:01 +0200 | [diff] [blame] | 416 | } |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 417 | g_bankd->accept_fd = rc; |
Harald Welte | 12534e7 | 2018-08-15 23:37:29 +0200 | [diff] [blame] | 418 | |
Harald Welte | a0f3950 | 2019-03-09 20:59:34 +0100 | [diff] [blame] | 419 | /* create worker threads: One per reader/slot! */ |
Harald Welte | 2513d81 | 2019-04-01 21:03:02 +0200 | [diff] [blame] | 420 | for (i = 0; i < g_bankd->srvc.bankd.num_slots; i++) { |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 421 | struct bankd_worker *w; |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 422 | w = bankd_create_worker(g_bankd, i); |
Harald Welte | e9e505c | 2022-05-03 11:58:01 +0200 | [diff] [blame] | 423 | if (!w) { |
| 424 | fprintf(stderr, "Error creating bankd worker thread\n"); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 425 | exit(21); |
Harald Welte | e9e505c | 2022-05-03 11:58:01 +0200 | [diff] [blame] | 426 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | while (1) { |
| 430 | if (terminate) |
| 431 | break; |
Harald Welte | 707c85a | 2019-03-09 12:56:35 +0100 | [diff] [blame] | 432 | osmo_select_main(0); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Harald Welte | f4b16f1 | 2019-03-09 20:58:17 +0100 | [diff] [blame] | 435 | talloc_free(g_bankd); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 436 | exit(0); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | |
| 441 | /*********************************************************************** |
| 442 | * bankd worker thread |
| 443 | ***********************************************************************/ |
| 444 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 445 | static __thread struct bankd_worker *g_worker; |
| 446 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 447 | struct value_string worker_state_names[] = { |
| 448 | { BW_ST_INIT, "INIT" }, |
| 449 | { BW_ST_ACCEPTING, "ACCEPTING" }, |
| 450 | { BW_ST_CONN_WAIT_ID, "CONN_WAIT_ID" }, |
| 451 | { BW_ST_CONN_CLIENT, "CONN_CLIENT" }, |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 452 | { BW_ST_CONN_CLIENT_WAIT_MAP, "CONN_CLIENT_WAIT_MAP" }, |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 453 | { BW_ST_CONN_CLIENT_MAPPED, "CONN_CLIENT_MAPPED" }, |
| 454 | { BW_ST_CONN_CLIENT_MAPPED_CARD,"CONN_CLIENT_MAPPED_CARD" }, |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 455 | { BW_ST_CONN_CLIENT_UNMAPPED, "CONN_CLIENT_UNMAPPED" }, |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 456 | { 0, NULL } |
| 457 | }; |
| 458 | |
Harald Welte | 1f699b4 | 2019-03-28 13:41:08 +0100 | [diff] [blame] | 459 | static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu); |
| 460 | |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 461 | static void worker_set_state(struct bankd_worker *worker, enum bankd_worker_state new_state) |
| 462 | { |
| 463 | LOGW(worker, "Changing state to %s\n", get_value_string(worker_state_names, new_state)); |
| 464 | worker->state = new_state; |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 465 | worker->timeout = 0; |
| 466 | } |
| 467 | |
| 468 | static void worker_set_state_timeout(struct bankd_worker *worker, enum bankd_worker_state new_state, |
| 469 | unsigned int timeout_secs) |
| 470 | { |
| 471 | LOGW(worker, "Changing state to %s (timeout=%u)\n", |
| 472 | get_value_string(worker_state_names, new_state), timeout_secs); |
| 473 | worker->state = new_state; |
| 474 | worker->timeout = timeout_secs; |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 475 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 476 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 477 | /* signal handler for receiving SIGMAPDEL from main thread */ |
| 478 | static void handle_sig_mapdel(int sig) |
| 479 | { |
| 480 | LOGW(g_worker, "SIGMAPDEL received: Main thread informs us our map is gone\n"); |
| 481 | OSMO_ASSERT(sig == SIGMAPDEL); |
Harald Welte | a8d945e | 2019-12-04 22:24:18 +0100 | [diff] [blame] | 482 | if (g_worker->state >= BW_ST_CONN_CLIENT_MAPPED) { |
| 483 | g_worker->slot.bank_id = 0xffff; |
| 484 | g_worker->slot.slot_nr = 0xffff; |
| 485 | worker_set_state(g_worker, BW_ST_CONN_CLIENT_UNMAPPED); |
| 486 | } |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Harald Welte | 0fd77a5 | 2019-12-05 22:05:39 +0100 | [diff] [blame] | 489 | /* signal handler for receiving SIGMAPADD from main thread */ |
| 490 | static void handle_sig_mapadd(int sig) |
| 491 | { |
| 492 | LOGW(g_worker, "SIGMAPADD received\n"); |
| 493 | /* do nothing */ |
| 494 | } |
| 495 | |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 496 | static void handle_sig_usr1(int sig) |
| 497 | { |
| 498 | OSMO_ASSERT(sig == SIGUSR1); |
| 499 | |
| 500 | if (pthread_equal(g_bankd->main, pthread_self())) { |
| 501 | struct bankd_worker *worker; |
| 502 | /* main thread */ |
| 503 | fprintf(stderr, "=== Talloc Report of main thread:\n"); |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 504 | talloc_report_full(g_tall_ctx, stderr); |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 505 | |
| 506 | /* iterate over worker threads and ask them to dump their talloc state */ |
| 507 | pthread_mutex_lock(&g_bankd->workers_mutex); |
| 508 | llist_for_each_entry(worker, &g_bankd->workers, list) { |
| 509 | pthread_kill(worker->thread, SIGUSR1); |
| 510 | } |
| 511 | pthread_mutex_unlock(&g_bankd->workers_mutex); |
| 512 | } else { |
| 513 | /* worker thread */ |
| 514 | fprintf(stderr, "=== Talloc Report of %s\n", g_worker->name); |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 515 | talloc_report_full(g_worker->tall_ctx, stderr); |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 519 | static void worker_cleanup(void *arg) |
| 520 | { |
| 521 | struct bankd_worker *worker = (struct bankd_worker *) arg; |
| 522 | struct bankd *bankd = worker->bankd; |
| 523 | |
| 524 | /* FIXME: should we still do this? in the thread ?!? */ |
| 525 | pthread_mutex_lock(&bankd->workers_mutex); |
| 526 | llist_del(&worker->list); |
| 527 | talloc_free(worker); /* FIXME: is this safe? */ |
| 528 | pthread_mutex_unlock(&bankd->workers_mutex); |
| 529 | } |
| 530 | |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 531 | static int worker_open_card(struct bankd_worker *worker) |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 532 | { |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 533 | int rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 534 | |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 535 | OSMO_ASSERT(worker->state == BW_ST_CONN_CLIENT_MAPPED); |
| 536 | |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 537 | if (!worker->reader.name) { |
| 538 | /* resolve PC/SC reader name from slot_id -> name map */ |
| 539 | worker->reader.name = bankd_pcsc_get_slot_name(worker->bankd, &worker->slot); |
| 540 | if (!worker->reader.name) { |
| 541 | LOGW(worker, "No PC/SC reader name configured for %u/%u, fix your config\n", |
| 542 | worker->slot.bank_id, worker->slot.slot_nr); |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 543 | return -1; |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 544 | } |
| 545 | } |
Harald Welte | 45c948c | 2018-09-23 19:26:52 +0200 | [diff] [blame] | 546 | OSMO_ASSERT(worker->reader.name); |
| 547 | |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 548 | rc = worker->ops->open_card(worker); |
| 549 | if (rc < 0) |
| 550 | return rc; |
Harald Welte | 1f699b4 | 2019-03-28 13:41:08 +0100 | [diff] [blame] | 551 | |
Harald Welte | 57593f0 | 2018-09-23 19:30:31 +0200 | [diff] [blame] | 552 | worker_set_state(worker, BW_ST_CONN_CLIENT_MAPPED_CARD); |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 553 | /* FIXME: notify client about this state change */ |
Harald Welte | 57593f0 | 2018-09-23 19:30:31 +0200 | [diff] [blame] | 554 | |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 555 | return 0; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 556 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 557 | |
| 558 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 559 | 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] | 560 | { |
| 561 | struct ipaccess_head *hh; |
| 562 | uint16_t len; |
| 563 | int needed, rc; |
| 564 | |
| 565 | if (buf_size < sizeof(*hh)) |
| 566 | return -1; |
| 567 | |
| 568 | hh = (struct ipaccess_head *) buf; |
| 569 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 570 | /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR |
| 571 | * in case of a signal being received */ |
| 572 | |
| 573 | restart_hdr: |
| 574 | /* 1) blocking recv from the socket (IPA header) */ |
| 575 | rc = recv(worker->client.fd, buf, sizeof(*hh), 0); |
| 576 | if (rc == -1 && errno == EINTR) { |
| 577 | if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED) |
| 578 | return -23; |
| 579 | goto restart_hdr; |
| 580 | } else if (rc < 0) |
| 581 | return rc; |
| 582 | else if (rc < sizeof(*hh)) |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 583 | return -2; |
| 584 | |
| 585 | len = ntohs(hh->len); |
| 586 | needed = len; //- sizeof(*hh); |
| 587 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 588 | restart_body: |
| 589 | /* 2) blocking recv from the socket (payload) */ |
| 590 | rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0); |
| 591 | if (rc == -1 && errno == EINTR) { |
| 592 | if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED) |
| 593 | return -23; |
| 594 | goto restart_body; |
| 595 | } else if (rc < 0) |
| 596 | return rc; |
| 597 | else if (rc < needed) |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 598 | return -3; |
| 599 | |
| 600 | return len; |
| 601 | } |
| 602 | |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 603 | static int worker_send_rspro(struct bankd_worker *worker, RsproPDU_t *pdu) |
| 604 | { |
| 605 | struct msgb *msg = rspro_enc_msg(pdu); |
| 606 | int rc; |
| 607 | |
| 608 | if (!msg) { |
Harald Welte | 2eee450 | 2019-03-30 08:50:35 +0100 | [diff] [blame] | 609 | ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu); |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 610 | LOGW(worker, "error encoding RSPRO\n"); |
| 611 | return -1; |
| 612 | } |
| 613 | |
Harald Welte | fd47119 | 2018-09-24 14:51:14 +0200 | [diff] [blame] | 614 | msg->l2h = msg->data; |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 615 | /* prepend the header */ |
| 616 | ipa_prepend_header_ext(msg, IPAC_PROTO_EXT_RSPRO); |
Harald Welte | fd47119 | 2018-09-24 14:51:14 +0200 | [diff] [blame] | 617 | ipa_prepend_header(msg, IPAC_PROTO_OSMO); |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 618 | |
| 619 | /* actually send it through the socket */ |
| 620 | rc = write(worker->client.fd, msgb_data(msg), msgb_length(msg)); |
| 621 | if (rc == msgb_length(msg)) |
| 622 | rc = 0; |
| 623 | else { |
| 624 | LOGW(worker, "error during write: %d != %d\n", rc, msgb_length(msg)); |
| 625 | rc = -1; |
| 626 | } |
| 627 | |
| 628 | msgb_free(msg); |
| 629 | |
| 630 | return rc; |
| 631 | } |
| 632 | |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 633 | /* attempt to obtain slot-map */ |
| 634 | static int worker_try_slotmap(struct bankd_worker *worker) |
| 635 | { |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 636 | struct slot_mapping *slmap; |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 637 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 638 | slmap = slotmap_by_client(worker->bankd->slotmaps, &worker->client.clslot); |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 639 | if (!slmap) { |
| 640 | LOGW(worker, "No slotmap (yet) for client C(%u:%u)\n", |
| 641 | worker->client.clslot.client_id, worker->client.clslot.slot_nr); |
| 642 | /* check in 10s if the map has been installed meanwhile by main thread */ |
| 643 | worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_WAIT_MAP, 10); |
| 644 | return -1; |
| 645 | } else { |
| 646 | LOGW(worker, "slotmap found: C(%u:%u) -> B(%u:%u)\n", |
| 647 | slmap->client.client_id, slmap->client.slot_nr, |
| 648 | slmap->bank.bank_id, slmap->bank.slot_nr); |
| 649 | worker->slot = slmap->bank; |
| 650 | worker_set_state_timeout(worker, BW_ST_CONN_CLIENT_MAPPED, 10); |
| 651 | return worker_open_card(worker); |
| 652 | } |
| 653 | } |
| 654 | |
Harald Welte | 1f699b4 | 2019-03-28 13:41:08 +0100 | [diff] [blame] | 655 | /* inform the remote end (client) about the (new) ATR */ |
| 656 | static int worker_send_atr(struct bankd_worker *worker) |
| 657 | { |
| 658 | RsproPDU_t *set_atr; |
| 659 | set_atr = rspro_gen_SetAtrReq(worker->client.clslot.client_id, |
| 660 | worker->client.clslot.slot_nr, |
| 661 | worker->card.atr, worker->card.atr_len); |
| 662 | if (!set_atr) |
| 663 | return -1; |
| 664 | return worker_send_rspro(worker, set_atr); |
| 665 | } |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 666 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 667 | static int worker_handle_connectClientReq(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 668 | { |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 669 | const struct ComponentIdentity *cid = &pdu->msg.choice.connectClientReq.identity; |
Harald Welte | 458e01b | 2019-03-10 11:14:43 +0100 | [diff] [blame] | 670 | RsproPDU_t *resp = NULL; |
Harald Welte | 3e68987 | 2018-09-24 14:52:56 +0200 | [diff] [blame] | 671 | e_ResultCode res; |
Harald Welte | 458e01b | 2019-03-10 11:14:43 +0100 | [diff] [blame] | 672 | int rc; |
Harald Welte | af61473 | 2018-08-17 22:10:05 +0200 | [diff] [blame] | 673 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 674 | OSMO_ASSERT(pdu->msg.present == RsproPDUchoice_PR_connectClientReq); |
| 675 | |
Harald Welte | 9f7ca61 | 2021-12-08 15:29:28 +0100 | [diff] [blame] | 676 | LOGW(worker, "Rx RSPRO connectClientReq(T=%lu, N='%s', SW='%s', VER='%s')\n", |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 677 | cid->type, cid->name.buf, cid->software.buf, cid->swVersion.buf); |
| 678 | /* FIXME: store somewhere? */ |
| 679 | |
| 680 | if (worker->state != BW_ST_CONN_WAIT_ID) { |
| 681 | LOGW(worker, "Unexpected connectClientReq\n"); |
Harald Welte | 458e01b | 2019-03-10 11:14:43 +0100 | [diff] [blame] | 682 | rc = -102; |
| 683 | goto respond_and_err; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 686 | if (!pdu->msg.choice.connectClientReq.clientSlot) { |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 687 | LOGW(worker, "missing clientID, aborting\n"); |
Harald Welte | 458e01b | 2019-03-10 11:14:43 +0100 | [diff] [blame] | 688 | res = ResultCode_illegalClientId; |
| 689 | rc = -103; |
| 690 | goto respond_and_err; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 691 | } |
Harald Welte | 371d026 | 2018-08-16 15:23:58 +0200 | [diff] [blame] | 692 | worker->client.clslot.client_id = pdu->msg.choice.connectClientReq.clientSlot->clientId; |
| 693 | worker->client.clslot.slot_nr = pdu->msg.choice.connectClientReq.clientSlot->slotNr; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 694 | worker_set_state(worker, BW_ST_CONN_CLIENT); |
| 695 | |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 696 | if (worker_try_slotmap(worker) >= 0) |
| 697 | res = ResultCode_ok; |
| 698 | else |
Harald Welte | 3e68987 | 2018-09-24 14:52:56 +0200 | [diff] [blame] | 699 | res = ResultCode_cardNotPresent; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 700 | |
Harald Welte | 3e68987 | 2018-09-24 14:52:56 +0200 | [diff] [blame] | 701 | resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res); |
Harald Welte | 1f699b4 | 2019-03-28 13:41:08 +0100 | [diff] [blame] | 702 | rc = worker_send_rspro(worker, resp); |
| 703 | if (rc < 0) |
| 704 | return rc; |
| 705 | |
| 706 | if (res == ResultCode_ok) |
| 707 | rc = worker_send_atr(worker); |
| 708 | |
| 709 | return rc; |
Harald Welte | 458e01b | 2019-03-10 11:14:43 +0100 | [diff] [blame] | 710 | |
| 711 | respond_and_err: |
| 712 | if (res) { |
| 713 | resp = rspro_gen_ConnectClientRes(&worker->bankd->comp_id, res); |
| 714 | worker_send_rspro(worker, resp); |
| 715 | } |
| 716 | return rc; |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 719 | static int worker_handle_tpduModemToCard(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 720 | { |
| 721 | const struct TpduModemToCard *mdm2sim = &pdu->msg.choice.tpduModemToCard; |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 722 | uint8_t rx_buf[1024]; |
| 723 | DWORD rx_buf_len = sizeof(rx_buf); |
| 724 | RsproPDU_t *pdu_resp; |
Harald Welte | e1d3289 | 2019-03-27 20:47:42 +0100 | [diff] [blame] | 725 | struct client_slot clslot; |
| 726 | struct bank_slot bslot; |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 727 | int rc; |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 728 | |
Harald Welte | 9f7ca61 | 2021-12-08 15:29:28 +0100 | [diff] [blame] | 729 | LOGW(worker, "Rx RSPRO tpduModemToCard(%s)\n", |
| 730 | osmo_hexdump_nospc(mdm2sim->data.buf, mdm2sim->data.size)); |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 731 | |
| 732 | if (worker->state != BW_ST_CONN_CLIENT_MAPPED_CARD) { |
| 733 | LOGW(worker, "Unexpected tpduModemToCaard\n"); |
| 734 | return -104; |
| 735 | } |
| 736 | |
Harald Welte | e1d3289 | 2019-03-27 20:47:42 +0100 | [diff] [blame] | 737 | /* Validate that toBankSlot / fromClientSlot match our expectations */ |
| 738 | rspro2client_slot(&clslot, &mdm2sim->fromClientSlot); |
| 739 | rspro2bank_slot(&bslot, &mdm2sim->toBankSlot); |
| 740 | if (!bank_slot_equals(&worker->slot, &bslot)) { |
| 741 | LOGW(worker, "Unexpected BankSlot %u:%u in tpduModemToCard\n", |
| 742 | bslot.bank_id, bslot.slot_nr); |
| 743 | return -105; |
| 744 | } |
| 745 | if (!client_slot_equals(&worker->client.clslot, &clslot)) { |
| 746 | LOGW(worker, "Unexpected ClientSlot %u:%u in tpduModemToCard\n", |
| 747 | clslot.client_id, clslot.slot_nr); |
| 748 | return -106; |
| 749 | } |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 750 | |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 751 | rc = worker->ops->transceive(worker, mdm2sim->data.buf, mdm2sim->data.size, |
| 752 | rx_buf, &rx_buf_len); |
| 753 | if (rc < 0) |
| 754 | return rc; |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 755 | |
Harald Welte | 001884c | 2022-07-11 23:38:48 +0200 | [diff] [blame] | 756 | LOGW(worker, "Tx RSPRO tpduCardToModem(%s)\n", osmo_hexdump_nospc(rx_buf, rx_buf_len)); |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 757 | /* encode response PDU and send it */ |
| 758 | pdu_resp = rspro_gen_TpduCard2Modem(&mdm2sim->toBankSlot, &mdm2sim->fromClientSlot, |
| 759 | rx_buf, rx_buf_len); |
| 760 | worker_send_rspro(worker, pdu_resp); |
| 761 | |
| 762 | return 0; |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 763 | } |
| 764 | |
Harald Welte | bbd18bd | 2019-12-16 13:11:50 +0100 | [diff] [blame] | 765 | static int worker_handle_clientSlotStatusInd(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 766 | { |
| 767 | const struct ClientSlotStatusInd *cssi = &pdu->msg.choice.clientSlotStatusInd; |
| 768 | const struct SlotPhysStatus *sps = &cssi->slotPhysStatus; |
| 769 | int rc = 0; |
| 770 | |
Harald Welte | 9f7ca61 | 2021-12-08 15:29:28 +0100 | [diff] [blame] | 771 | LOGW(worker, "Rx RSPRO clientSlotStatusInd(RST=%s, VCC=%s, CLK=%s)\n", |
Harald Welte | bbd18bd | 2019-12-16 13:11:50 +0100 | [diff] [blame] | 772 | sps->resetActive ? "ACTIVE" : "INACTIVE", |
| 773 | sps->vccPresent ? *sps->vccPresent ? "PRESENT" : "ABSENT" : "NULL", |
| 774 | sps->clkActive ? *sps->clkActive ? "ACTIVE" : "INACTIVE" : "NULL"); |
| 775 | |
| 776 | /* perform cold or warm reset */ |
| 777 | if (sps->vccPresent && *sps->vccPresent == 0) |
| 778 | rc = worker->ops->reset_card(worker, true); |
| 779 | else if (sps->resetActive) |
| 780 | rc = worker->ops->reset_card(worker, false); |
| 781 | |
| 782 | return rc; |
| 783 | } |
| 784 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 785 | /* handle one incoming RSPRO message from a client inside a worker thread */ |
| 786 | static int worker_handle_rspro(struct bankd_worker *worker, const RsproPDU_t *pdu) |
| 787 | { |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 788 | int rc = -100; |
| 789 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 790 | switch (pdu->msg.present) { |
| 791 | case RsproPDUchoice_PR_connectClientReq: |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 792 | rc = worker_handle_connectClientReq(worker, pdu); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 793 | break; |
| 794 | case RsproPDUchoice_PR_tpduModemToCard: |
Harald Welte | 796a749 | 2018-09-23 19:31:28 +0200 | [diff] [blame] | 795 | rc = worker_handle_tpduModemToCard(worker, pdu); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 796 | break; |
| 797 | case RsproPDUchoice_PR_clientSlotStatusInd: |
Harald Welte | bbd18bd | 2019-12-16 13:11:50 +0100 | [diff] [blame] | 798 | rc = worker_handle_clientSlotStatusInd(worker, pdu); |
Harald Welte | 24a3e32 | 2019-03-31 13:08:30 +0200 | [diff] [blame] | 799 | rc = 0; |
| 800 | break; |
| 801 | case RsproPDUchoice_PR_setAtrRes: |
Harald Welte | 9f7ca61 | 2021-12-08 15:29:28 +0100 | [diff] [blame] | 802 | LOGW(worker, "Rx RSPRO %s\n", rspro_msgt_name(pdu)); |
Harald Welte | 24a3e32 | 2019-03-31 13:08:30 +0200 | [diff] [blame] | 803 | rc = 0; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 804 | break; |
| 805 | default: |
Harald Welte | 9f7ca61 | 2021-12-08 15:29:28 +0100 | [diff] [blame] | 806 | LOGW(worker, "Rx RSPRO %s (unhandled)\n", rspro_msgt_name(pdu)); |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 807 | rc = -101; |
| 808 | break; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Harald Welte | cce2aad | 2018-08-16 14:44:37 +0200 | [diff] [blame] | 811 | return rc; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 814 | static int wait_for_fd_or_timeout(int fd, unsigned int timeout_secs) |
| 815 | { |
| 816 | struct timeval tout = { timeout_secs, 0 }; |
| 817 | fd_set readset; |
| 818 | |
| 819 | FD_ZERO(&readset); |
| 820 | FD_SET(fd, &readset); |
| 821 | return select(fd + 1, &readset, NULL, NULL, timeout_secs ? &tout : NULL); |
| 822 | } |
| 823 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 824 | /* body of the main transceive loop */ |
| 825 | static int worker_transceive_loop(struct bankd_worker *worker) |
| 826 | { |
| 827 | struct ipaccess_head *hh; |
| 828 | struct ipaccess_head_ext *hh_ext; |
| 829 | uint8_t buf[65536]; /* maximum length expressed in 16bit length field */ |
| 830 | asn_dec_rval_t rval; |
| 831 | int data_len, rc; |
Harald Welte | 9ebbacc | 2018-09-24 17:43:39 +0200 | [diff] [blame] | 832 | RsproPDU_t *pdu = NULL; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 833 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 834 | restart_wait: |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 835 | rc = wait_for_fd_or_timeout(worker->client.fd, worker->timeout); |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 836 | if (rc == -1 && errno == EINTR) { |
| 837 | if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED) |
| 838 | return -23; |
Harald Welte | 0fd77a5 | 2019-12-05 22:05:39 +0100 | [diff] [blame] | 839 | else |
| 840 | worker_try_slotmap(worker); |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 841 | goto restart_wait; |
| 842 | } else if (rc < 0) |
| 843 | return rc; |
| 844 | else if (rc == 0) { |
Harald Welte | 694df83 | 2018-10-03 22:47:52 +0200 | [diff] [blame] | 845 | /* TIMEOUT case */ |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 846 | switch (worker->state) { |
| 847 | case BW_ST_CONN_CLIENT_WAIT_MAP: |
| 848 | /* re-check if mapping exists meanwhile? */ |
Harald Welte | 1f699b4 | 2019-03-28 13:41:08 +0100 | [diff] [blame] | 849 | rc = worker_try_slotmap(worker); |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 850 | break; |
| 851 | case BW_ST_CONN_CLIENT_MAPPED: |
| 852 | /* re-check if reader/card can be opened meanwhile? */ |
Harald Welte | 1f699b4 | 2019-03-28 13:41:08 +0100 | [diff] [blame] | 853 | rc = worker_open_card(worker); |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 854 | break; |
| 855 | default: |
| 856 | OSMO_ASSERT(0); |
| 857 | } |
Harald Welte | 1f699b4 | 2019-03-28 13:41:08 +0100 | [diff] [blame] | 858 | if (rc == 0) |
| 859 | worker_send_atr(worker); |
Harald Welte | 150d6d6 | 2018-10-03 23:07:47 +0200 | [diff] [blame] | 860 | /* 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] | 861 | return 0; |
| 862 | }; |
| 863 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 864 | /* 1) blocking read of entire IPA message from the socket */ |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 865 | rc = blocking_ipa_read(worker, buf, sizeof(buf)); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 866 | if (rc < 0) |
| 867 | return rc; |
| 868 | data_len = rc; |
| 869 | |
| 870 | hh = (struct ipaccess_head *) buf; |
Harald Welte | 5a3613a | 2018-10-11 12:56:21 +0200 | [diff] [blame] | 871 | if (hh->proto != IPAC_PROTO_OSMO && hh->proto != IPAC_PROTO_IPACCESS) { |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 872 | LOGW(worker, "Received unsupported IPA protocol != OSMO: 0x%02x\n", hh->proto); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 873 | return -4; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 874 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 875 | |
Harald Welte | 5a3613a | 2018-10-11 12:56:21 +0200 | [diff] [blame] | 876 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
Harald Welte | 19f881a | 2019-03-11 18:39:13 +0100 | [diff] [blame] | 877 | switch (hh->data[0]) { |
| 878 | case IPAC_MSGT_PING: |
| 879 | return ipa_ccm_send_pong(worker->client.fd); |
Harald Welte | 667d694 | 2019-12-01 22:34:21 +0100 | [diff] [blame] | 880 | case IPAC_MSGT_ID_ACK: |
| 881 | return ipa_ccm_send_id_ack(g_worker->client.fd); |
Harald Welte | 19f881a | 2019-03-11 18:39:13 +0100 | [diff] [blame] | 882 | default: |
| 883 | LOGW(worker, "IPA CCM 0x%02x not implemented yet\n", hh->data[0]); |
| 884 | break; |
| 885 | } |
Harald Welte | 5a3613a | 2018-10-11 12:56:21 +0200 | [diff] [blame] | 886 | return 0; |
| 887 | } |
| 888 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 889 | hh_ext = (struct ipaccess_head_ext *) buf + sizeof(*hh); |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 890 | if (data_len < sizeof(*hh_ext)) { |
| 891 | LOGW(worker, "Received short message\n"); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 892 | return -5; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 893 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 894 | data_len -= sizeof(*hh_ext); |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 895 | if (hh_ext->proto != IPAC_PROTO_EXT_RSPRO) { |
| 896 | 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] | 897 | return -6; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 898 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 899 | |
| 900 | /* 2) ASN1 BER decode of the message */ |
| 901 | 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] | 902 | if (rval.code != RC_OK) { |
| 903 | LOGW(worker, "Error during BER decode of RSPRO\n"); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 904 | return -7; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 905 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 906 | |
| 907 | /* 3) handling of the message, possibly resulting in PCSC commands */ |
| 908 | rc = worker_handle_rspro(worker, pdu); |
| 909 | ASN_STRUCT_FREE(asn_DEF_RsproPDU, pdu); |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 910 | if (rc < 0) { |
| 911 | LOGW(worker, "Error handling RSPRO\n"); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 912 | return rc; |
Harald Welte | e1176cf | 2018-09-24 14:54:58 +0200 | [diff] [blame] | 913 | } |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 914 | |
| 915 | /* everything OK if we reach here */ |
| 916 | return 0; |
| 917 | } |
| 918 | |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 919 | /* obtain an ascii representation of the client IP/port */ |
| 920 | static int worker_client_addrstr(char *out, unsigned int outlen, const struct bankd_worker *worker) |
| 921 | { |
| 922 | char hostbuf[32], portbuf[32]; |
| 923 | int rc; |
| 924 | |
| 925 | rc = getnameinfo((const struct sockaddr *)&worker->client.peer_addr, |
| 926 | worker->client.peer_addr_len, hostbuf, sizeof(hostbuf), |
| 927 | portbuf, sizeof(portbuf), NI_NUMERICHOST | NI_NUMERICSERV); |
| 928 | if (rc != 0) { |
| 929 | out[0] = '\0'; |
| 930 | return -1; |
| 931 | } |
| 932 | snprintf(out, outlen, "%s:%s", hostbuf, portbuf); |
| 933 | return 0; |
| 934 | } |
| 935 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 936 | /* worker thread main function */ |
| 937 | static void *worker_main(void *arg) |
| 938 | { |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 939 | void *top_ctx; |
| 940 | int rc; |
Harald Welte | 31c9eca | 2018-10-03 21:03:34 +0200 | [diff] [blame] | 941 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 942 | g_worker = (struct bankd_worker *) arg; |
| 943 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 944 | worker_set_state(g_worker, BW_ST_INIT); |
Harald Welte | 8d85829 | 2018-08-15 23:36:46 +0200 | [diff] [blame] | 945 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 946 | /* not permitted in multithreaded environment */ |
| 947 | talloc_disable_null_tracking(); |
Harald Welte | 2507597 | 2019-03-11 17:33:17 +0100 | [diff] [blame] | 948 | g_worker->tall_ctx = talloc_named_const(NULL, 0, "top"); |
| 949 | talloc_asn1_ctx = talloc_named_const(g_worker->tall_ctx, 0, "asn1"); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 950 | |
Harald Welte | 286a2be | 2019-03-11 17:36:58 +0100 | [diff] [blame] | 951 | /* set the thread name */ |
| 952 | g_worker->name = talloc_asprintf(g_worker->tall_ctx, "bankd-worker(%u)", g_worker->num); |
| 953 | pthread_setname_np(pthread_self(), g_worker->name); |
| 954 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 955 | /* push cleanup helper */ |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 956 | pthread_cleanup_push(&worker_cleanup, g_worker); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 957 | |
Harald Welte | 602b6f7 | 2019-12-04 21:51:02 +0100 | [diff] [blame] | 958 | g_worker->slot.bank_id = 0xffff; |
| 959 | g_worker->slot.slot_nr = 0xffff; |
| 960 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 961 | /* we continuously perform the same loop here, recycling the worker thread |
| 962 | * once the client connection is gone or we have some trouble with the card/reader */ |
| 963 | while (1) { |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 964 | char buf[128]; |
| 965 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 966 | g_worker->client.peer_addr_len = sizeof(g_worker->client.peer_addr); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 967 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 968 | worker_set_state(g_worker, BW_ST_ACCEPTING); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 969 | /* first wait for an incoming TCP connection */ |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 970 | rc = accept(g_worker->bankd->accept_fd, (struct sockaddr *) &g_worker->client.peer_addr, |
| 971 | &g_worker->client.peer_addr_len); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 972 | if (rc < 0) { |
| 973 | continue; |
| 974 | } |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 975 | g_worker->client.fd = rc; |
| 976 | worker_client_addrstr(buf, sizeof(buf), g_worker); |
| 977 | LOGW(g_worker, "Accepted connection from %s\n", buf); |
| 978 | worker_set_state(g_worker, BW_ST_CONN_WAIT_ID); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 979 | |
| 980 | /* run the main worker transceive loop body until there was some error */ |
| 981 | while (1) { |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 982 | rc = worker_transceive_loop(g_worker); |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 983 | if (rc < 0) |
| 984 | break; |
Harald Welte | 653d6a0 | 2019-03-11 18:38:44 +0100 | [diff] [blame] | 985 | if (g_worker->state == BW_ST_CONN_CLIENT_UNMAPPED) |
| 986 | break; |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 987 | } |
| 988 | |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 989 | LOGW(g_worker, "Error %d occurred: Cleaning up state\n", rc); |
Harald Welte | d6dfb8c | 2018-08-16 14:46:53 +0200 | [diff] [blame] | 990 | |
Harald Welte | 77911b0 | 2018-08-14 23:47:30 +0200 | [diff] [blame] | 991 | /* clean-up: reset to sane state */ |
Harald Welte | 1f699b4 | 2019-03-28 13:41:08 +0100 | [diff] [blame] | 992 | memset(&g_worker->card, 0, sizeof(g_worker->card)); |
Harald Welte | 297d72e | 2019-03-28 18:42:35 +0100 | [diff] [blame] | 993 | g_worker->ops->cleanup(g_worker); |
Harald Welte | 00a9673 | 2019-03-11 17:18:02 +0100 | [diff] [blame] | 994 | if (g_worker->reader.name) |
| 995 | g_worker->reader.name = NULL; |
| 996 | if (g_worker->client.fd >= 0) |
| 997 | close(g_worker->client.fd); |
| 998 | memset(&g_worker->client.peer_addr, 0, sizeof(g_worker->client.peer_addr)); |
| 999 | g_worker->client.fd = -1; |
| 1000 | 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] | 1001 | } |
| 1002 | |
| 1003 | pthread_cleanup_pop(1); |
| 1004 | talloc_free(top_ctx); |
| 1005 | pthread_exit(NULL); |
| 1006 | } |