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