blob: 2c321c74426a8fc12552f58762f46bacaaa5bf70 [file] [log] [blame]
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +01001/* nacc_fsm.c
2 *
3 * Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
4 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010015 */
16
17#include <unistd.h>
18
19#include <talloc.h>
20
21#include <osmocom/core/rate_ctr.h>
22#include <osmocom/ctrl/control_cmd.h>
23#include <osmocom/ctrl/control_if.h>
24
25#include <osmocom/gsm/gsm48.h>
26#include <osmocom/gprs/gprs_bssgp.h>
27#include <osmocom/gprs/gprs_bssgp_rim.h>
28
29#include <nacc_fsm.h>
30#include <gprs_rlcmac.h>
31#include <gprs_debug.h>
32#include <gprs_ms.h>
33#include <encoding.h>
34#include <bts.h>
35#include <neigh_cache.h>
36
37#define X(s) (1 << (s))
38
Pau Espin Pedrol069a6372021-02-10 17:33:13 +010039/* Infer CTRL id (seqnum) for a given tgt arfcn+bsic (bsic range: 0-63) */
40#define arfcn_bsic_2_ctrl_id(arfcn, bsic) ((arfcn) * 100 + (bsic))
41
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010042static const struct osmo_tdef_state_timeout nacc_fsm_timeouts[32] = {
43 [NACC_ST_INITIAL] = {},
44 [NACC_ST_WAIT_RESOLVE_RAC_CI] = { .T = PCU_TDEF_NEIGH_RESOLVE_TO },
45 [NACC_ST_WAIT_REQUEST_SI] = { .T = PCU_TDEF_SI_RESOLVE_TO },
46 [NACC_ST_TX_NEIGHBOUR_DATA] = {},
47 [NACC_ST_TX_CELL_CHG_CONTINUE] = {},
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +010048 [NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK] = {}, /* Timeout through event controlled by tbf::poll_timeout() */
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010049 [NACC_ST_DONE] = {},
50};
51
Pau Espin Pedrol628d8812021-07-29 13:46:34 +020052/* Transition to a state, using the T timer defined in nacc_fsm_timeouts.
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010053 * The actual timeout value is in turn obtained from conn->T_defs.
54 * Assumes local variable fi exists. */
55
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010056#define nacc_fsm_state_chg(fi, NEXT_STATE) \
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010057 osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, \
58 nacc_fsm_timeouts, \
59 ((struct nacc_fsm_ctx*)(fi->priv))->ms->bts->pcu->T_defs, \
60 -1)
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010061
62const struct value_string nacc_fsm_event_names[] = {
63 { NACC_EV_RX_CELL_CHG_NOTIFICATION, "RX_CELL_CHG_NOTIFICATION" },
64 { NACC_EV_RX_RAC_CI, "RX_RAC_CI" },
65 { NACC_EV_RX_SI, "RX_SI" },
66 { NACC_EV_CREATE_RLCMAC_MSG, "CREATE_RLCMAC_MSG" },
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +010067 { NACC_EV_RX_CELL_CHG_CONTINUE_ACK, "RX_CELL_CHG_CONTINUE_ACK"},
68 { NACC_EV_TIMEOUT_CELL_CHG_CONTINUE, "TIMEOUT_CELL_CHG_CONTINUE" },
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +010069 { 0, NULL }
70};
71
72/* TS 44 060 11.2.9e Packet Neighbour Cell Data */
73static struct msgb *create_packet_neighbour_cell_data(struct nacc_fsm_ctx *ctx,
74 const struct gprs_rlcmac_tbf *tbf,
75 bool *all_si_info_sent)
76{
77 struct msgb *msg;
78 int rc;
79 RlcMacDownlink_t *mac_control_block;
80 struct GprsMs *ms = tbf_ms(tbf);
81 OSMO_ASSERT(tbf_is_tfi_assigned(tbf));
82 uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF;
83 uint8_t tfi = tbf_tfi(tbf);
84 uint8_t container_id = 0;
85 PNCDContainer_t container;
86 size_t max_len, len_to_write;
87 uint8_t *cont_buf;
88 uint8_t si_type = ctx->si_info.type_psi ? 0x01 : 0x0;
89
90 memset(&container, 0, sizeof(container));
91 if (ctx->container_idx == 0) {
92 container.UnionType = 1; /* with ID */
93 container.u.PNCD_Container_With_ID.ARFCN = ctx->neigh_key.tgt_arfcn;
94 container.u.PNCD_Container_With_ID.BSIC = ctx->neigh_key.tgt_bsic;
95 cont_buf = &container.u.PNCD_Container_With_ID.CONTAINER[0];
96 max_len = sizeof(container.u.PNCD_Container_With_ID.CONTAINER) - 1;
97 } else {
98 container.UnionType = 0; /* without ID */
99 cont_buf = &container.u.PNCD_Container_Without_ID.CONTAINER[0];
100 max_len = sizeof(container.u.PNCD_Container_Without_ID.CONTAINER) - 1;
101 }
102
103 len_to_write = ctx->si_info.si_len - ctx->si_info_bytes_sent;
104
105 if (len_to_write == 0) {
106 /* We sent all info on last message filing it exactly, we now send a zeroed one to finish */
107 *all_si_info_sent = true;
108 *cont_buf = (si_type << 5) | 0x00;
109 } else if (len_to_write >= max_len) {
110 /* We fill the rlcmac block, we'll need more messages */
111 *all_si_info_sent = false;
112 *cont_buf = (si_type << 5) | 0x1F;
113 memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], max_len);
114 ctx->si_info_bytes_sent += max_len;
115 } else {
116 /* Last block, we don't fill it exactly */
117 *all_si_info_sent = true;
118 *cont_buf = (si_type << 5) | (len_to_write & 0x1F);
119 memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], len_to_write);
120 ctx->si_info_bytes_sent += len_to_write;
121 }
122
123 msg = msgb_alloc(GSM_MACBLOCK_LEN, "neighbour_cell_data");
124 if (!msg)
125 return NULL;
126
127 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
128 struct bitvec bv = {
129 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
130 .data_len = GSM_MACBLOCK_LEN,
131 };
132 bitvec_unhex(&bv, DUMMY_VEC);
133
134 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t);
135
136 write_packet_neighbour_cell_data(mac_control_block,
137 tfi_is_dl, tfi, container_id,
138 ctx->container_idx, &container);
139 LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Neighbour Cell Data +++++++++++++++++++++++++\n");
140 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
141 if (rc < 0) {
142 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Neighbour Cell Data failed (%d)\n", rc);
143 goto free_ret;
144 }
145 LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Neighbour Cell Data -------------------------\n");
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200146 rate_ctr_inc(rate_ctr_group_get_ctr(bts_rate_counters(ms->bts), CTR_PKT_NEIGH_CELL_DATA));
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100147 talloc_free(mac_control_block);
148
149 ctx->container_idx++;
150
151 return msg;
152
153free_ret:
154 talloc_free(mac_control_block);
155 msgb_free(msg);
156 return NULL;
157}
158
159/* TS 44 060 11.2.2a Packet Cell Change Continue */
160static struct msgb *create_packet_cell_chg_continue(const struct nacc_fsm_ctx *ctx,
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100161 const struct nacc_ev_create_rlcmac_msg_ctx *data,
162 uint32_t *new_poll_fn)
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100163{
164 struct msgb *msg;
165 int rc;
166 RlcMacDownlink_t *mac_control_block;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100167 struct gprs_rlcmac_tbf *tbf = data->tbf;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100168 struct GprsMs *ms = tbf_ms(tbf);
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100169 unsigned int rrbp;
170
171 rc = tbf_check_polling(tbf, data->fn, data->ts, new_poll_fn, &rrbp);
172 if (rc < 0) {
173 LOGP(DTBF, LOGL_ERROR, "Failed registering poll for Pkt Cell Chg Continue (%d)\n", rc);
174 return NULL;
175 }
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100176
177 msg = msgb_alloc(GSM_MACBLOCK_LEN, "pkt_cell_chg_continue");
178 if (!msg)
179 return NULL;
180
181 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
182 struct bitvec bv = {
183 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
184 .data_len = GSM_MACBLOCK_LEN,
185 };
186 bitvec_unhex(&bv, DUMMY_VEC);
187
188 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t);
189
190 OSMO_ASSERT(tbf_is_tfi_assigned(tbf));
191 uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF;
192 uint8_t tfi = tbf_tfi(tbf);
193 uint8_t container_id = 0;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100194 write_packet_cell_change_continue(mac_control_block, 1, rrbp, tfi_is_dl, tfi, true,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100195 ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic, container_id);
196 LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Cell Change Continue +++++++++++++++++++++++++\n");
197 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
198 if (rc < 0) {
199 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Cell Change Continue failed (%d)\n", rc);
200 goto free_ret;
201 }
202 LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Cell Change Continue -------------------------\n");
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200203 rate_ctr_inc(rate_ctr_group_get_ctr(bts_rate_counters(ms->bts), CTR_PKT_CELL_CHG_CONTINUE));
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100204 talloc_free(mac_control_block);
Pau Espin Pedrol86580e12021-03-29 18:15:30 +0200205 tbf_set_polling(tbf, *new_poll_fn, data->ts, PDCH_ULC_POLL_CELL_CHG_CONTINUE);
Pau Espin Pedrola161bf42021-07-30 13:42:06 +0200206 LOGPTBFDL(tbf, LOGL_DEBUG, "Scheduled 'Packet Cell Change Continue' polling on PACCH (FN=%d, TS=%d)\n",
207 *new_poll_fn, data->ts);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100208 return msg;
209
210free_ret:
211 talloc_free(mac_control_block);
212 msgb_free(msg);
213 return NULL;
214}
215
216static int fill_rim_ran_info_req(const struct nacc_fsm_ctx *ctx, struct bssgp_ran_information_pdu *pdu)
217{
218 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
219
220 *pdu = (struct bssgp_ran_information_pdu){
221 .routing_info_dest = {
222 .discr = BSSGP_RIM_ROUTING_INFO_GERAN,
223 .geran = {
224 .raid = {
225 .mcc = ctx->cgi_ps.rai.lac.plmn.mcc,
226 .mnc = ctx->cgi_ps.rai.lac.plmn.mnc,
227 .mnc_3_digits = ctx->cgi_ps.rai.lac.plmn.mnc_3_digits,
228 .lac = ctx->cgi_ps.rai.lac.lac,
229 .rac = ctx->cgi_ps.rai.rac,
230 },
231 .cid = ctx->cgi_ps.cell_identity,
232 },
233 },
234 .routing_info_src = {
235 .discr = BSSGP_RIM_ROUTING_INFO_GERAN,
236 .geran = {
237 .raid = {
238 .mcc = bts->cgi_ps.rai.lac.plmn.mcc,
239 .mnc = bts->cgi_ps.rai.lac.plmn.mnc,
240 .mnc_3_digits = bts->cgi_ps.rai.lac.plmn.mnc_3_digits,
241 .lac = bts->cgi_ps.rai.lac.lac,
242 .rac = bts->cgi_ps.rai.rac,
243 },
244 .cid = bts->cgi_ps.cell_identity,
245 },
246 },
247 .rim_cont_iei = BSSGP_IE_RI_REQ_RIM_CONTAINER,
248 .decoded_present = true,
249 .decoded = {
250 .req_rim_cont = {
251 .app_id = BSSGP_RAN_INF_APP_ID_NACC,
252 .seq_num = 1,
253 .pdu_ind = {
254 .ack_requested = 0,
255 .pdu_type_ext = RIM_PDU_TYPE_SING_REP,
256 },
257 .prot_ver = 1,
258 .son_trans_app_id = NULL,
259 .son_trans_app_id_len = 0,
260 .u = {
261 .app_cont_nacc = {
262 .reprt_cell = ctx->cgi_ps,
263 },
264 },
265 },
266 },
267 };
268
269 return 0;
270}
271
Pau Espin Pedrol0c10b3c2021-02-10 17:12:02 +0100272static int fill_neigh_key_from_bts_pkt_cell_chg_not(struct neigh_cache_entry_key *neigh_key,
273 const struct gprs_rlcmac_bts *bts,
274 const Packet_Cell_Change_Notification_t *notif)
275{
276 switch (notif->Target_Cell.UnionType) {
277 case 0: /* GSM */
278 neigh_key->local_lac = bts->cgi_ps.rai.lac.lac;
279 neigh_key->local_ci = bts->cgi_ps.cell_identity;
280 neigh_key->tgt_arfcn = notif->Target_Cell.u.Target_Cell_GSM_Notif.ARFCN;
281 neigh_key->tgt_bsic = notif->Target_Cell.u.Target_Cell_GSM_Notif.BSIC;
282 return 0;
283 default:
284 return -ENOTSUP;
285 }
286}
287
Pau Espin Pedrol44768f22021-02-02 13:11:30 +0100288#define SI_HDR_LEN 2
289static void bts_fill_si_cache_value(const struct gprs_rlcmac_bts *bts, struct si_cache_value *val)
290{
291 val->type_psi = false;
292 val->si_len = 0;
293 if (bts->si1_is_set) {
294 osmo_static_assert(sizeof(bts->si1) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si1_header_size);
295 memcpy(&val->si_buf[val->si_len], bts->si1 + SI_HDR_LEN, BSSGP_RIM_SI_LEN);
296 val->si_len += BSSGP_RIM_SI_LEN;
297 }
298 if (bts->si3_is_set) {
299 osmo_static_assert(sizeof(bts->si3) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si3_header_size);
300 memcpy(&val->si_buf[val->si_len], bts->si3 + SI_HDR_LEN, BSSGP_RIM_SI_LEN);
301 val->si_len += BSSGP_RIM_SI_LEN;
302 }
303 if (bts->si13_is_set) {
304 osmo_static_assert(sizeof(bts->si13) - SI_HDR_LEN == BSSGP_RIM_SI_LEN, _si13_header_size);
305 memcpy(&val->si_buf[val->si_len], bts->si13 + SI_HDR_LEN, BSSGP_RIM_SI_LEN);
306 val->si_len += BSSGP_RIM_SI_LEN;
307 }
308}
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100309
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100310/* Called on event NACC_EV_RX_CELL_CHG_NOTIFICATION on states after
311 * WAIT_RESOLVE_RAC_CI. Ignore duplicate messages, transition back if target
312 * cell changed.
313 */
314static void handle_retrans_pkt_cell_chg_notif(struct nacc_fsm_ctx *ctx, const Packet_Cell_Change_Notification_t *notif)
315{
316 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
317 struct neigh_cache_entry_key neigh_key;
318
319 if (fill_neigh_key_from_bts_pkt_cell_chg_not(&neigh_key, bts, notif) < 0) {
320 LOGPFSML(ctx->fi, LOGL_NOTICE, "TargetCell type=0x%x not supported\n",
321 notif->Target_Cell.UnionType);
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100322 if (ctx->fi->state != NACC_ST_TX_CELL_CHG_CONTINUE)
323 nacc_fsm_state_chg(ctx->fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100324 return;
325 }
326 /* If tgt cell changed, restart resolving it */
327 if (!neigh_cache_entry_key_eq(&ctx->neigh_key, &neigh_key)) {
328 ctx->neigh_key = neigh_key;
329 nacc_fsm_state_chg(ctx->fi, NACC_ST_WAIT_RESOLVE_RAC_CI);
330 }
331 /* else: ignore it, it's a dup, carry on what we were doing */
332}
333
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100334////////////////
335// FSM states //
336////////////////
337
338static void st_initial(struct osmo_fsm_inst *fi, uint32_t event, void *data)
339{
340 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
341 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
342 Packet_Cell_Change_Notification_t *notif;
343
344 switch (event) {
345 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
346 notif = (Packet_Cell_Change_Notification_t *)data;
Pau Espin Pedrol0c10b3c2021-02-10 17:12:02 +0100347 if (fill_neigh_key_from_bts_pkt_cell_chg_not(&ctx->neigh_key, bts, notif) < 0) {
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100348 LOGPFSML(fi, LOGL_NOTICE, "TargetCell type=0x%x not supported\n",
349 notif->Target_Cell.UnionType);
350 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
Pau Espin Pedrol0c10b3c2021-02-10 17:12:02 +0100351 } else {
352 nacc_fsm_state_chg(fi, NACC_ST_WAIT_RESOLVE_RAC_CI);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100353 }
354 break;
355 default:
356 OSMO_ASSERT(0);
357 }
358}
359
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200360static int send_neigh_addr_req_ctrl_iface(struct nacc_fsm_ctx *ctx)
361{
362 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
363 struct gprs_pcu *pcu = bts->pcu;
364 struct ctrl_cmd *cmd = NULL;
365 int rc;
366
367 /* We may have changed to this state previously (eg: we are handling
368 * another Pkt cell Change Notify with different target). Avoid
369 * re-creating the socket in that case. */
370 if (ctx->neigh_ctrl_conn->write_queue.bfd.fd == -1) {
371 rc = osmo_sock_init2_ofd(&ctx->neigh_ctrl_conn->write_queue.bfd,
372 AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP,
373 NULL, 0, pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port,
374 OSMO_SOCK_F_CONNECT);
375 if (rc < 0) {
376 LOGPFSML(ctx->fi, LOGL_ERROR,
377 "Failed to establish CTRL (neighbor resolution) connection to BSC r=%s:%u\n\n",
378 pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port);
379 goto err_term;
380 }
381 }
382
383 cmd = ctrl_cmd_create(ctx, CTRL_TYPE_GET);
384 if (!cmd) {
385 LOGPFSML(ctx->fi, LOGL_ERROR, "CTRL msg creation failed\n");
386 goto err_term;
387 }
388
389 cmd->id = talloc_asprintf(cmd, "%u", arfcn_bsic_2_ctrl_id(ctx->neigh_key.tgt_arfcn,
390 ctx->neigh_key.tgt_bsic));
391 cmd->variable = talloc_asprintf(cmd, "neighbor_resolve_cgi_ps_from_lac_ci.%d.%d.%d.%d",
392 ctx->neigh_key.local_lac, ctx->neigh_key.local_ci,
393 ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic);
394 rc = ctrl_cmd_send(&ctx->neigh_ctrl_conn->write_queue, cmd);
395 if (rc) {
396 LOGPFSML(ctx->fi, LOGL_ERROR, "CTRL msg sent failed: %d\n", rc);
397 goto err_term;
398 }
399
400 talloc_free(cmd);
401 return 0;
402
403err_term:
404 talloc_free(cmd);
405 return -1;
406}
407
408static int send_neigh_addr_req(struct nacc_fsm_ctx *ctx)
409{
410 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
411
412 /* If PCU was configured to use the old CTRL interface, use it: */
413 if (ctx->neigh_ctrl_conn)
414 return send_neigh_addr_req_ctrl_iface(ctx);
415
416 /* Otherwise, by default the new PCUIF over IPA Abis multiplex proto should be used: */
417 return pcu_tx_neigh_addr_res_req(bts, &ctx->neigh_key);
418}
419
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100420static void st_wait_resolve_rac_ci_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
421{
422 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
423 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
424 struct gprs_pcu *pcu = bts->pcu;
425 const struct osmo_cell_global_id_ps *cgi_ps;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100426
427 /* First try to find the value in the cache */
428 cgi_ps = neigh_cache_lookup_value(pcu->neigh_cache, &ctx->neigh_key);
429 if (cgi_ps) {
430 ctx->cgi_ps = *cgi_ps;
431 nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI);
432 return;
433 }
434
435 /* CGI-PS not in cache, resolve it using BSC Neighbor Resolution CTRL interface */
436
437 LOGPFSML(fi, LOGL_DEBUG, "No CGI-PS found in cache, resolving " NEIGH_CACHE_ENTRY_KEY_FMT "...\n",
438 NEIGH_CACHE_ENTRY_KEY_ARGS(&ctx->neigh_key));
439
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200440 if (send_neigh_addr_req(ctx) < 0)
441 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100442}
443
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100444static void st_wait_resolve_rac_ci(struct osmo_fsm_inst *fi, uint32_t event, void *data)
445{
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100446 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100447 const Packet_Cell_Change_Notification_t *notif;
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100448
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100449 switch (event) {
450 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100451 notif = (const Packet_Cell_Change_Notification_t *)data;
452 handle_retrans_pkt_cell_chg_notif(ctx, notif);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100453 break;
454 case NACC_EV_RX_RAC_CI:
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200455 /* data is NULL upon failure */
456 if (data) {
457 ctx->cgi_ps = *(struct osmo_cell_global_id_ps *)data;
458 nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI);
459 } else {
460 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
461 }
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100462 break;
463 default:
464 OSMO_ASSERT(0);
465 }
466}
467
468/* At this point, we expect correct tgt cell info to be already in ctx->cgi_ps */
469static void st_wait_request_si_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
470{
471 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
472 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
473 struct gprs_pcu *pcu = bts->pcu;
474 struct bssgp_ran_information_pdu pdu;
475 const struct si_cache_value *si;
Pau Espin Pedrol44768f22021-02-02 13:11:30 +0100476 struct gprs_rlcmac_bts *bts_i;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100477 int rc;
478
Pau Espin Pedrol44768f22021-02-02 13:11:30 +0100479 /* First check if the CGI-PS addresses a cell managed by this PCU. If
480 * that's the case, we already have the info and there's no need to go
481 * the RIM way since we'd end up to this same PCU on the other end anyway.
482 */
483 llist_for_each_entry(bts_i, &the_pcu->bts_list, list) {
484 if (bts_i == bts) /* Makes no sense targeting the same cell */
485 continue;
486 if (osmo_cgi_ps_cmp(&ctx->cgi_ps, &bts_i->cgi_ps) != 0)
487 continue;
488
489 LOGPFSML(fi, LOGL_DEBUG, "neighbor CGI-PS %s addresses local BTS %d\n",
490 osmo_cgi_ps_name(&ctx->cgi_ps), bts_i->nr);
491 bts_fill_si_cache_value(bts, &ctx->si_info);
492 /* Tell the PCU scheduler we are ready to go, from here one we
493 * are polled/driven by the scheduler */
494 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
495 return;
496 }
497
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100498 /* First check if we have SI info for the target cell in cache */
499 si = si_cache_lookup_value(pcu->si_cache, &ctx->cgi_ps);
500 if (si) {
501 /* Copy info since cache can be deleted at any point */
502 memcpy(&ctx->si_info, si, sizeof(ctx->si_info));
503 /* Tell the PCU scheduler we are ready to go, from here one we
504 * are polled/driven by the scheduler */
505 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
506 return;
507 }
508
509 /* SI info not in cache, resolve it using RIM procedure against SGSN */
510 if (fill_rim_ran_info_req(ctx, &pdu) < 0) {
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100511 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100512 return;
513 }
514
Pau Espin Pedrol9313da52021-02-15 12:12:44 +0100515 LOGPFSML(fi, LOGL_INFO, "Tx RIM RAN-INFO to request SI of %s\n",
516 osmo_cgi_ps_name(&ctx->cgi_ps));
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100517 rc = bssgp_tx_rim(&pdu, gprs_ns2_nse_nsei(ctx->ms->bts->nse));
518 if (rc < 0) {
Pau Espin Pedrol9313da52021-02-15 12:12:44 +0100519 LOGPFSML(fi, LOGL_ERROR, "Failed transmitting RIM RAN-INFO %s PDU: %d\n",
520 osmo_cgi_ps_name(&ctx->cgi_ps), rc);
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100521 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100522 return;
523 }
524}
525
526
527static void st_wait_request_si(struct osmo_fsm_inst *fi, uint32_t event, void *data)
528{
529 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100530 const Packet_Cell_Change_Notification_t *notif;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100531 struct si_cache_entry *entry;
532
533 switch (event) {
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100534 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
535 notif = (const Packet_Cell_Change_Notification_t *)data;
536 handle_retrans_pkt_cell_chg_notif(ctx, notif);
537 break;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100538 case NACC_EV_RX_SI:
539 entry = (struct si_cache_entry *)data;
540 /* Copy info since cache can be deleted at any point */
541 memcpy(&ctx->si_info, &entry->value, sizeof(ctx->si_info));
542 /* Tell the PCU scheduler we are ready to go, from here one we
543 * are polled/driven by the scheduler */
544 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
545 break;
546 default:
547 OSMO_ASSERT(0);
548 }
549}
550
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100551/* At this point, we already received all required SI information to send stored
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100552 * in struct nacc_fsm_ctx. We now wait for scheduler to ask us to construct
553 * RLCMAC DL CTRL messages to move FSM states forward
554 */
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100555static void st_tx_neighbour_data_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
556{
557 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
558 ctx->si_info_bytes_sent = 0;
559 ctx->container_idx = 0;
560}
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100561
562static void st_tx_neighbour_data(struct osmo_fsm_inst *fi, uint32_t event, void *data)
563{
564 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100565 const Packet_Cell_Change_Notification_t *notif;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100566 struct nacc_ev_create_rlcmac_msg_ctx *data_ctx;
567 bool all_si_info_sent;
568
569 switch (event) {
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100570 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
571 notif = (const Packet_Cell_Change_Notification_t *)data;
572 handle_retrans_pkt_cell_chg_notif(ctx, notif);
573 break;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100574 case NACC_EV_CREATE_RLCMAC_MSG:
575 data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data;
576 data_ctx->msg = create_packet_neighbour_cell_data(ctx, data_ctx->tbf, &all_si_info_sent);
577 if (!data_ctx->msg) {
578 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
579 return;
580 }
581 if (all_si_info_sent) /* DONE */
582 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
583 break;
584 default:
585 OSMO_ASSERT(0);
586 }
587}
588
Pau Espin Pedrol1aef1132021-02-01 19:33:59 +0100589/* st_cell_chg_continue_on_enter:
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100590 * At this point, we already sent all Pkt Cell Neighbour Change rlcmac
591 * blocks, and we only need to wait to be scheduled again to send PKT
592 * CELL CHANGE NOTIFICATION and then we are done
593 */
594
Pau Espin Pedrol1aef1132021-02-01 19:33:59 +0100595static void st_cell_chg_continue(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100596{
597 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100598 const Packet_Cell_Change_Notification_t *notif;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100599 struct nacc_ev_create_rlcmac_msg_ctx *data_ctx;
600
601 switch (event) {
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100602 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
603 notif = (const Packet_Cell_Change_Notification_t *)data;
604 handle_retrans_pkt_cell_chg_notif(ctx, notif);
605 break;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100606 case NACC_EV_CREATE_RLCMAC_MSG:
607 data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100608 data_ctx->msg = create_packet_cell_chg_continue(ctx, data_ctx, &ctx->continue_poll_fn);
609 if (data_ctx->msg) {
610 ctx->continue_poll_ts = data_ctx->ts;
611 nacc_fsm_state_chg(fi, NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK);
612 }
613 break;
614 default:
615 OSMO_ASSERT(0);
616 }
617}
618
619static void st_wait_cell_chg_continue_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
620{
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100621 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
622 const Packet_Cell_Change_Notification_t *notif;
623
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100624 switch (event) {
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100625 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
626 notif = (const Packet_Cell_Change_Notification_t *)data;
627 handle_retrans_pkt_cell_chg_notif(ctx, notif);
628 break;
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100629 case NACC_EV_TIMEOUT_CELL_CHG_CONTINUE:
630 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
631 break;
632 case NACC_EV_RX_CELL_CHG_CONTINUE_ACK:
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100633 nacc_fsm_state_chg(fi, NACC_ST_DONE);
634 break;
635 default:
636 OSMO_ASSERT(0);
637 }
638}
639
640
641static void st_done_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
642{
643 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
644}
645
646static void nacc_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
647{
648 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
649 /* after cleanup() finishes, FSM termination calls osmo_fsm_inst_free,
650 so we need to avoid double-freeing it during ctx talloc free
651 destructor */
652 talloc_reparent(ctx, ctx->ms, ctx->fi);
653 ctx->fi = NULL;
654
655 /* remove references from owning MS and free entire ctx */
656 ctx->ms->nacc = NULL;
657 talloc_free(ctx);
658}
659
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100660static int nacc_fsm_timer_cb(struct osmo_fsm_inst *fi)
661{
662 switch (fi->T) {
663 case PCU_TDEF_NEIGH_RESOLVE_TO:
664 case PCU_TDEF_SI_RESOLVE_TO:
665 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
666 break;
667 }
668 return 0;
669}
670
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100671static struct osmo_fsm_state nacc_fsm_states[] = {
672 [NACC_ST_INITIAL] = {
673 .in_event_mask =
674 X(NACC_EV_RX_CELL_CHG_NOTIFICATION),
675 .out_state_mask =
676 X(NACC_ST_WAIT_RESOLVE_RAC_CI),
677 .name = "INITIAL",
678 .action = st_initial,
679 },
680 [NACC_ST_WAIT_RESOLVE_RAC_CI] = {
681 .in_event_mask =
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100682 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100683 X(NACC_EV_RX_RAC_CI),
684 .out_state_mask =
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100685 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100686 X(NACC_ST_WAIT_REQUEST_SI) |
687 X(NACC_ST_TX_CELL_CHG_CONTINUE),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100688 .name = "WAIT_RESOLVE_RAC_CI",
689 .onenter = st_wait_resolve_rac_ci_on_enter,
690 .action = st_wait_resolve_rac_ci,
691 },
692 [NACC_ST_WAIT_REQUEST_SI] = {
693 .in_event_mask =
694 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
695 X(NACC_EV_RX_SI),
696 .out_state_mask =
Pau Espin Pedrol8e69fc02021-02-10 17:44:27 +0100697 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100698 X(NACC_ST_TX_NEIGHBOUR_DATA) |
699 X(NACC_ST_TX_CELL_CHG_CONTINUE),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100700 .name = "WAIT_REQUEST_SI",
701 .onenter = st_wait_request_si_on_enter,
702 .action = st_wait_request_si,
703 },
704 [NACC_ST_TX_NEIGHBOUR_DATA] = {
705 .in_event_mask =
706 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100707 X(NACC_EV_CREATE_RLCMAC_MSG),
708 .out_state_mask =
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100709 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100710 X(NACC_ST_TX_CELL_CHG_CONTINUE),
711 .name = "TX_NEIGHBOUR_DATA",
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100712 .onenter = st_tx_neighbour_data_on_enter,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100713 .action = st_tx_neighbour_data,
714 },
715 [NACC_ST_TX_CELL_CHG_CONTINUE] = {
716 .in_event_mask =
717 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100718 X(NACC_EV_CREATE_RLCMAC_MSG),
719 .out_state_mask =
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100720 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100721 X(NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100722 .name = "TX_CELL_CHG_CONTINUE",
Pau Espin Pedrol1aef1132021-02-01 19:33:59 +0100723 .action = st_cell_chg_continue,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100724 },
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100725 [NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK] = {
726 .in_event_mask =
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100727 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100728 X(NACC_EV_RX_CELL_CHG_CONTINUE_ACK) |
729 X(NACC_EV_TIMEOUT_CELL_CHG_CONTINUE),
730 .out_state_mask =
Pau Espin Pedrola78f0d52021-02-11 12:53:13 +0100731 X(NACC_ST_WAIT_RESOLVE_RAC_CI) |
Pau Espin Pedrol952cb3d2021-02-01 14:52:48 +0100732 X(NACC_ST_TX_CELL_CHG_CONTINUE) |
733 X(NACC_ST_DONE),
734 .name = "WAIT_CELL_CHG_CONTINUE_ACK",
735 .action = st_wait_cell_chg_continue_ack,
736 },
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100737 [NACC_ST_DONE] = {
738 .in_event_mask = 0,
739 .out_state_mask = 0,
740 .name = "DONE",
741 .onenter = st_done_on_enter,
742 },
743};
744
745static struct osmo_fsm nacc_fsm = {
746 .name = "NACC",
747 .states = nacc_fsm_states,
748 .num_states = ARRAY_SIZE(nacc_fsm_states),
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100749 .timer_cb = nacc_fsm_timer_cb,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100750 .cleanup = nacc_fsm_cleanup,
751 .log_subsys = DNACC,
752 .event_names = nacc_fsm_event_names,
753};
754
755static __attribute__((constructor)) void nacc_fsm_init(void)
756{
757 OSMO_ASSERT(osmo_fsm_register(&nacc_fsm) == 0);
758}
759
760void nacc_fsm_ctrl_reply_cb(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, void *data)
761{
762 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)data;
763 char *tmp = NULL, *tok, *saveptr;
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100764 unsigned int exp_id;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200765 struct osmo_cell_global_id_ps cgi_ps;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100766
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100767 LOGPFSML(ctx->fi, LOGL_NOTICE, "Received CTRL message: type=%d %s %s: %s\n",
768 cmd->type, cmd->variable, cmd->id, osmo_escape_str(cmd->reply, -1));
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100769
770 if (cmd->type != CTRL_TYPE_GET_REPLY || !cmd->reply) {
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200771 osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, NULL);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100772 return;
773 }
774
Pau Espin Pedrol069a6372021-02-10 17:33:13 +0100775 /* Validate it's the seqnum from our last GET cmd, and not from older
776 * one we may have requested in case MS decided to resend Pkt Cell
777 * Change Notify with a different tgt cell:
778 */
779 exp_id = arfcn_bsic_2_ctrl_id(ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic);
780 if ((unsigned int)atoi(cmd->id) != exp_id) {
781 LOGPFSML(ctx->fi, LOGL_INFO,
782 "Received CTRL message with id=%s doesn't match our expected last id=%d, ignoring\n",
783 cmd->id, exp_id);
784 return;
785 }
786
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100787 /* TODO: Potentially validate cmd->variable contains same params as we
788 sent, and that cmd->id matches the original set. We may want to keep
789 the original cmd around by setting cmd->defer=1 when sending it. */
790
791 tmp = talloc_strdup(cmd, cmd->reply);
792 if (!tmp)
793 goto free_ret;
794
795 if (!(tok = strtok_r(tmp, "-", &saveptr)))
796 goto free_ret;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200797 cgi_ps.rai.lac.plmn.mcc = atoi(tok);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100798
799 if (!(tok = strtok_r(NULL, "-", &saveptr)))
800 goto free_ret;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200801 cgi_ps.rai.lac.plmn.mnc = atoi(tok);
Pau Espin Pedrol263c75c2022-06-29 18:56:42 +0200802 cgi_ps.rai.lac.plmn.mnc_3_digits = strlen(tok) > 2;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100803
804 if (!(tok = strtok_r(NULL, "-", &saveptr)))
805 goto free_ret;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200806 cgi_ps.rai.lac.lac = atoi(tok);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100807
808 if (!(tok = strtok_r(NULL, "-", &saveptr)))
809 goto free_ret;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200810 cgi_ps.rai.rac = atoi(tok);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100811
812 if (!(tok = strtok_r(NULL, "\0", &saveptr)))
813 goto free_ret;
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200814 cgi_ps.cell_identity = atoi(tok);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100815
816 /* Cache the cgi_ps so we can avoid requesting again same resolution for a while */
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200817 neigh_cache_add(ctx->ms->bts->pcu->neigh_cache, &ctx->neigh_key, &cgi_ps);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100818
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200819 osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, &cgi_ps);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100820 return;
821
822free_ret:
823 talloc_free(tmp);
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200824 osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, NULL);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100825 return;
826}
827
828static int nacc_fsm_ctx_talloc_destructor(struct nacc_fsm_ctx *ctx)
829{
830 if (ctx->fi) {
831 osmo_fsm_inst_free(ctx->fi);
832 ctx->fi = NULL;
833 }
834
835 if (ctx->neigh_ctrl_conn) {
836 if (ctx->neigh_ctrl_conn->write_queue.bfd.fd != -1) {
837 osmo_wqueue_clear(&ctx->neigh_ctrl_conn->write_queue);
838 osmo_fd_unregister(&ctx->neigh_ctrl_conn->write_queue.bfd);
839 close(ctx->neigh_ctrl_conn->write_queue.bfd.fd);
840 ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1;
841 }
842 }
843
844 return 0;
845}
846
847struct nacc_fsm_ctx *nacc_fsm_alloc(struct GprsMs* ms)
848{
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100849 struct nacc_fsm_ctx *ctx = talloc_zero(ms, struct nacc_fsm_ctx);
850 char buf[64];
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100851
852 talloc_set_destructor(ctx, nacc_fsm_ctx_talloc_destructor);
853
854 ctx->ms = ms;
855
856 snprintf(buf, sizeof(buf), "TLLI-0x%08x", ms_tlli(ms));
857 ctx->fi = osmo_fsm_inst_alloc(&nacc_fsm, ctx, ctx, LOGL_INFO, buf);
858 if (!ctx->fi)
859 goto free_ret;
860
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200861 /* If CTRL ip present, use the old CTRL interface for neighbor resolution */
862 if (ms->bts->pcu->vty.neigh_ctrl_addr) {
863 ctx->neigh_ctrl = ctrl_handle_alloc(ctx, ctx, NULL);
864 ctx->neigh_ctrl->reply_cb = nacc_fsm_ctrl_reply_cb;
865 ctx->neigh_ctrl_conn = osmo_ctrl_conn_alloc(ctx, ctx->neigh_ctrl);
866 if (!ctx->neigh_ctrl_conn)
867 goto free_ret;
868 /* Older versions of osmo_ctrl_conn_alloc didn't properly initialize fd to -1,
869 * so make sure to do it here otherwise fd may be valid fd 0 and cause trouble */
870 ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1;
871 llist_add(&ctx->neigh_ctrl_conn->list_entry, &ctx->neigh_ctrl->ccon_list);
872 }
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100873
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100874 return ctx;
875free_ret:
876 talloc_free(ctx);
877 return NULL;
878}
Pau Espin Pedrol5557c0a2021-09-07 14:09:50 +0200879
880bool nacc_fsm_is_waiting_addr_resolution(const struct nacc_fsm_ctx *ctx,
881 const struct neigh_cache_entry_key *neigh_key)
882{
883 if (ctx->fi->state != NACC_ST_WAIT_RESOLVE_RAC_CI)
884 return false;
885 return neigh_cache_entry_key_eq(&ctx->neigh_key, neigh_key);
886}
Pau Espin Pedrol6c81add2021-09-07 14:43:46 +0200887
888bool nacc_fsm_is_waiting_si_resolution(const struct nacc_fsm_ctx *ctx,
889 const struct osmo_cell_global_id_ps *cgi_ps)
890{
891 if (ctx->fi->state != NACC_ST_WAIT_REQUEST_SI)
892 return false;
893 return !osmo_cgi_ps_cmp(&ctx->cgi_ps, cgi_ps);
894}
Pau Espin Pedrolc276e492021-09-28 12:53:35 +0200895
896bool nacc_fsm_exp_ctrl_ack(const struct nacc_fsm_ctx *ctx, uint32_t fn, uint8_t ts)
897{
898 return ctx->fi->state == NACC_ST_WAIT_CELL_CHG_CONTINUE_ACK &&
899 ctx->continue_poll_fn == fn &&
900 ctx->continue_poll_ts == ts;
901}