blob: 2bcd0df366df60cadf33b104f63a94842ee3807a [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \file gprs_ns2_sns.c
2 * NS Sub-Network Service Protocol implementation
3 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
4 * as well as its successor 3GPP TS 48.016 */
5
Harald Weltec1c7e4a2021-03-02 20:47:29 +01006/* (C) 2018-2021 by Harald Welte <laforge@gnumonks.org>
Alexander Couzens6a161492020-07-12 13:45:50 +02007 * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
8 * Author: Alexander Couzens <lynxis@fe80.eu>
9 *
10 * All Rights Reserved
11 *
12 * SPDX-License-Identifier: GPL-2.0+
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 *
27 */
28
29/* The BSS NSE only has one SGSN IP address configured, and it will use the SNS procedures
30 * to communicated its local IPs/ports as well as all the SGSN side IPs/ports and
31 * associated weights. The BSS then uses this to establish a full mesh
32 * of NSVCs between all BSS-side IPs/ports and SGSN-side IPs/ports.
33 *
34 * Known limitation/expectation/bugs:
35 * - No concurrent dual stack. It supports either IPv4 or IPv6, but not both at the same time.
36 * - SNS Add/Change/Delete: Doesn't answer on the same NSVC as received SNS ADD/CHANGE/DELETE PDUs.
37 * - SNS Add/Change/Delete: Doesn't communicated the failed IPv4/IPv6 entries on the SNS_ACK.
38 */
39
40#include <errno.h>
41
42#include <netinet/in.h>
43#include <arpa/inet.h>
44#include <stdint.h>
45
46#include <osmocom/core/fsm.h>
47#include <osmocom/core/msgb.h>
48#include <osmocom/core/socket.h>
Alexander Couzens412bc342020-11-19 05:24:37 +010049#include <osmocom/core/sockaddr_str.h>
Alexander Couzens6a161492020-07-12 13:45:50 +020050#include <osmocom/gsm/tlv.h>
51#include <osmocom/gprs/gprs_msgb.h>
52#include <osmocom/gprs/gprs_ns2.h>
53#include <osmocom/gprs/protocol/gsm_08_16.h>
54
55#include "gprs_ns2_internal.h"
56
57#define S(x) (1 << (x))
58
59enum ns2_sns_type {
60 IPv4,
61 IPv6,
62};
63
Harald Welte4f127462021-03-02 20:49:10 +010064enum ns2_sns_role {
65 GPRS_SNS_ROLE_BSS,
66 GPRS_SNS_ROLE_SGSN,
67};
68
Harald Welte694dad52021-03-23 15:22:16 +010069/* BSS-side-only states _ST_BSS_; SGSN-side only states _ST_SGSN_; others shared */
Alexander Couzens6a161492020-07-12 13:45:50 +020070enum gprs_sns_bss_state {
71 GPRS_SNS_ST_UNCONFIGURED,
Harald Welte694dad52021-03-23 15:22:16 +010072 GPRS_SNS_ST_BSS_SIZE, /*!< SNS-SIZE procedure ongoing */
73 GPRS_SNS_ST_BSS_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */
74 GPRS_SNS_ST_BSS_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */
Alexander Couzens6a161492020-07-12 13:45:50 +020075 GPRS_SNS_ST_CONFIGURED,
Harald Welte4f127462021-03-02 20:49:10 +010076 GPRS_SNS_ST_SGSN_WAIT_CONFIG, /* !< SGSN role: Wait for CONFIG from BSS */
77 GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, /* !< SGSN role: Wait for CONFIG-ACK from BSS */
Alexander Couzens6a161492020-07-12 13:45:50 +020078};
79
80enum gprs_sns_event {
Alexander Couzens67725e22021-02-15 02:37:03 +010081 GPRS_SNS_EV_REQ_SELECT_ENDPOINT, /*!< Select a SNS endpoint from the list */
82 GPRS_SNS_EV_RX_SIZE,
83 GPRS_SNS_EV_RX_SIZE_ACK,
84 GPRS_SNS_EV_RX_CONFIG,
85 GPRS_SNS_EV_RX_CONFIG_END, /*!< SNS-CONFIG with end flag received */
86 GPRS_SNS_EV_RX_CONFIG_ACK,
87 GPRS_SNS_EV_RX_ADD,
88 GPRS_SNS_EV_RX_DELETE,
89 GPRS_SNS_EV_RX_CHANGE_WEIGHT,
Harald Welteb9f23872021-03-02 20:48:54 +010090 GPRS_SNS_EV_RX_ACK, /*!< Rx of SNS-ACK (response to ADD/DELETE/CHG_WEIGHT */
Harald Welte04647e12021-03-02 18:50:40 +010091 GPRS_SNS_EV_REQ_NO_NSVC, /*!< no more NS-VC remaining (all dead) */
Alexander Couzens67725e22021-02-15 02:37:03 +010092 GPRS_SNS_EV_REQ_NSVC_ALIVE, /*!< a NS-VC became alive */
Harald Welte04647e12021-03-02 18:50:40 +010093 GPRS_SNS_EV_REQ_ADD_BIND, /*!< add a new local bind to this NSE */
94 GPRS_SNS_EV_REQ_DELETE_BIND, /*!< remove a local bind from this NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +020095};
96
97static const struct value_string gprs_sns_event_names[] = {
Alexander Couzens67725e22021-02-15 02:37:03 +010098 { GPRS_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" },
99 { GPRS_SNS_EV_RX_SIZE, "RX_SIZE" },
100 { GPRS_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" },
101 { GPRS_SNS_EV_RX_CONFIG, "RX_CONFIG" },
102 { GPRS_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" },
103 { GPRS_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" },
104 { GPRS_SNS_EV_RX_ADD, "RX_ADD" },
105 { GPRS_SNS_EV_RX_DELETE, "RX_DELETE" },
Harald Welteb9f23872021-03-02 20:48:54 +0100106 { GPRS_SNS_EV_RX_ACK, "RX_ACK" },
Alexander Couzens67725e22021-02-15 02:37:03 +0100107 { GPRS_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" },
108 { GPRS_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" },
109 { GPRS_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"},
110 { GPRS_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"},
111 { GPRS_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200112 { 0, NULL }
113};
114
Alexander Couzense769f522020-12-07 07:37:07 +0100115struct sns_endpoint {
116 struct llist_head list;
117 struct osmo_sockaddr saddr;
118};
119
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100120struct ns2_sns_bind {
121 struct llist_head list;
122 struct gprs_ns2_vc_bind *bind;
123};
124
Alexander Couzens6a161492020-07-12 13:45:50 +0200125struct ns2_sns_state {
126 struct gprs_ns2_nse *nse;
127
128 enum ns2_sns_type ip;
Harald Welte4f127462021-03-02 20:49:10 +0100129 enum ns2_sns_role role; /* local role: BSS or SGSN */
Alexander Couzens6a161492020-07-12 13:45:50 +0200130
Alexander Couzense769f522020-12-07 07:37:07 +0100131 /* holds the list of initial SNS endpoints */
132 struct llist_head sns_endpoints;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100133 /* list of used struct ns2_sns_bind */
134 struct llist_head binds;
135 /* pointer to the bind which was used to initiate the SNS connection */
136 struct ns2_sns_bind *initial_bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100137 /* prevent recursive reselection */
138 bool reselection_running;
139
140 /* The current initial SNS endpoints.
141 * The initial connection will be moved into the NSE
142 * if configured via SNS. Otherwise it will be removed
143 * in configured state. */
144 struct sns_endpoint *initial;
Alexander Couzens6a161492020-07-12 13:45:50 +0200145 /* all SNS PDU will be sent over this nsvc */
146 struct gprs_ns2_vc *sns_nsvc;
Alexander Couzens90ee9632020-12-07 06:18:32 +0100147 /* timer N */
148 int N;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100149 /* true if at least one nsvc is alive */
150 bool alive;
Alexander Couzens6a161492020-07-12 13:45:50 +0200151
152 /* local configuration to send to the remote end */
153 struct gprs_ns_ie_ip4_elem *ip4_local;
154 size_t num_ip4_local;
155
156 /* local configuration to send to the remote end */
157 struct gprs_ns_ie_ip6_elem *ip6_local;
158 size_t num_ip6_local;
159
160 /* local configuration about our capabilities in terms of connections to
161 * remote (SGSN) side */
162 size_t num_max_nsvcs;
163 size_t num_max_ip4_remote;
164 size_t num_max_ip6_remote;
165
166 /* remote configuration as received */
167 struct gprs_ns_ie_ip4_elem *ip4_remote;
168 unsigned int num_ip4_remote;
169
170 /* remote configuration as received */
171 struct gprs_ns_ie_ip6_elem *ip6_remote;
172 unsigned int num_ip6_remote;
173};
174
175static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi)
176{
177 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
178 return gss->nse;
179}
180
181/* helper function to compute the sum of all (data or signaling) weights */
182static int ip4_weight_sum(const struct gprs_ns_ie_ip4_elem *ip4, unsigned int num,
183 bool data_weight)
184{
185 unsigned int i;
186 int weight_sum = 0;
187
188 for (i = 0; i < num; i++) {
189 if (data_weight)
190 weight_sum += ip4[i].data_weight;
191 else
192 weight_sum += ip4[i].sig_weight;
193 }
194 return weight_sum;
195}
196#define ip4_weight_sum_data(x,y) ip4_weight_sum(x, y, true)
197#define ip4_weight_sum_sig(x,y) ip4_weight_sum(x, y, false)
198
199/* helper function to compute the sum of all (data or signaling) weights */
200static int ip6_weight_sum(const struct gprs_ns_ie_ip6_elem *ip6, unsigned int num,
201 bool data_weight)
202{
203 unsigned int i;
204 int weight_sum = 0;
205
206 for (i = 0; i < num; i++) {
207 if (data_weight)
208 weight_sum += ip6[i].data_weight;
209 else
210 weight_sum += ip6[i].sig_weight;
211 }
212 return weight_sum;
213}
214#define ip6_weight_sum_data(x,y) ip6_weight_sum(x, y, true)
215#define ip6_weight_sum_sig(x,y) ip6_weight_sum(x, y, false)
216
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100217static int nss_weight_sum(const struct ns2_sns_state *nss, bool data_weight)
218{
219 return ip4_weight_sum(nss->ip4_remote, nss->num_ip4_remote, data_weight) +
220 ip6_weight_sum(nss->ip6_remote, nss->num_ip6_remote, data_weight);
221}
222#define nss_weight_sum_data(nss) nss_weight_sum(nss, true)
223#define nss_weight_sum_sig(nss) nss_weight_sum(nss, false)
224
Alexander Couzens6a161492020-07-12 13:45:50 +0200225static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
226 const struct gprs_ns_ie_ip4_elem *ip4)
227{
228 struct osmo_sockaddr sa;
229 /* copy over. Both data structures use network byte order */
230 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
231 sa.u.sin.sin_port = ip4->udp_port;
232 sa.u.sin.sin_family = AF_INET;
233
Alexander Couzens38b19e82020-09-23 23:56:37 +0200234 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200235}
236
237static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
238 const struct gprs_ns_ie_ip6_elem *ip6)
239{
240 struct osmo_sockaddr sa;
241 /* copy over. Both data structures use network byte order */
242 sa.u.sin6.sin6_addr = ip6->ip_addr;
243 sa.u.sin6.sin6_port = ip6->udp_port;
244 sa.u.sin6.sin6_family = AF_INET;
245
Alexander Couzens38b19e82020-09-23 23:56:37 +0200246 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200247}
248
Alexander Couzens125298f2020-10-11 21:22:42 +0200249/*! Return the initial SNS remote socket address
250 * \param nse NS Entity
251 * \return address of the initial SNS connection; NULL in case of error
252 */
253const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
254{
255 struct ns2_sns_state *gss;
256
257 if (!nse->bss_sns_fi)
258 return NULL;
259
260 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100261 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200262}
263
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100264/*! called when a nsvc is beeing freed or the nsvc became dead */
265void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200266{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100267 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200268 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100269 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200270 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200271
272 if (!fi)
273 return;
274
275 gss = (struct ns2_sns_state *) fi->priv;
276 if (nsvc != gss->sns_nsvc)
277 return;
278
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100279 gss->sns_nsvc = NULL;
280 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200281 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100282 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200283 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100284 return;
285 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200286 }
287 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100288 /* the SNS is waiting for its first NS-VC to come up
289 * choose any other nsvc */
290 llist_for_each_entry(tmp, &nse->nsvc, list) {
291 if (nsvc != tmp) {
292 gss->sns_nsvc = tmp;
293 return;
294 }
295 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200296 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100297
Alexander Couzens67725e22021-02-15 02:37:03 +0100298 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200299}
300
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100301static void ns2_clear_ipv46_entries(struct ns2_sns_state *gss)
302{
303 TALLOC_FREE(gss->ip4_local);
304 TALLOC_FREE(gss->ip4_remote);
305 TALLOC_FREE(gss->ip6_local);
306 TALLOC_FREE(gss->ip6_remote);
307
308 gss->num_ip4_local = 0;
309 gss->num_ip4_remote = 0;
310 gss->num_ip6_local = 0;
311 gss->num_ip6_remote = 0;
312}
313
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100314static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
315 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200316{
317 struct gprs_ns2_inst *nsi = nse->nsi;
318 struct gprs_ns2_vc *nsvc;
319 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100320
321 /* for every bind, create a connection if bind type == IP */
322 llist_for_each_entry(bind, &nsi->binding, list) {
323 if (bind->ll != GPRS_NS2_LL_UDP)
324 continue;
325 /* ignore failed connection */
326 nsvc = gprs_ns2_ip_connect_inactive(bind,
327 remote,
328 nse, 0);
329 if (!nsvc) {
330 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
331 continue;
332 }
333
334 nsvc->sig_weight = sig_weight;
335 nsvc->data_weight = data_weight;
336 }
337}
338
339static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
340 struct gprs_ns2_nse *nse,
341 const struct gprs_ns_ie_ip4_elem *ip4)
342{
Alexander Couzensc068d862020-10-12 04:11:51 +0200343 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200344 /* copy over. Both data structures use network byte order */
345 remote.u.sin.sin_family = AF_INET;
346 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
347 remote.u.sin.sin_port = ip4->udp_port;
348
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100349 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200350}
351
352static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
353 struct gprs_ns2_nse *nse,
354 const struct gprs_ns_ie_ip6_elem *ip6)
355{
Alexander Couzens6a161492020-07-12 13:45:50 +0200356 struct osmo_sockaddr remote = {};
357 /* copy over. Both data structures use network byte order */
358 remote.u.sin6.sin6_family = AF_INET6;
359 remote.u.sin6.sin6_addr = ip6->ip_addr;
360 remote.u.sin6.sin6_port = ip6->udp_port;
361
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100362 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200363}
364
365
366static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
367{
368 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
369 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
370 struct gprs_ns2_vc *nsvc;
371 struct gprs_ns2_vc_bind *bind;
372 struct osmo_sockaddr remote = { };
373 unsigned int i;
374
375 for (i = 0; i < gss->num_ip4_remote; i++) {
376 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->ip4_remote[i];
377
378 remote.u.sin.sin_family = AF_INET;
379 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
380 remote.u.sin.sin_port = ip4->udp_port;
381
382 llist_for_each_entry(bind, &nse->nsi->binding, list) {
383 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100384 if (bind->ll != GPRS_NS2_LL_UDP)
385 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200386
387 llist_for_each_entry(nsvc, &nse->nsvc, list) {
388 if (nsvc->bind != bind)
389 continue;
390
Alexander Couzensc4229a42020-10-11 20:58:04 +0200391 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200392 found = true;
393 break;
394 }
395 }
396
397 if (!found) {
398 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
399 if (!nsvc) {
400 /* TODO: add to a list to send back a NS-STATUS */
401 continue;
402 }
403 }
404
405 /* update data / signalling weight */
406 nsvc->data_weight = ip4->data_weight;
407 nsvc->sig_weight = ip4->sig_weight;
408 nsvc->sns_only = false;
409 }
410 }
411
412 for (i = 0; i < gss->num_ip6_remote; i++) {
413 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->ip6_remote[i];
414
415 remote.u.sin6.sin6_family = AF_INET6;
416 remote.u.sin6.sin6_addr = ip6->ip_addr;
417 remote.u.sin6.sin6_port = ip6->udp_port;
418
419 llist_for_each_entry(bind, &nse->nsi->binding, list) {
420 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100421 if (bind->ll != GPRS_NS2_LL_UDP)
422 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200423
424 llist_for_each_entry(nsvc, &nse->nsvc, list) {
425 if (nsvc->bind != bind)
426 continue;
427
Alexander Couzensc4229a42020-10-11 20:58:04 +0200428 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200429 found = true;
430 break;
431 }
432 }
433
434 if (!found) {
435 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
436 if (!nsvc) {
437 /* TODO: add to a list to send back a NS-STATUS */
438 continue;
439 }
440 }
441
442 /* update data / signalling weight */
443 nsvc->data_weight = ip6->data_weight;
444 nsvc->sig_weight = ip6->sig_weight;
445 nsvc->sns_only = false;
446 }
447 }
448
449
450 return 0;
451}
452
453/* Add a given remote IPv4 element to gprs_sns_state */
454static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
455{
456 unsigned int i;
457
458 if (gss->num_ip4_remote >= gss->num_max_ip4_remote)
459 return -NS_CAUSE_INVAL_NR_NS_VC;
460
461 /* check for duplicates */
462 for (i = 0; i < gss->num_ip4_remote; i++) {
463 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
464 continue;
465 /* TODO: log message duplicate */
Alexander Couzens6a161492020-07-12 13:45:50 +0200466 return -NS_CAUSE_PROTO_ERR_UNSPEC;
467 }
468
469 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem,
470 gss->num_ip4_remote+1);
471 gss->ip4_remote[gss->num_ip4_remote] = *ip4;
472 gss->num_ip4_remote += 1;
473 return 0;
474}
475
476/* Remove a given remote IPv4 element from gprs_sns_state */
477static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
478{
479 unsigned int i;
480
481 for (i = 0; i < gss->num_ip4_remote; i++) {
482 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
483 continue;
484 /* all array elements < i remain as they are; all > i are shifted left by one */
485 memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1);
486 gss->num_ip4_remote -= 1;
487 return 0;
488 }
489 return -1;
490}
491
492/* update the weights for specified remote IPv4 */
493static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
494{
495 unsigned int i;
496
497 for (i = 0; i < gss->num_ip4_remote; i++) {
498 if (gss->ip4_remote[i].ip_addr != ip4->ip_addr ||
499 gss->ip4_remote[i].udp_port != ip4->udp_port)
500 continue;
501
502 gss->ip4_remote[i].sig_weight = ip4->sig_weight;
503 gss->ip4_remote[i].data_weight = ip4->data_weight;
504 return 0;
505 }
506 return -1;
507}
508
509/* Add a given remote IPv6 element to gprs_sns_state */
510static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
511{
512 if (gss->num_ip6_remote >= gss->num_max_ip6_remote)
513 return -NS_CAUSE_INVAL_NR_NS_VC;
514
515 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, struct gprs_ns_ie_ip6_elem,
516 gss->num_ip6_remote+1);
517 gss->ip6_remote[gss->num_ip6_remote] = *ip6;
518 gss->num_ip6_remote += 1;
519 return 0;
520}
521
522/* Remove a given remote IPv6 element from gprs_sns_state */
523static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
524{
525 unsigned int i;
526
527 for (i = 0; i < gss->num_ip6_remote; i++) {
528 if (memcmp(&gss->ip6_remote[i], ip6, sizeof(*ip6)))
529 continue;
530 /* all array elements < i remain as they are; all > i are shifted left by one */
531 memmove(&gss->ip6_remote[i], &gss->ip6_remote[i+1], gss->num_ip6_remote-i-1);
532 gss->num_ip6_remote -= 1;
533 return 0;
534 }
535 return -1;
536}
537
538/* update the weights for specified remote IPv6 */
539static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
540{
541 unsigned int i;
542
543 for (i = 0; i < gss->num_ip6_remote; i++) {
544 if (memcmp(&gss->ip6_remote[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
545 gss->ip6_remote[i].udp_port != ip6->udp_port)
546 continue;
547 gss->ip6_remote[i].sig_weight = ip6->sig_weight;
548 gss->ip6_remote[i].data_weight = ip6->data_weight;
549 return 0;
550 }
551 return -1;
552}
553
554static int do_sns_change_weight(struct osmo_fsm_inst *fi, const struct gprs_ns_ie_ip4_elem *ip4, const struct gprs_ns_ie_ip6_elem *ip6)
555{
556 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
557 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
558 struct gprs_ns2_vc *nsvc;
559 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200560 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200561 uint8_t new_signal;
562 uint8_t new_data;
563
564 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
565 * signalling weights of all the peer IP endpoints configured for this NSE is
566 * equal to zero or if the resulting sum of the data weights of all the peer IP
567 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
568 * SNS-ACK PDU with a cause code of "Invalid weights". */
569
570 if (ip4) {
571 if (update_remote_ip4_elem(gss, ip4))
572 return -NS_CAUSE_UNKN_IP_EP;
573
574 /* copy over. Both data structures use network byte order */
575 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
576 sa.u.sin.sin_port = ip4->udp_port;
577 sa.u.sin.sin_family = AF_INET;
578 new_signal = ip4->sig_weight;
579 new_data = ip4->data_weight;
580 } else if (ip6) {
581 if (update_remote_ip6_elem(gss, ip6))
582 return -NS_CAUSE_UNKN_IP_EP;
583
584 /* copy over. Both data structures use network byte order */
585 sa.u.sin6.sin6_addr = ip6->ip_addr;
586 sa.u.sin6.sin6_port = ip6->udp_port;
587 sa.u.sin6.sin6_family = AF_INET6;
588 new_signal = ip6->sig_weight;
589 new_data = ip6->data_weight;
590 } else {
591 OSMO_ASSERT(false);
592 }
593
594 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200595 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200596 /* all nsvc in NSE should be IP/UDP nsvc */
597 OSMO_ASSERT(remote);
598
599 if (osmo_sockaddr_cmp(&sa, remote))
600 continue;
601
602 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
603 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
604 nsvc->sig_weight, new_signal);
605
606 nsvc->data_weight = new_data;
607 nsvc->sig_weight = new_signal;
608 }
609
610 return 0;
611}
612
613static int do_sns_delete(struct osmo_fsm_inst *fi,
614 const struct gprs_ns_ie_ip4_elem *ip4,
615 const struct gprs_ns_ie_ip6_elem *ip6)
616{
617 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
618 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
619 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200620 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200621 struct osmo_sockaddr sa = {};
622
623 if (ip4) {
624 if (remove_remote_ip4_elem(gss, ip4) < 0)
625 return -NS_CAUSE_UNKN_IP_EP;
626 /* copy over. Both data structures use network byte order */
627 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
628 sa.u.sin.sin_port = ip4->udp_port;
629 sa.u.sin.sin_family = AF_INET;
630 } else if (ip6) {
631 if (remove_remote_ip6_elem(gss, ip6))
632 return -NS_CAUSE_UNKN_IP_EP;
633
634 /* copy over. Both data structures use network byte order */
635 sa.u.sin6.sin6_addr = ip6->ip_addr;
636 sa.u.sin6.sin6_port = ip6->udp_port;
637 sa.u.sin6.sin6_family = AF_INET6;
638 } else {
639 OSMO_ASSERT(false);
640 }
641
642 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200643 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200644 /* all nsvc in NSE should be IP/UDP nsvc */
645 OSMO_ASSERT(remote);
646 if (osmo_sockaddr_cmp(&sa, remote))
647 continue;
648
649 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
650 gprs_ns2_free_nsvc(nsvc);
651 }
652
653 return 0;
654}
655
656static int do_sns_add(struct osmo_fsm_inst *fi,
657 const struct gprs_ns_ie_ip4_elem *ip4,
658 const struct gprs_ns_ie_ip6_elem *ip6)
659{
660 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
661 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
662 struct gprs_ns2_vc *nsvc;
663 int rc = 0;
664
665 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
666 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
667 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
668 switch (gss->ip) {
669 case IPv4:
670 rc = add_remote_ip4_elem(gss, ip4);
671 break;
672 case IPv6:
673 rc = add_remote_ip6_elem(gss, ip6);
674 break;
675 default:
676 /* the gss->ip is initialized with the bss */
677 OSMO_ASSERT(false);
678 }
679
680 if (rc)
681 return rc;
682
683 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
684 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
685 * unspecified" */
686 switch (gss->ip) {
687 case IPv4:
688 nsvc = nsvc_by_ip4_elem(nse, ip4);
689 if (nsvc) {
690 /* the nsvc should be already in sync with the ip4 / ip6 elements */
691 return -NS_CAUSE_PROTO_ERR_UNSPEC;
692 }
693
694 /* TODO: failure case */
695 ns2_nsvc_create_ip4(fi, nse, ip4);
696 break;
697 case IPv6:
698 nsvc = nsvc_by_ip6_elem(nse, ip6);
699 if (nsvc) {
700 /* the nsvc should be already in sync with the ip4 / ip6 elements */
701 return -NS_CAUSE_PROTO_ERR_UNSPEC;
702 }
703
704 /* TODO: failure case */
705 ns2_nsvc_create_ip6(fi, nse, ip6);
706 break;
707 }
708
709 gprs_ns2_start_alive_all_nsvcs(nse);
710
711 return 0;
712}
713
714
Harald Welte694dad52021-03-23 15:22:16 +0100715static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200716{
Harald Weltef61a9152021-03-02 22:20:17 +0100717 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
718 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzense769f522020-12-07 07:37:07 +0100719 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200720}
721
Harald Welte694dad52021-03-23 15:22:16 +0100722static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200723{
Harald Weltef61a9152021-03-02 22:20:17 +0100724 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200725 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
726 struct gprs_ns2_inst *nsi = nse->nsi;
727 struct tlv_parsed *tp = NULL;
728
Harald Weltef61a9152021-03-02 22:20:17 +0100729 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
730
Alexander Couzens6a161492020-07-12 13:45:50 +0200731 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100732 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200733 tp = data;
734 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
735 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
736 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
737 /* TODO: What to do? */
738 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100739 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200740 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
741 }
742 break;
743 default:
744 OSMO_ASSERT(0);
745 }
746}
747
Harald Welte24920e22021-03-04 13:03:27 +0100748static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200749{
750 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100751 struct gprs_ns_ie_ip4_elem *ip4_elems;
752 struct gprs_ns_ie_ip6_elem *ip6_elems;
753 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100754 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100755 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100756 const struct osmo_sockaddr *sa;
757 struct osmo_sockaddr local;
758 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200759
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100760 ns2_clear_ipv46_entries(gss);
761
Alexander Couzense769f522020-12-07 07:37:07 +0100762 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100763 if (gss->role == GPRS_SNS_ROLE_BSS) {
764 if (!gss->initial)
765 return;
766 remote = &gss->initial->saddr;
767 } else
768 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100769
770 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100771 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100772 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100773 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100774 return;
775 }
776
Alexander Couzense769f522020-12-07 07:37:07 +0100777 switch (gss->ip) {
778 case IPv4:
779 ip4_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip4_elem) * count);
780 if (!ip4_elems)
781 return;
782
783 gss->ip4_local = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100784 llist_for_each_entry(sbind, &gss->binds, list) {
785 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100786 sa = gprs_ns2_ip_bind_sockaddr(bind);
787 if (!sa)
788 continue;
789
790 if (sa->u.sas.ss_family != AF_INET)
791 continue;
792
793 /* check if this is an specific bind */
794 if (sa->u.sin.sin_addr.s_addr == 0) {
795 if (osmo_sockaddr_local_ip(&local, remote))
796 continue;
797
798 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
799 } else {
800 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
801 }
802
803 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100804 ip4_elems->sig_weight = bind->sns_sig_weight;
805 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100806 ip4_elems++;
807 }
808
809 gss->num_ip4_local = count;
810 gss->num_max_ip4_remote = 4;
811 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->num_ip4_local, 8);
812 break;
813 case IPv6:
814 /* IPv6 */
815 ip6_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip6_elem) * count);
816 if (!ip6_elems)
817 return;
818
819 gss->ip6_local = ip6_elems;
820
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100821 llist_for_each_entry(sbind, &gss->binds, list) {
822 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100823 sa = gprs_ns2_ip_bind_sockaddr(bind);
824 if (!sa)
825 continue;
826
827 if (sa->u.sas.ss_family != AF_INET6)
828 continue;
829
830 /* check if this is an specific bind */
831 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
832 if (osmo_sockaddr_local_ip(&local, remote))
833 continue;
834
835 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
836 } else {
837 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
838 }
839
840 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100841 ip6_elems->sig_weight = bind->sns_sig_weight;
842 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100843
844 ip6_elems++;
845 }
846 gss->num_ip6_local = count;
847 gss->num_max_ip6_remote = 4;
848 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->num_ip6_local, 8);
849 break;
850 }
Harald Welte24920e22021-03-04 13:03:27 +0100851}
852
853/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Harald Welte694dad52021-03-23 15:22:16 +0100854static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Harald Welte24920e22021-03-04 13:03:27 +0100855{
856 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
857
Harald Weltef61a9152021-03-02 22:20:17 +0100858 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
859
Harald Welte24920e22021-03-04 13:03:27 +0100860 /* on a generic failure, the timer callback will recover */
861 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
862 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Harald Welte694dad52021-03-23 15:22:16 +0100863 if (old_state != GPRS_SNS_ST_BSS_SIZE)
Harald Welte24920e22021-03-04 13:03:27 +0100864 gss->N = 0;
865
866 gss->alive = false;
867
868 ns2_sns_compute_local_ep_from_binds(fi);
869
870 /* take the first bind or take the next bind */
871 if (!gss->initial_bind) {
872 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
873 } else {
874 if (gss->initial_bind->list.next != &gss->binds) {
875 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
876 } else {
877 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
878 }
879 }
880
881
882 /* setup the NSVC */
883 if (!gss->sns_nsvc) {
884 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
885 struct osmo_sockaddr *remote = &gss->initial->saddr;
886 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
887 if (!gss->sns_nsvc)
888 return;
889 gss->sns_nsvc->sns_only = true;
890 }
891
Alexander Couzense769f522020-12-07 07:37:07 +0100892
Alexander Couzens6a161492020-07-12 13:45:50 +0200893 if (gss->num_max_ip4_remote > 0)
894 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->num_max_ip4_remote, -1);
895 else
896 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->num_max_ip6_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200897}
898
Harald Welte694dad52021-03-23 15:22:16 +0100899static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200900{
Harald Weltef61a9152021-03-02 22:20:17 +0100901 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens3df58862021-02-05 17:18:08 +0100902 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Harald Weltef61a9152021-03-02 22:20:17 +0100903 struct tlv_parsed *tp = NULL;
904
905 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzens6a161492020-07-12 13:45:50 +0200906
907 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100908 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200909 tp = (struct tlv_parsed *) data;
910 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
911 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
912 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
913 /* TODO: What to do? */
914 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100915 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_SGSN, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 3);
Alexander Couzens6a161492020-07-12 13:45:50 +0200916 }
917 break;
918 default:
919 OSMO_ASSERT(0);
920 }
921}
922
Harald Welte694dad52021-03-23 15:22:16 +0100923static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200924{
925 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100926
Harald Weltef61a9152021-03-02 22:20:17 +0100927 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
928
Harald Welte694dad52021-03-23 15:22:16 +0100929 if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS)
Alexander Couzens790a9632021-02-05 17:18:39 +0100930 gss->N = 0;
931
Alexander Couzens6a161492020-07-12 13:45:50 +0200932 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200933 switch (gss->ip) {
934 case IPv4:
935 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100936 gss->ip4_local, gss->num_ip4_local,
937 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200938 break;
939 case IPv6:
940 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100941 NULL, 0,
942 gss->ip6_local, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200943 break;
944 }
945}
946
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100947/* calculate the timeout of the configured state. the configured
948 * state will fail if not at least one NS-VC is alive within X second.
949 */
950static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
951{
952 int secs;
953 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
954 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
955 secs += nsi->timeout[NS_TOUT_TNS_TEST];
956
957 return secs;
958}
Alexander Couzens6a161492020-07-12 13:45:50 +0200959
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100960/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
961static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200962{
963 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200964
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100965 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
966 const struct gprs_ns_ie_ip4_elem *v4_list;
967 unsigned int num_v4;
968 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
969 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +0200970
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100971 if (num_v4 && gss->ip6_remote)
972 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +0200973
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100974 /* realloc to the new size */
975 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote,
976 struct gprs_ns_ie_ip4_elem,
977 gss->num_ip4_remote + num_v4);
978 /* append the new entries to the end of the list */
979 memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list));
980 gss->num_ip4_remote += num_v4;
981
982 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
983 gss->num_ip4_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200984 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200985
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100986 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
987 const struct gprs_ns_ie_ip6_elem *v6_list;
988 unsigned int num_v6;
989 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
990 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
991
992 if (num_v6 && gss->ip4_remote)
993 return -NS_CAUSE_INVAL_NR_IPv6_EP;
994
995 /* realloc to the new size */
996 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote,
997 struct gprs_ns_ie_ip6_elem,
998 gss->num_ip6_remote + num_v6);
999 /* append the new entries to the end of the list */
1000 memcpy(&gss->ip6_remote[gss->num_ip6_remote], v6_list, num_v6*sizeof(*v6_list));
1001 gss->num_ip6_remote += num_v6;
1002
1003 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %u entries\n",
1004 gss->num_ip6_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +02001005 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001006
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001007 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001008}
1009
Harald Welte694dad52021-03-23 15:22:16 +01001010static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens790a9632021-02-05 17:18:39 +01001011{
1012 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1013
Harald Weltef61a9152021-03-02 22:20:17 +01001014 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1015
Harald Welte694dad52021-03-23 15:22:16 +01001016 if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN)
Alexander Couzens790a9632021-02-05 17:18:39 +01001017 gss->N = 0;
1018}
1019
Harald Welte694dad52021-03-23 15:22:16 +01001020static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +02001021{
1022 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001023 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1024 uint8_t cause;
1025 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001026
Harald Weltef61a9152021-03-02 22:20:17 +01001027 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1028
Alexander Couzens6a161492020-07-12 13:45:50 +02001029 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001030 case GPRS_SNS_EV_RX_CONFIG_END:
1031 case GPRS_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001032 rc = ns_sns_append_remote_eps(fi, data);
1033 if (rc < 0) {
1034 cause = -rc;
1035 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1036 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1037 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001038 }
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001039 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1040 /* check if sum of data / sig weights == 0 */
1041 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1042 cause = NS_CAUSE_INVAL_WEIGH;
1043 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1044 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1045 return;
1046 }
1047 create_missing_nsvcs(fi);
1048 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1049 /* start the test procedure on ALL NSVCs! */
1050 gprs_ns2_start_alive_all_nsvcs(nse);
1051 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1052 } else {
1053 /* just send CONFIG-ACK */
1054 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1055 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001056 }
1057 break;
1058 default:
1059 OSMO_ASSERT(0);
1060 }
1061}
1062
Alexander Couzens67725e22021-02-15 02:37:03 +01001063/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001064static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1065 struct ns2_sns_state *gss,
1066 struct tlv_parsed *tp)
1067{
1068 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1069 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1070 int num_v4 = 0, num_v6 = 0;
1071 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001072 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001073 int rc = 0;
1074
1075 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1076 * check uniqueness within the lists (no doublicate entries)
1077 * check not-known-by-us and sent back a list of unknown/known values
1078 * (abnormal behaviour according to 48.016)
1079 */
1080
1081 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1082 if (gss->ip == IPv4) {
1083 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1084 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1085 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1086 return;
1087 }
1088
1089 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1090 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001091 for (i = 0; i < num_v4; i++) {
1092 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001093 rc = do_sns_add(fi, &v4_list[i], NULL);
1094 if (rc < 0) {
1095 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001096 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001097 do_sns_delete(fi, &v4_list[j], NULL);
1098 cause = -rc;
1099 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1100 break;
1101 }
1102 }
1103 } else { /* IPv6 */
1104 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1105 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1106 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1107 return;
1108 }
1109
1110 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1111 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001112 for (i = 0; i < num_v6; i++) {
1113 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001114 rc = do_sns_add(fi, NULL, &v6_list[i]);
1115 if (rc < 0) {
1116 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001117 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001118 do_sns_delete(fi, NULL, &v6_list[j]);
1119 cause = -rc;
1120 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1121 break;
1122 }
1123 }
1124 }
1125
1126 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1127 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1128}
1129
1130static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1131 struct ns2_sns_state *gss,
1132 struct tlv_parsed *tp)
1133{
1134 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1135 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1136 int num_v4 = 0, num_v6 = 0;
1137 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001138 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001139 int rc = 0;
1140
1141 /* TODO: split up delete into v4 + v6
1142 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1143 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1144 */
1145 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1146 if (gss->ip == IPv4) {
1147 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1148 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1149 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001150 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001151 rc = do_sns_delete(fi, &v4_list[i], NULL);
1152 if (rc < 0) {
1153 cause = -rc;
1154 /* continue to delete others */
1155 }
1156 }
1157 if (cause != 0xff) {
1158 /* TODO: create list of not-deleted and return it */
1159 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1160 return;
1161 }
1162
1163 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1164 /* delete all NS-VCs for given IPv4 address */
1165 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1166 struct gprs_ns_ie_ip4_elem *ip4_remote;
1167 uint32_t ip_addr = *(uint32_t *)(ie+1);
1168 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1169 cause = NS_CAUSE_UNKN_IP_ADDR;
1170 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1171 return;
1172 }
1173 /* make a copy as do_sns_delete() will change the array underneath us */
1174 ip4_remote = talloc_memdup(fi, gss->ip4_remote,
1175 gss->num_ip4_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001176 for (i = 0; i < gss->num_ip4_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001177 if (ip4_remote[i].ip_addr == ip_addr) {
1178 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1179 if (rc < 0) {
1180 cause = -rc;
1181 /* continue to delete others */
1182 }
1183 }
1184 }
1185 talloc_free(ip4_remote);
1186 if (cause != 0xff) {
1187 /* TODO: create list of not-deleted and return it */
1188 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1189 return;
1190 }
1191 } else {
1192 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1193 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1194 return;
1195 }
1196 } else { /* IPv6 */
1197 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1198 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1199 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001200 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001201 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1202 if (rc < 0) {
1203 cause = -rc;
1204 /* continue to delete others */
1205 }
1206 }
1207 if (cause != 0xff) {
1208 /* TODO: create list of not-deleted and return it */
1209 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1210 return;
1211 }
1212 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1213 /* delete all NS-VCs for given IPv4 address */
1214 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1215 struct gprs_ns_ie_ip6_elem *ip6_remote;
1216 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001217 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001218 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1219 cause = NS_CAUSE_UNKN_IP_ADDR;
1220 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1221 return;
1222 }
1223 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1224 /* make a copy as do_sns_delete() will change the array underneath us */
1225 ip6_remote = talloc_memdup(fi, gss->ip6_remote,
1226 gss->num_ip6_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001227 for (i = 0; i < gss->num_ip6_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001228 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1229 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1230 if (rc < 0) {
1231 cause = -rc;
1232 /* continue to delete others */
1233 }
1234 }
1235 }
1236
1237 talloc_free(ip6_remote);
1238 if (cause != 0xff) {
1239 /* TODO: create list of not-deleted and return it */
1240 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1241 return;
1242 }
1243 } else {
1244 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1245 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1246 return;
1247 }
1248 }
1249 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1250}
1251
1252static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1253 struct ns2_sns_state *gss,
1254 struct tlv_parsed *tp)
1255{
1256 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1257 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1258 int num_v4 = 0, num_v6 = 0;
1259 uint8_t trans_id, cause = 0xff;
1260 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001261 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001262
1263 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1264 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1265 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1266 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001267 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001268 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1269 if (rc < 0) {
1270 cause = -rc;
1271 /* continue to others */
1272 }
1273 }
1274 if (cause != 0xff) {
1275 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1276 return;
1277 }
1278 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1279 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1280 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001281 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001282 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1283 if (rc < 0) {
1284 cause = -rc;
1285 /* continue to others */
1286 }
1287 }
1288 if (cause != 0xff) {
1289 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1290 return;
1291 }
1292 } else {
1293 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1294 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1295 return;
1296 }
1297 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1298}
1299
1300static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1301{
1302 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1303 struct tlv_parsed *tp = data;
1304
1305 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001306 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001307 ns2_sns_st_configured_add(fi, gss, tp);
1308 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001309 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001310 ns2_sns_st_configured_delete(fi, gss, tp);
1311 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001312 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001313 ns2_sns_st_configured_change(fi, gss, tp);
1314 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001315 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001316 osmo_timer_del(&fi->timer);
1317 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001318 }
1319}
1320
1321static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1322{
1323 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens138b96f2021-01-25 16:23:29 +01001324 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001325}
1326
1327static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1328 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001329 .in_event_mask = 0, /* handled by all_state_action */
Harald Welte694dad52021-03-23 15:22:16 +01001330 .out_state_mask = S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001331 .name = "UNCONFIGURED",
Harald Welte694dad52021-03-23 15:22:16 +01001332 .action = ns2_sns_st_bss_unconfigured,
Alexander Couzens6a161492020-07-12 13:45:50 +02001333 },
Harald Welte694dad52021-03-23 15:22:16 +01001334 [GPRS_SNS_ST_BSS_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001335 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001336 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001337 S(GPRS_SNS_ST_BSS_SIZE) |
1338 S(GPRS_SNS_ST_BSS_CONFIG_BSS),
1339 .name = "BSS_SIZE",
1340 .action = ns2_sns_st_bss_size,
1341 .onenter = ns2_sns_st_bss_size_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001342 },
Harald Welte694dad52021-03-23 15:22:16 +01001343 [GPRS_SNS_ST_BSS_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001344 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001345 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001346 S(GPRS_SNS_ST_BSS_CONFIG_BSS) |
1347 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
1348 S(GPRS_SNS_ST_BSS_SIZE),
1349 .name = "BSS_CONFIG_BSS",
1350 .action = ns2_sns_st_bss_config_bss,
1351 .onenter = ns2_sns_st_bss_config_bss_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001352 },
Harald Welte694dad52021-03-23 15:22:16 +01001353 [GPRS_SNS_ST_BSS_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001354 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1355 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001356 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001357 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens6a161492020-07-12 13:45:50 +02001358 S(GPRS_SNS_ST_CONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001359 S(GPRS_SNS_ST_BSS_SIZE),
1360 .name = "BSS_CONFIG_SGSN",
1361 .action = ns2_sns_st_bss_config_sgsn,
1362 .onenter = ns2_sns_st_bss_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001363 },
1364 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001365 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1366 S(GPRS_SNS_EV_RX_DELETE) |
1367 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1368 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001369 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001370 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001371 .name = "CONFIGURED",
1372 .action = ns2_sns_st_configured,
1373 .onenter = ns2_sns_st_configured_onenter,
1374 },
1375};
1376
1377static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1378{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001379 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001380 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1381 struct gprs_ns2_inst *nsi = nse->nsi;
1382
Alexander Couzens90ee9632020-12-07 06:18:32 +01001383 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001384 switch (fi->T) {
1385 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001386 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1387 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001388 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001389 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001390 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_SIZE, nsi->timeout[NS_TOUT_TSNS_PROV], 1);
Alexander Couzensa367d082020-12-21 14:06:24 +01001391 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001392 break;
1393 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001394 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001395 LOGPFSML(fi, LOGL_ERROR, "NSE %d: BSS Config retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001396 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001397 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001398 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS, nsi->timeout[NS_TOUT_TSNS_PROV], 2);
Alexander Couzensa367d082020-12-21 14:06:24 +01001399 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001400 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001401 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001402 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1403 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001404 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001405 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001406 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_SGSN, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
Alexander Couzens3df58862021-02-05 17:18:08 +01001407 }
1408 break;
1409 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001410 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001411 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001412 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001413 }
1414 return 0;
1415}
1416
Harald Welte9e37bf42021-03-02 20:48:31 +01001417/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001418static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1419{
1420 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001421 struct ns2_sns_bind *sbind;
1422 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001423
Alexander Couzense769f522020-12-07 07:37:07 +01001424 switch (event) {
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001425 case GPRS_SNS_EV_REQ_ADD_BIND:
1426 sbind = data;
1427 switch (fi->state) {
1428 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001429 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001430 break;
Harald Welte694dad52021-03-23 15:22:16 +01001431 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001432 /* TODO: add the ip4 element to the list */
1433 break;
Harald Welte694dad52021-03-23 15:22:16 +01001434 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1435 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001436 case GPRS_SNS_ST_CONFIGURED:
1437 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1438 break;
1439 }
1440 break;
1441 case GPRS_SNS_EV_REQ_DELETE_BIND:
1442 sbind = data;
1443 switch (fi->state) {
1444 case GPRS_SNS_ST_UNCONFIGURED:
1445 break;
Harald Welte694dad52021-03-23 15:22:16 +01001446 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001447 /* TODO: remove the ip4 element from the list */
1448 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1449 if (nsvc->bind == sbind->bind) {
1450 gprs_ns2_free_nsvc(nsvc);
1451 }
1452 }
1453 break;
Harald Welte694dad52021-03-23 15:22:16 +01001454 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1455 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001456 case GPRS_SNS_ST_CONFIGURED:
1457 /* TODO: do an delete SNS-IP procedure */
1458 /* TODO: remove the ip4 element to the list */
1459 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1460 if (nsvc->bind == sbind->bind) {
1461 gprs_ns2_free_nsvc(nsvc);
1462 }
1463 }
1464 break;
1465 }
1466 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1467 talloc_free(sbind);
1468 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001469 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001470}
1471
Harald Welte9e37bf42021-03-02 20:48:31 +01001472/* allstate-action for BSS role */
1473static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1474{
1475 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1476 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1477
1478 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
1479 switch (event) {
1480 case GPRS_SNS_EV_REQ_NO_NSVC:
1481 /* ignore reselection running */
1482 if (gss->reselection_running)
1483 break;
1484
1485 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1486 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1487 break;
1488 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
1489 /* tear down previous state
1490 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1491 gss->reselection_running = true;
1492 gprs_ns2_free_nsvcs(nse);
1493 ns2_clear_ipv46_entries(gss);
1494
1495 /* Choose the next sns endpoint. */
1496 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds)) {
1497 gss->initial = NULL;
1498 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1499 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1500 return;
1501 } else if (!gss->initial) {
1502 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1503 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1504 /* last entry, continue with first */
1505 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1506 } else {
1507 /* next element is an entry */
1508 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1509 }
1510
1511 gss->reselection_running = false;
Harald Welte694dad52021-03-23 15:22:16 +01001512 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_SIZE, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 1);
Harald Welte9e37bf42021-03-02 20:48:31 +01001513 break;
1514 default:
1515 ns2_sns_st_all_action(fi, event, data);
1516 break;
1517 }
1518}
1519
Alexander Couzens6a161492020-07-12 13:45:50 +02001520static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1521 .name = "GPRS-NS2-SNS-BSS",
1522 .states = ns2_sns_bss_states,
1523 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001524 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1525 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001526 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1527 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001528 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001529 .cleanup = NULL,
1530 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001531 .event_names = gprs_sns_event_names,
1532 .pre_term = NULL,
1533 .log_subsys = DLNS,
1534};
1535
Harald Welte5bef2cc2020-09-18 22:33:24 +02001536/*! Allocate an IP-SNS FSM for the BSS side.
1537 * \param[in] nse NS Entity in which the FSM runs
1538 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001539 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001540struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1541 const char *id)
1542{
1543 struct osmo_fsm_inst *fi;
1544 struct ns2_sns_state *gss;
1545
1546 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1547 if (!fi)
1548 return fi;
1549
1550 gss = talloc_zero(fi, struct ns2_sns_state);
1551 if (!gss)
1552 goto err;
1553
1554 fi->priv = gss;
1555 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001556 gss->role = GPRS_SNS_ROLE_BSS;
Alexander Couzense769f522020-12-07 07:37:07 +01001557 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001558 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001559
1560 return fi;
1561err:
1562 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1563 return NULL;
1564}
1565
Harald Welte5bef2cc2020-09-18 22:33:24 +02001566/*! main entry point for receiving SNS messages from the network.
1567 * \param[in] nsvc NS-VC on which the message was received
1568 * \param[in] msg message buffer of the IP-SNS message
1569 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001570 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001571int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001572{
1573 struct gprs_ns2_nse *nse = nsvc->nse;
1574 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1575 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001576 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001577 struct osmo_fsm_inst *fi;
1578
1579 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001580 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1581 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001582 return -EINVAL;
1583 }
1584
Alexander Couzens6a161492020-07-12 13:45:50 +02001585 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1586 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001587 gss = (struct ns2_sns_state *) fi->priv;
1588 if (!gss->sns_nsvc)
1589 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001590
Harald Weltef2949742021-01-20 14:54:14 +01001591 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1592 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1593
Alexander Couzens6a161492020-07-12 13:45:50 +02001594 switch (nsh->pdu_type) {
1595 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001596 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001597 break;
1598 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001599 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001600 break;
1601 case SNS_PDUT_CONFIG:
1602 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001603 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001604 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001605 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001606 break;
1607 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001608 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001609 break;
1610 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001611 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001612 break;
1613 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001614 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001615 break;
1616 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001617 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001618 break;
1619 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001620 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001621 break;
1622 default:
Harald Weltef2949742021-01-20 14:54:14 +01001623 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1624 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001625 return -EINVAL;
1626 }
1627
1628 return 0;
1629}
1630
1631#include <osmocom/vty/vty.h>
1632#include <osmocom/vty/misc.h>
1633
Harald Welte1262c4f2021-01-19 20:58:33 +01001634static void vty_dump_sns_ip4(struct vty *vty, const char *prefix, const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +02001635{
1636 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001637 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001638 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1639}
1640
Harald Welte1262c4f2021-01-19 20:58:33 +01001641static void vty_dump_sns_ip6(struct vty *vty, const char *prefix, const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +02001642{
1643 char ip_addr[INET6_ADDRSTRLEN] = {};
1644 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1645 strcpy(ip_addr, "Invalid IPv6");
1646
Harald Welte1262c4f2021-01-19 20:58:33 +01001647 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001648 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1649}
1650
Harald Welte5bef2cc2020-09-18 22:33:24 +02001651/*! Dump the IP-SNS state to a vty.
1652 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001653 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001654 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1655 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001656void ns2_sns_dump_vty(struct vty *vty, const char *prefix, const struct gprs_ns2_nse *nse, bool stats)
Alexander Couzens6a161492020-07-12 13:45:50 +02001657{
1658 struct ns2_sns_state *gss;
1659 unsigned int i;
1660
1661 if (!nse->bss_sns_fi)
1662 return;
1663
Harald Welte1262c4f2021-01-19 20:58:33 +01001664 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001665 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1666
Harald Welte1262c4f2021-01-19 20:58:33 +01001667 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1668 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001669
1670 if (gss->num_ip4_local && gss->num_ip4_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001671 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001672 for (i = 0; i < gss->num_ip4_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001673 vty_dump_sns_ip4(vty, prefix, &gss->ip4_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001674
Harald Welte1262c4f2021-01-19 20:58:33 +01001675 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001676 for (i = 0; i < gss->num_ip4_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001677 vty_dump_sns_ip4(vty, prefix, &gss->ip4_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001678 }
1679
1680 if (gss->num_ip6_local && gss->num_ip6_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001681 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001682 for (i = 0; i < gss->num_ip6_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001683 vty_dump_sns_ip6(vty, prefix, &gss->ip6_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001684
Harald Welte1262c4f2021-01-19 20:58:33 +01001685 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001686 for (i = 0; i < gss->num_ip6_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001687 vty_dump_sns_ip6(vty, prefix, &gss->ip6_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001688 }
1689}
1690
Alexander Couzens412bc342020-11-19 05:24:37 +01001691/*! write IP-SNS to a vty
1692 * \param[in] vty VTY to which the state shall be printed
1693 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001694void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001695{
1696 struct ns2_sns_state *gss;
1697 struct osmo_sockaddr_str addr_str;
1698 struct sns_endpoint *endpoint;
1699
1700 if (!nse->bss_sns_fi)
1701 return;
1702
1703 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1704 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001705 /* It's unlikely that an error happens, but let's better be safe. */
1706 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1707 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001708 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001709 }
1710}
1711
Alexander Couzense769f522020-12-07 07:37:07 +01001712static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1713 const struct osmo_sockaddr *saddr)
1714{
1715 struct sns_endpoint *endpoint;
1716
1717 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1718 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1719 return endpoint;
1720 }
1721
1722 return NULL;
1723}
1724
1725/*! gprs_ns2_sns_add_endpoint
1726 * \param[in] nse
1727 * \param[in] sockaddr
1728 * \return
1729 */
1730int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1731 const struct osmo_sockaddr *saddr)
1732{
1733 struct ns2_sns_state *gss;
1734 struct sns_endpoint *endpoint;
1735 bool do_selection = false;
1736
1737 if (nse->ll != GPRS_NS2_LL_UDP) {
1738 return -EINVAL;
1739 }
1740
Alexander Couzens138b96f2021-01-25 16:23:29 +01001741 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001742 return -EINVAL;
1743 }
1744
1745 gss = nse->bss_sns_fi->priv;
1746
1747 if (ns2_get_sns_endpoint(gss, saddr))
1748 return -EADDRINUSE;
1749
1750 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1751 if (!endpoint)
1752 return -ENOMEM;
1753
1754 endpoint->saddr = *saddr;
1755 if (llist_empty(&gss->sns_endpoints))
1756 do_selection = true;
1757
1758 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1759 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001760 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001761
1762 return 0;
1763}
1764
1765/*! gprs_ns2_sns_del_endpoint
1766 * \param[in] nse
1767 * \param[in] sockaddr
1768 * \return 0 on success, otherwise < 0
1769 */
1770int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1771 const struct osmo_sockaddr *saddr)
1772{
1773 struct ns2_sns_state *gss;
1774 struct sns_endpoint *endpoint;
1775
1776 if (nse->ll != GPRS_NS2_LL_UDP) {
1777 return -EINVAL;
1778 }
1779
Alexander Couzens138b96f2021-01-25 16:23:29 +01001780 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001781 return -EINVAL;
1782 }
1783
1784 gss = nse->bss_sns_fi->priv;
1785 endpoint = ns2_get_sns_endpoint(gss, saddr);
1786 if (!endpoint)
1787 return -ENOENT;
1788
1789 /* if this is an unused SNS endpoint it's done */
1790 if (gss->initial != endpoint) {
1791 llist_del(&endpoint->list);
1792 talloc_free(endpoint);
1793 return 0;
1794 }
1795
Alexander Couzens67725e22021-02-15 02:37:03 +01001796 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001797 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001798 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001799 "Closing all NS-VC and restart SNS-SIZE procedure"
1800 "with a remaining SNS endpoint.\n");
1801
1802 /* Continue with the next endpoint in the list.
1803 * Special case if the endpoint is at the start or end of the list */
1804 if (endpoint->list.prev == &gss->sns_endpoints ||
1805 endpoint->list.next == &gss->sns_endpoints)
1806 gss->initial = NULL;
1807 else
1808 gss->initial = llist_entry(endpoint->list.next->prev,
1809 struct sns_endpoint,
1810 list);
1811
1812 llist_del(&endpoint->list);
1813 gprs_ns2_free_nsvcs(nse);
1814 talloc_free(endpoint);
1815
1816 return 0;
1817}
1818
1819/*! gprs_ns2_sns_count
1820 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1821 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1822 */
1823int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1824{
1825 struct ns2_sns_state *gss;
1826 struct sns_endpoint *endpoint;
1827 int count = 0;
1828
1829 if (nse->ll != GPRS_NS2_LL_UDP) {
1830 return -EINVAL;
1831 }
1832
Alexander Couzens138b96f2021-01-25 16:23:29 +01001833 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001834 return -EINVAL;
1835 }
1836
1837 gss = nse->bss_sns_fi->priv;
1838 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1839 count++;
1840
1841 return count;
1842}
1843
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001844void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1845{
1846 struct ns2_sns_state *gss;
1847 struct gprs_ns2_vc *tmp;
1848
1849 if (!nse->bss_sns_fi)
1850 return;
1851
1852 gss = nse->bss_sns_fi->priv;
1853 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1854 return;
1855
1856 if (alive == gss->alive)
1857 return;
1858
1859 /* check if this is the current SNS NS-VC */
1860 if (nsvc == gss->sns_nsvc) {
1861 /* only replace the SNS NS-VC if there are other alive NS-VC.
1862 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1863 * and couldn't confirm yet if the NS-VC comes up */
1864 if (gss->alive && !alive)
1865 ns2_sns_replace_nsvc(nsvc);
1866 }
1867
1868 if (alive) {
1869 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001870 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001871 } else {
1872 /* is there at least another alive nsvc? */
1873 llist_for_each_entry(tmp, &nse->nsvc, list) {
1874 if (ns2_vc_is_unblocked(tmp))
1875 return;
1876 }
1877
1878 /* all NS-VC have failed */
1879 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001880 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001881 }
1882}
1883
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001884int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1885 struct gprs_ns2_vc_bind *bind)
1886{
1887 struct ns2_sns_state *gss;
1888 struct ns2_sns_bind *tmp;
1889
1890 OSMO_ASSERT(nse->bss_sns_fi);
1891 gss = nse->bss_sns_fi->priv;
1892
1893 if (!gprs_ns2_is_ip_bind(bind)) {
1894 return -EINVAL;
1895 }
1896
1897 if (!llist_empty(&gss->binds)) {
1898 llist_for_each_entry(tmp, &gss->binds, list) {
1899 if (tmp->bind == bind)
1900 return -EALREADY;
1901 }
1902 }
1903
1904 tmp = talloc_zero(gss, struct ns2_sns_bind);
1905 if (!tmp)
1906 return -ENOMEM;
1907 tmp->bind = bind;
1908 llist_add_tail(&tmp->list, &gss->binds);
1909
1910 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
1911 return 0;
1912}
1913
1914/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
1915int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
1916 struct gprs_ns2_vc_bind *bind)
1917{
1918 struct ns2_sns_state *gss;
1919 struct ns2_sns_bind *tmp, *tmp2;
1920 bool found = false;
1921
1922 if (!nse->bss_sns_fi)
1923 return -EINVAL;
1924
1925 gss = nse->bss_sns_fi->priv;
1926 if (gss->initial_bind && gss->initial_bind->bind == bind) {
1927 if (gss->initial_bind->list.prev == &gss->binds)
1928 gss->initial_bind = NULL;
1929 else
1930 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
1931 }
1932
1933 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
1934 if (tmp->bind == bind) {
1935 llist_del(&tmp->list);
1936 found = true;
1937 }
1938 }
1939
1940 if (!found)
1941 return -ENOENT;
1942
1943 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
1944 return 0;
1945}
1946
Alexander Couzensc4704762021-02-08 23:13:12 +01001947/* Update SNS weights
1948 * \param[in] nsvc the NSVC which should be updated
1949 */
1950void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
1951{
1952 /* TODO: implement weights after binds per sns implemented */
1953}
1954
Harald Welte4f127462021-03-02 20:49:10 +01001955
1956
1957
1958/***********************************************************************
1959 * SGSN role
1960 ***********************************************************************/
1961
1962static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1963{
1964 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1965 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
1966 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
1967}
1968
1969/* We're waiting for inbound SNS-CONFIG from the BSS */
1970static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1971{
1972 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1973 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1974 struct gprs_ns2_inst *nsi = nse->nsi;
1975 uint8_t cause;
1976 int rc;
1977
1978 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
1979
1980 switch (event) {
1981 case GPRS_SNS_EV_RX_CONFIG:
1982 case GPRS_SNS_EV_RX_CONFIG_END:
1983 rc = ns_sns_append_remote_eps(fi, data);
1984 if (rc < 0) {
1985 cause = -rc;
1986 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1987 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1988 return;
1989 }
1990 /* only change state if last CONFIG was received */
1991 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1992 /* ensure sum of data weight / sig weights is > 0 */
1993 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1994 cause = NS_CAUSE_INVAL_WEIGH;
1995 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1996 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1997 break;
1998 }
1999 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2000 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2001 } else {
2002 /* just send CONFIG-ACK */
2003 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2004 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2005 }
2006 break;
2007 }
2008}
2009
2010static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2011{
2012 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2013 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2014
2015 ns2_sns_compute_local_ep_from_binds(fi);
2016 /* transmit SGSN-oriented SNS-CONFIG */
2017 ns2_tx_sns_config(gss->sns_nsvc, true, gss->ip4_local, gss->num_ip4_local,
2018 gss->ip6_local, gss->num_ip6_local);
2019}
2020
2021/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2022static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2023{
2024 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2025 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2026 struct tlv_parsed *tp = NULL;
2027
2028 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2029
2030 switch (event) {
2031 case GPRS_SNS_EV_RX_CONFIG_ACK:
2032 tp = data;
2033 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2034 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2035 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2036 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2037 break;
2038 }
2039 /* we currently only send one SNS-CONFIG with END FLAG */
2040 if (true) {
2041 create_missing_nsvcs(fi);
2042 /* start the test procedure on ALL NSVCs! */
2043 gprs_ns2_start_alive_all_nsvcs(nse);
2044 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2045 }
2046 break;
2047 }
2048}
2049
2050/* SGSN-side SNS state machine */
2051static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2052 [GPRS_SNS_ST_UNCONFIGURED] = {
2053 .in_event_mask = 0, /* handled by all_state_action */
2054 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2055 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2056 .name = "UNCONFIGURED",
2057 .action = ns2_sns_st_sgsn_unconfigured,
2058 },
2059 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
2060 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
2061 S(GPRS_SNS_EV_RX_CONFIG_END),
2062 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2063 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2064 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2065 .name = "SGSN_WAIT_CONFIG",
2066 .action = ns2_sns_st_sgsn_wait_config,
2067 },
2068 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
2069 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
2070 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2071 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2072 S(GPRS_SNS_ST_CONFIGURED),
2073 .name = "SGSN_WAIT_CONFIG_ACK",
2074 .action = ns2_sns_st_sgsn_wait_config_ack,
2075 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2076 },
2077 [GPRS_SNS_ST_CONFIGURED] = {
2078 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
2079 S(GPRS_SNS_EV_RX_DELETE) |
2080 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
2081 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
2082 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2083 .name = "CONFIGURED",
2084 /* shared with BSS side; once configured there's no difference */
2085 .action = ns2_sns_st_configured,
2086 .onenter = ns2_sns_st_configured_onenter,
2087 },
2088};
2089
2090static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2091{
2092 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2093 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2094 struct gprs_ns2_inst *nsi = nse->nsi;
2095
2096 gss->N++;
2097 switch (fi->T) {
2098 case 3:
2099 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2100 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2101 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2102 } else {
2103 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2104 }
2105 break;
2106 case 4:
2107 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2108 break;
2109 }
2110 return 0;
2111}
2112
2113
2114/* allstate-action for SGSN role */
2115static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2116{
2117 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2118 struct tlv_parsed *tp = NULL;
2119 uint8_t flag;
2120
2121 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2122
2123 switch (event) {
2124 case GPRS_SNS_EV_RX_SIZE:
2125 tp = (struct tlv_parsed *) data;
2126 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1)) {
2127 uint8_t cause = NS_CAUSE_MISSING_ESSENT_IE;
2128 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2129 break;
2130 }
2131 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2132 if (flag & 1) {
2133 struct gprs_ns2_vc *nsvc, *nsvc2;
2134 /* clear all state */
2135 gss->N = 0;
2136 ns2_clear_ipv46_entries(gss);
2137 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2138 if (nsvc == gss->sns_nsvc) {
2139 /* keep the NSVC we need for SNS, but unconfigure it */
2140 nsvc->sig_weight = 0;
2141 nsvc->data_weight = 0;
2142 ns2_vc_force_unconfigured(nsvc);
2143 } else {
2144 /* free all other NS-VCs */
2145 gprs_ns2_free_nsvc(nsvc);
2146 }
2147 }
2148 }
2149 /* send SIZE_ACK */
2150 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2151 /* only wait for SNS-CONFIG in case of Reset flag */
2152 if (flag & 1)
2153 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2154 break;
2155 default:
2156 ns2_sns_st_all_action(fi, event, data);
2157 break;
2158 }
2159}
2160
2161static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2162 .name = "GPRS-NS2-SNS-SGSN",
2163 .states = ns2_sns_sgsn_states,
2164 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
2165 .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) |
2166 S(GPRS_SNS_EV_REQ_NO_NSVC) |
2167 S(GPRS_SNS_EV_REQ_ADD_BIND) |
2168 S(GPRS_SNS_EV_REQ_DELETE_BIND),
2169 .allstate_action = ns2_sns_st_all_action_sgsn,
2170 .cleanup = NULL,
2171 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2172 .event_names = gprs_sns_event_names,
2173 .pre_term = NULL,
2174 .log_subsys = DLNS,
2175};
2176
2177/*! Allocate an IP-SNS FSM for the SGSN side.
2178 * \param[in] nse NS Entity in which the FSM runs
2179 * \param[in] id string identifier
2180 * \returns FSM instance on success; NULL on error */
2181struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2182{
2183 struct osmo_fsm_inst *fi;
2184 struct ns2_sns_state *gss;
2185
2186 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2187 if (!fi)
2188 return fi;
2189
2190 gss = talloc_zero(fi, struct ns2_sns_state);
2191 if (!gss)
2192 goto err;
2193
2194 fi->priv = gss;
2195 gss->nse = nse;
2196 gss->role = GPRS_SNS_ROLE_SGSN;
2197 INIT_LLIST_HEAD(&gss->sns_endpoints);
2198 INIT_LLIST_HEAD(&gss->binds);
2199
2200 return fi;
2201err:
2202 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2203 return NULL;
2204}
2205
2206
2207
2208
Alexander Couzens6a161492020-07-12 13:45:50 +02002209/* initialize osmo_ctx on main tread */
2210static __attribute__((constructor)) void on_dso_load_ctx(void)
2211{
2212 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002213 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002214}