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