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