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