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