blob: fcf4b586d16f27e4c76965aa8b42e274138f873c [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <unistd.h>
22
23#include <talloc.h>
24
25#include <osmocom/core/rate_ctr.h>
26#include <osmocom/ctrl/control_cmd.h>
27#include <osmocom/ctrl/control_if.h>
28
29#include <osmocom/gsm/gsm48.h>
30#include <osmocom/gprs/gprs_bssgp.h>
31#include <osmocom/gprs/gprs_bssgp_rim.h>
32
33#include <nacc_fsm.h>
34#include <gprs_rlcmac.h>
35#include <gprs_debug.h>
36#include <gprs_ms.h>
37#include <encoding.h>
38#include <bts.h>
39#include <neigh_cache.h>
40
41#define X(s) (1 << (s))
42
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +010043static const struct osmo_tdef_state_timeout nacc_fsm_timeouts[32] = {
44 [NACC_ST_INITIAL] = {},
45 [NACC_ST_WAIT_RESOLVE_RAC_CI] = { .T = PCU_TDEF_NEIGH_RESOLVE_TO },
46 [NACC_ST_WAIT_REQUEST_SI] = { .T = PCU_TDEF_SI_RESOLVE_TO },
47 [NACC_ST_TX_NEIGHBOUR_DATA] = {},
48 [NACC_ST_TX_CELL_CHG_CONTINUE] = {},
49 [NACC_ST_DONE] = {},
50};
51
52/* Transition to a state, using the T timer defined in assignment_fsm_timeouts.
53 * 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" },
67 { 0, NULL }
68};
69
70/* TS 44 060 11.2.9e Packet Neighbour Cell Data */
71static struct msgb *create_packet_neighbour_cell_data(struct nacc_fsm_ctx *ctx,
72 const struct gprs_rlcmac_tbf *tbf,
73 bool *all_si_info_sent)
74{
75 struct msgb *msg;
76 int rc;
77 RlcMacDownlink_t *mac_control_block;
78 struct GprsMs *ms = tbf_ms(tbf);
79 OSMO_ASSERT(tbf_is_tfi_assigned(tbf));
80 uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF;
81 uint8_t tfi = tbf_tfi(tbf);
82 uint8_t container_id = 0;
83 PNCDContainer_t container;
84 size_t max_len, len_to_write;
85 uint8_t *cont_buf;
86 uint8_t si_type = ctx->si_info.type_psi ? 0x01 : 0x0;
87
88 memset(&container, 0, sizeof(container));
89 if (ctx->container_idx == 0) {
90 container.UnionType = 1; /* with ID */
91 container.u.PNCD_Container_With_ID.ARFCN = ctx->neigh_key.tgt_arfcn;
92 container.u.PNCD_Container_With_ID.BSIC = ctx->neigh_key.tgt_bsic;
93 cont_buf = &container.u.PNCD_Container_With_ID.CONTAINER[0];
94 max_len = sizeof(container.u.PNCD_Container_With_ID.CONTAINER) - 1;
95 } else {
96 container.UnionType = 0; /* without ID */
97 cont_buf = &container.u.PNCD_Container_Without_ID.CONTAINER[0];
98 max_len = sizeof(container.u.PNCD_Container_Without_ID.CONTAINER) - 1;
99 }
100
101 len_to_write = ctx->si_info.si_len - ctx->si_info_bytes_sent;
102
103 if (len_to_write == 0) {
104 /* We sent all info on last message filing it exactly, we now send a zeroed one to finish */
105 *all_si_info_sent = true;
106 *cont_buf = (si_type << 5) | 0x00;
107 } else if (len_to_write >= max_len) {
108 /* We fill the rlcmac block, we'll need more messages */
109 *all_si_info_sent = false;
110 *cont_buf = (si_type << 5) | 0x1F;
111 memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], max_len);
112 ctx->si_info_bytes_sent += max_len;
113 } else {
114 /* Last block, we don't fill it exactly */
115 *all_si_info_sent = true;
116 *cont_buf = (si_type << 5) | (len_to_write & 0x1F);
117 memcpy(cont_buf + 1, &ctx->si_info.si_buf[ctx->si_info_bytes_sent], len_to_write);
118 ctx->si_info_bytes_sent += len_to_write;
119 }
120
121 msg = msgb_alloc(GSM_MACBLOCK_LEN, "neighbour_cell_data");
122 if (!msg)
123 return NULL;
124
125 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
126 struct bitvec bv = {
127 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
128 .data_len = GSM_MACBLOCK_LEN,
129 };
130 bitvec_unhex(&bv, DUMMY_VEC);
131
132 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t);
133
134 write_packet_neighbour_cell_data(mac_control_block,
135 tfi_is_dl, tfi, container_id,
136 ctx->container_idx, &container);
137 LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Neighbour Cell Data +++++++++++++++++++++++++\n");
138 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
139 if (rc < 0) {
140 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Neighbour Cell Data failed (%d)\n", rc);
141 goto free_ret;
142 }
143 LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Neighbour Cell Data -------------------------\n");
144 rate_ctr_inc(&bts_rate_counters(ms->bts)->ctr[CTR_PKT_NEIGH_CELL_DATA]);
145 talloc_free(mac_control_block);
146
147 ctx->container_idx++;
148
149 return msg;
150
151free_ret:
152 talloc_free(mac_control_block);
153 msgb_free(msg);
154 return NULL;
155}
156
157/* TS 44 060 11.2.2a Packet Cell Change Continue */
158static struct msgb *create_packet_cell_chg_continue(const struct nacc_fsm_ctx *ctx,
159 const struct gprs_rlcmac_tbf *tbf)
160{
161 struct msgb *msg;
162 int rc;
163 RlcMacDownlink_t *mac_control_block;
164 struct GprsMs *ms = tbf_ms(tbf);
165
166 msg = msgb_alloc(GSM_MACBLOCK_LEN, "pkt_cell_chg_continue");
167 if (!msg)
168 return NULL;
169
170 /* Initialize a bit vector that uses allocated msgb as the data buffer. */
171 struct bitvec bv = {
172 .data = msgb_put(msg, GSM_MACBLOCK_LEN),
173 .data_len = GSM_MACBLOCK_LEN,
174 };
175 bitvec_unhex(&bv, DUMMY_VEC);
176
177 mac_control_block = (RlcMacDownlink_t *)talloc_zero(ctx->ms, RlcMacDownlink_t);
178
179 OSMO_ASSERT(tbf_is_tfi_assigned(tbf));
180 uint8_t tfi_is_dl = tbf_direction(tbf) == GPRS_RLCMAC_DL_TBF;
181 uint8_t tfi = tbf_tfi(tbf);
182 uint8_t container_id = 0;
183 write_packet_cell_change_continue(mac_control_block, tfi_is_dl, tfi, true,
184 ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic, container_id);
185 LOGP(DNACC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Cell Change Continue +++++++++++++++++++++++++\n");
186 rc = encode_gsm_rlcmac_downlink(&bv, mac_control_block);
187 if (rc < 0) {
188 LOGP(DTBF, LOGL_ERROR, "Encoding of Packet Cell Change Continue failed (%d)\n", rc);
189 goto free_ret;
190 }
191 LOGP(DNACC, LOGL_DEBUG, "------------------------- TX : Packet Cell Change Continue -------------------------\n");
192 rate_ctr_inc(&bts_rate_counters(ms->bts)->ctr[CTR_PKT_CELL_CHG_CONTINUE]);
193 talloc_free(mac_control_block);
194 return msg;
195
196free_ret:
197 talloc_free(mac_control_block);
198 msgb_free(msg);
199 return NULL;
200}
201
202static int fill_rim_ran_info_req(const struct nacc_fsm_ctx *ctx, struct bssgp_ran_information_pdu *pdu)
203{
204 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
205
206 *pdu = (struct bssgp_ran_information_pdu){
207 .routing_info_dest = {
208 .discr = BSSGP_RIM_ROUTING_INFO_GERAN,
209 .geran = {
210 .raid = {
211 .mcc = ctx->cgi_ps.rai.lac.plmn.mcc,
212 .mnc = ctx->cgi_ps.rai.lac.plmn.mnc,
213 .mnc_3_digits = ctx->cgi_ps.rai.lac.plmn.mnc_3_digits,
214 .lac = ctx->cgi_ps.rai.lac.lac,
215 .rac = ctx->cgi_ps.rai.rac,
216 },
217 .cid = ctx->cgi_ps.cell_identity,
218 },
219 },
220 .routing_info_src = {
221 .discr = BSSGP_RIM_ROUTING_INFO_GERAN,
222 .geran = {
223 .raid = {
224 .mcc = bts->cgi_ps.rai.lac.plmn.mcc,
225 .mnc = bts->cgi_ps.rai.lac.plmn.mnc,
226 .mnc_3_digits = bts->cgi_ps.rai.lac.plmn.mnc_3_digits,
227 .lac = bts->cgi_ps.rai.lac.lac,
228 .rac = bts->cgi_ps.rai.rac,
229 },
230 .cid = bts->cgi_ps.cell_identity,
231 },
232 },
233 .rim_cont_iei = BSSGP_IE_RI_REQ_RIM_CONTAINER,
234 .decoded_present = true,
235 .decoded = {
236 .req_rim_cont = {
237 .app_id = BSSGP_RAN_INF_APP_ID_NACC,
238 .seq_num = 1,
239 .pdu_ind = {
240 .ack_requested = 0,
241 .pdu_type_ext = RIM_PDU_TYPE_SING_REP,
242 },
243 .prot_ver = 1,
244 .son_trans_app_id = NULL,
245 .son_trans_app_id_len = 0,
246 .u = {
247 .app_cont_nacc = {
248 .reprt_cell = ctx->cgi_ps,
249 },
250 },
251 },
252 },
253 };
254
255 return 0;
256}
257
258
259////////////////
260// FSM states //
261////////////////
262
263static void st_initial(struct osmo_fsm_inst *fi, uint32_t event, void *data)
264{
265 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
266 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
267 Packet_Cell_Change_Notification_t *notif;
268
269 switch (event) {
270 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
271 notif = (Packet_Cell_Change_Notification_t *)data;
272 switch (notif->Target_Cell.UnionType) {
273 case 0: /* GSM */
274 ctx->neigh_key.local_lac = bts->cgi_ps.rai.lac.lac;
275 ctx->neigh_key.local_ci = bts->cgi_ps.cell_identity;
276 ctx->neigh_key.tgt_arfcn = notif->Target_Cell.u.Target_Cell_GSM_Notif.ARFCN;
277 ctx->neigh_key.tgt_bsic = notif->Target_Cell.u.Target_Cell_GSM_Notif.BSIC;
278 nacc_fsm_state_chg(fi, NACC_ST_WAIT_RESOLVE_RAC_CI);
279 break;
280 default:
281 LOGPFSML(fi, LOGL_NOTICE, "TargetCell type=0x%x not supported\n",
282 notif->Target_Cell.UnionType);
283 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
284 return;
285 }
286 break;
287 default:
288 OSMO_ASSERT(0);
289 }
290}
291
292static void st_wait_resolve_rac_ci_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
293{
294 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
295 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
296 struct gprs_pcu *pcu = bts->pcu;
297 const struct osmo_cell_global_id_ps *cgi_ps;
Pau Espin Pedrolc0805e62021-01-27 17:16:59 +0100298 struct ctrl_cmd *cmd = NULL;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100299 int rc;
300
301 /* First try to find the value in the cache */
302 cgi_ps = neigh_cache_lookup_value(pcu->neigh_cache, &ctx->neigh_key);
303 if (cgi_ps) {
304 ctx->cgi_ps = *cgi_ps;
305 nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI);
306 return;
307 }
308
309 /* CGI-PS not in cache, resolve it using BSC Neighbor Resolution CTRL interface */
310
311 LOGPFSML(fi, LOGL_DEBUG, "No CGI-PS found in cache, resolving " NEIGH_CACHE_ENTRY_KEY_FMT "...\n",
312 NEIGH_CACHE_ENTRY_KEY_ARGS(&ctx->neigh_key));
313
Pau Espin Pedrolc0805e62021-01-27 17:16:59 +0100314 rc = osmo_sock_init2_ofd(&ctx->neigh_ctrl_conn->write_queue.bfd,
315 AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP,
316 NULL, 0, pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port,
317 OSMO_SOCK_F_CONNECT);
318 if (rc < 0) {
319 LOGPFSML(fi, LOGL_ERROR, "Can't connect to CTRL @ %s:%u\n",
320 pcu->vty.neigh_ctrl_addr, pcu->vty.neigh_ctrl_port);
321 goto err_term;
322 }
323
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100324 cmd = ctrl_cmd_create(ctx, CTRL_TYPE_GET);
325 if (!cmd) {
326 LOGPFSML(fi, LOGL_ERROR, "CTRL msg creation failed\n");
327 goto err_term;
328 }
329
330 cmd->id = talloc_asprintf(cmd, "1");
331 cmd->variable = talloc_asprintf(cmd, "neighbor_resolve_cgi_ps_from_lac_ci.%d.%d.%d.%d",
332 ctx->neigh_key.local_lac, ctx->neigh_key.local_ci,
333 ctx->neigh_key.tgt_arfcn, ctx->neigh_key.tgt_bsic);
334 rc = ctrl_cmd_send(&ctx->neigh_ctrl_conn->write_queue, cmd);
335 if (rc) {
336 LOGPFSML(fi, LOGL_ERROR, "CTRL msg sent failed: %d\n", rc);
337 goto err_term;
338 }
339
340 talloc_free(cmd);
341 return;
342
343err_term:
344 talloc_free(cmd);
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100345 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100346}
347
348
349static void st_wait_resolve_rac_ci(struct osmo_fsm_inst *fi, uint32_t event, void *data)
350{
351 switch (event) {
352 case NACC_EV_RX_CELL_CHG_NOTIFICATION:
353 break;
354 case NACC_EV_RX_RAC_CI:
355 /* Assumption: ctx->cgi_ps has been filled by caller of the event */
356 nacc_fsm_state_chg(fi, NACC_ST_WAIT_REQUEST_SI);
357 break;
358 default:
359 OSMO_ASSERT(0);
360 }
361}
362
363/* At this point, we expect correct tgt cell info to be already in ctx->cgi_ps */
364static void st_wait_request_si_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
365{
366 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
367 struct gprs_rlcmac_bts *bts = ctx->ms->bts;
368 struct gprs_pcu *pcu = bts->pcu;
369 struct bssgp_ran_information_pdu pdu;
370 const struct si_cache_value *si;
371 int rc;
372
373 /* First check if we have SI info for the target cell in cache */
374 si = si_cache_lookup_value(pcu->si_cache, &ctx->cgi_ps);
375 if (si) {
376 /* Copy info since cache can be deleted at any point */
377 memcpy(&ctx->si_info, si, sizeof(ctx->si_info));
378 /* Tell the PCU scheduler we are ready to go, from here one we
379 * are polled/driven by the scheduler */
380 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
381 return;
382 }
383
384 /* SI info not in cache, resolve it using RIM procedure against SGSN */
385 if (fill_rim_ran_info_req(ctx, &pdu) < 0) {
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100386 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100387 return;
388 }
389
390 rc = bssgp_tx_rim(&pdu, gprs_ns2_nse_nsei(ctx->ms->bts->nse));
391 if (rc < 0) {
392 LOGPFSML(fi, LOGL_ERROR, "Failed transmitting RIM PDU: %d\n", rc);
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100393 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100394 return;
395 }
396}
397
398
399static void st_wait_request_si(struct osmo_fsm_inst *fi, uint32_t event, void *data)
400{
401 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
402 struct si_cache_entry *entry;
403
404 switch (event) {
405 case NACC_EV_RX_SI:
406 entry = (struct si_cache_entry *)data;
407 /* Copy info since cache can be deleted at any point */
408 memcpy(&ctx->si_info, &entry->value, sizeof(ctx->si_info));
409 /* Tell the PCU scheduler we are ready to go, from here one we
410 * are polled/driven by the scheduler */
411 nacc_fsm_state_chg(fi, NACC_ST_TX_NEIGHBOUR_DATA);
412 break;
413 default:
414 OSMO_ASSERT(0);
415 }
416}
417
418/* st_tx_neighbour_data_on_enter:
419 * At this point, we already received all required SI information to send stored
420 * in struct nacc_fsm_ctx. We now wait for scheduler to ask us to construct
421 * RLCMAC DL CTRL messages to move FSM states forward
422 */
423
424static void st_tx_neighbour_data(struct osmo_fsm_inst *fi, uint32_t event, void *data)
425{
426 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
427 struct nacc_ev_create_rlcmac_msg_ctx *data_ctx;
428 bool all_si_info_sent;
429
430 switch (event) {
431 case NACC_EV_CREATE_RLCMAC_MSG:
432 data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data;
433 data_ctx->msg = create_packet_neighbour_cell_data(ctx, data_ctx->tbf, &all_si_info_sent);
434 if (!data_ctx->msg) {
435 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
436 return;
437 }
438 if (all_si_info_sent) /* DONE */
439 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
440 break;
441 default:
442 OSMO_ASSERT(0);
443 }
444}
445
446/* st_cell_cgh_continue_on_enter:
447 * At this point, we already sent all Pkt Cell Neighbour Change rlcmac
448 * blocks, and we only need to wait to be scheduled again to send PKT
449 * CELL CHANGE NOTIFICATION and then we are done
450 */
451
452static void st_cell_cgh_continue(struct osmo_fsm_inst *fi, uint32_t event, void *data)
453{
454 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
455 struct nacc_ev_create_rlcmac_msg_ctx *data_ctx;
456
457 switch (event) {
458 case NACC_EV_CREATE_RLCMAC_MSG:
459 data_ctx = (struct nacc_ev_create_rlcmac_msg_ctx *)data;
460 data_ctx->msg = create_packet_cell_chg_continue(ctx, data_ctx->tbf);
461 nacc_fsm_state_chg(fi, NACC_ST_DONE);
462 break;
463 default:
464 OSMO_ASSERT(0);
465 }
466}
467
468
469static void st_done_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
470{
471 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
472}
473
474static void nacc_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
475{
476 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)fi->priv;
477 /* after cleanup() finishes, FSM termination calls osmo_fsm_inst_free,
478 so we need to avoid double-freeing it during ctx talloc free
479 destructor */
480 talloc_reparent(ctx, ctx->ms, ctx->fi);
481 ctx->fi = NULL;
482
483 /* remove references from owning MS and free entire ctx */
484 ctx->ms->nacc = NULL;
485 talloc_free(ctx);
486}
487
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100488static int nacc_fsm_timer_cb(struct osmo_fsm_inst *fi)
489{
490 switch (fi->T) {
491 case PCU_TDEF_NEIGH_RESOLVE_TO:
492 case PCU_TDEF_SI_RESOLVE_TO:
493 nacc_fsm_state_chg(fi, NACC_ST_TX_CELL_CHG_CONTINUE);
494 break;
495 }
496 return 0;
497}
498
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100499static struct osmo_fsm_state nacc_fsm_states[] = {
500 [NACC_ST_INITIAL] = {
501 .in_event_mask =
502 X(NACC_EV_RX_CELL_CHG_NOTIFICATION),
503 .out_state_mask =
504 X(NACC_ST_WAIT_RESOLVE_RAC_CI),
505 .name = "INITIAL",
506 .action = st_initial,
507 },
508 [NACC_ST_WAIT_RESOLVE_RAC_CI] = {
509 .in_event_mask =
510 X(NACC_EV_RX_RAC_CI),
511 .out_state_mask =
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100512 X(NACC_ST_WAIT_REQUEST_SI) |
513 X(NACC_ST_TX_CELL_CHG_CONTINUE),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100514 .name = "WAIT_RESOLVE_RAC_CI",
515 .onenter = st_wait_resolve_rac_ci_on_enter,
516 .action = st_wait_resolve_rac_ci,
517 },
518 [NACC_ST_WAIT_REQUEST_SI] = {
519 .in_event_mask =
520 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
521 X(NACC_EV_RX_SI),
522 .out_state_mask =
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100523 X(NACC_ST_TX_NEIGHBOUR_DATA) |
524 X(NACC_ST_TX_CELL_CHG_CONTINUE),
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100525 .name = "WAIT_REQUEST_SI",
526 .onenter = st_wait_request_si_on_enter,
527 .action = st_wait_request_si,
528 },
529 [NACC_ST_TX_NEIGHBOUR_DATA] = {
530 .in_event_mask =
531 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
532 X(NACC_EV_RX_SI) |
533 X(NACC_EV_CREATE_RLCMAC_MSG),
534 .out_state_mask =
535 X(NACC_ST_TX_CELL_CHG_CONTINUE),
536 .name = "TX_NEIGHBOUR_DATA",
537 .action = st_tx_neighbour_data,
538 },
539 [NACC_ST_TX_CELL_CHG_CONTINUE] = {
540 .in_event_mask =
541 X(NACC_EV_RX_CELL_CHG_NOTIFICATION) |
542 X(NACC_EV_RX_SI) |
543 X(NACC_EV_CREATE_RLCMAC_MSG),
544 .out_state_mask =
545 X(NACC_ST_DONE),
546 .name = "TX_CELL_CHG_CONTINUE",
547 .action = st_cell_cgh_continue,
548 },
549 [NACC_ST_DONE] = {
550 .in_event_mask = 0,
551 .out_state_mask = 0,
552 .name = "DONE",
553 .onenter = st_done_on_enter,
554 },
555};
556
557static struct osmo_fsm nacc_fsm = {
558 .name = "NACC",
559 .states = nacc_fsm_states,
560 .num_states = ARRAY_SIZE(nacc_fsm_states),
Pau Espin Pedrol41a22a72021-01-26 19:00:37 +0100561 .timer_cb = nacc_fsm_timer_cb,
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100562 .cleanup = nacc_fsm_cleanup,
563 .log_subsys = DNACC,
564 .event_names = nacc_fsm_event_names,
565};
566
567static __attribute__((constructor)) void nacc_fsm_init(void)
568{
569 OSMO_ASSERT(osmo_fsm_register(&nacc_fsm) == 0);
570}
571
572void nacc_fsm_ctrl_reply_cb(struct ctrl_handle *ctrl, struct ctrl_cmd *cmd, void *data)
573{
574 struct nacc_fsm_ctx *ctx = (struct nacc_fsm_ctx *)data;
575 char *tmp = NULL, *tok, *saveptr;
576
577 LOGPFSML(ctx->fi, LOGL_NOTICE, "Received CTRL message: type=%d %s: %s\n",
578 cmd->type, cmd->variable, osmo_escape_str(cmd->reply, -1));
579
580 if (cmd->type != CTRL_TYPE_GET_REPLY || !cmd->reply) {
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100581 nacc_fsm_state_chg(ctx->fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100582 return;
583 }
584
585 /* TODO: Potentially validate cmd->variable contains same params as we
586 sent, and that cmd->id matches the original set. We may want to keep
587 the original cmd around by setting cmd->defer=1 when sending it. */
588
589 tmp = talloc_strdup(cmd, cmd->reply);
590 if (!tmp)
591 goto free_ret;
592
593 if (!(tok = strtok_r(tmp, "-", &saveptr)))
594 goto free_ret;
595 ctx->cgi_ps.rai.lac.plmn.mcc = atoi(tok);
596
597 if (!(tok = strtok_r(NULL, "-", &saveptr)))
598 goto free_ret;
599 ctx->cgi_ps.rai.lac.plmn.mnc = atoi(tok);
600
601 if (!(tok = strtok_r(NULL, "-", &saveptr)))
602 goto free_ret;
603 ctx->cgi_ps.rai.lac.lac = atoi(tok);
604
605 if (!(tok = strtok_r(NULL, "-", &saveptr)))
606 goto free_ret;
607 ctx->cgi_ps.rai.rac = atoi(tok);
608
609 if (!(tok = strtok_r(NULL, "\0", &saveptr)))
610 goto free_ret;
611 ctx->cgi_ps.cell_identity = atoi(tok);
612
613 /* Cache the cgi_ps so we can avoid requesting again same resolution for a while */
614 neigh_cache_add(ctx->ms->bts->pcu->neigh_cache, &ctx->neigh_key, &ctx->cgi_ps);
615
616 osmo_fsm_inst_dispatch(ctx->fi, NACC_EV_RX_RAC_CI, NULL);
617 return;
618
619free_ret:
620 talloc_free(tmp);
Pau Espin Pedrola06ac182021-01-26 19:13:43 +0100621 nacc_fsm_state_chg(ctx->fi, NACC_ST_TX_CELL_CHG_CONTINUE);
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100622 return;
623}
624
625static int nacc_fsm_ctx_talloc_destructor(struct nacc_fsm_ctx *ctx)
626{
627 if (ctx->fi) {
628 osmo_fsm_inst_free(ctx->fi);
629 ctx->fi = NULL;
630 }
631
632 if (ctx->neigh_ctrl_conn) {
633 if (ctx->neigh_ctrl_conn->write_queue.bfd.fd != -1) {
634 osmo_wqueue_clear(&ctx->neigh_ctrl_conn->write_queue);
635 osmo_fd_unregister(&ctx->neigh_ctrl_conn->write_queue.bfd);
636 close(ctx->neigh_ctrl_conn->write_queue.bfd.fd);
637 ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1;
638 }
639 }
640
641 return 0;
642}
643
644struct nacc_fsm_ctx *nacc_fsm_alloc(struct GprsMs* ms)
645{
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100646 struct nacc_fsm_ctx *ctx = talloc_zero(ms, struct nacc_fsm_ctx);
647 char buf[64];
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100648
649 talloc_set_destructor(ctx, nacc_fsm_ctx_talloc_destructor);
650
651 ctx->ms = ms;
652
653 snprintf(buf, sizeof(buf), "TLLI-0x%08x", ms_tlli(ms));
654 ctx->fi = osmo_fsm_inst_alloc(&nacc_fsm, ctx, ctx, LOGL_INFO, buf);
655 if (!ctx->fi)
656 goto free_ret;
657
658 ctx->neigh_ctrl = ctrl_handle_alloc(ctx, ctx, NULL);
659 ctx->neigh_ctrl->reply_cb = nacc_fsm_ctrl_reply_cb;
660 ctx->neigh_ctrl_conn = osmo_ctrl_conn_alloc(ctx, ctx->neigh_ctrl);
661 if (!ctx->neigh_ctrl_conn)
662 goto free_ret;
Pau Espin Pedrol202a4782021-01-27 17:05:12 +0100663 /* Older versions of osmo_ctrl_conn_alloc didn't properly initialize fd to -1,
664 * so make sure to do it here otherwise fd may be valid fd 0 and cause trouble */
665 ctx->neigh_ctrl_conn->write_queue.bfd.fd = -1;
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100666 llist_add(&ctx->neigh_ctrl_conn->list_entry, &ctx->neigh_ctrl->ccon_list);
667
Pau Espin Pedrolc0a250d2021-01-21 18:46:13 +0100668 return ctx;
669free_ret:
670 talloc_free(ctx);
671 return NULL;
672}