blob: 51b3ea89381c98a0c38a6d9bf3451cad138a14a3 [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
Alexander Couzens6a161492020-07-12 13:45:50 +020069enum gprs_sns_bss_state {
70 GPRS_SNS_ST_UNCONFIGURED,
71 GPRS_SNS_ST_SIZE, /*!< SNS-SIZE procedure ongoing */
72 GPRS_SNS_ST_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */
73 GPRS_SNS_ST_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */
74 GPRS_SNS_ST_CONFIGURED,
Harald Welte4f127462021-03-02 20:49:10 +010075 GPRS_SNS_ST_SGSN_WAIT_CONFIG, /* !< SGSN role: Wait for CONFIG from BSS */
76 GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, /* !< SGSN role: Wait for CONFIG-ACK from BSS */
Alexander Couzens6a161492020-07-12 13:45:50 +020077};
78
79enum gprs_sns_event {
Alexander Couzens67725e22021-02-15 02:37:03 +010080 GPRS_SNS_EV_REQ_SELECT_ENDPOINT, /*!< Select a SNS endpoint from the list */
81 GPRS_SNS_EV_RX_SIZE,
82 GPRS_SNS_EV_RX_SIZE_ACK,
83 GPRS_SNS_EV_RX_CONFIG,
84 GPRS_SNS_EV_RX_CONFIG_END, /*!< SNS-CONFIG with end flag received */
85 GPRS_SNS_EV_RX_CONFIG_ACK,
86 GPRS_SNS_EV_RX_ADD,
87 GPRS_SNS_EV_RX_DELETE,
88 GPRS_SNS_EV_RX_CHANGE_WEIGHT,
Harald Welteb9f23872021-03-02 20:48:54 +010089 GPRS_SNS_EV_RX_ACK, /*!< Rx of SNS-ACK (response to ADD/DELETE/CHG_WEIGHT */
Harald Welte04647e12021-03-02 18:50:40 +010090 GPRS_SNS_EV_REQ_NO_NSVC, /*!< no more NS-VC remaining (all dead) */
Alexander Couzens67725e22021-02-15 02:37:03 +010091 GPRS_SNS_EV_REQ_NSVC_ALIVE, /*!< a NS-VC became alive */
Harald Welte04647e12021-03-02 18:50:40 +010092 GPRS_SNS_EV_REQ_ADD_BIND, /*!< add a new local bind to this NSE */
93 GPRS_SNS_EV_REQ_DELETE_BIND, /*!< remove a local bind from this NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +020094};
95
96static const struct value_string gprs_sns_event_names[] = {
Alexander Couzens67725e22021-02-15 02:37:03 +010097 { GPRS_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" },
98 { GPRS_SNS_EV_RX_SIZE, "RX_SIZE" },
99 { GPRS_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" },
100 { GPRS_SNS_EV_RX_CONFIG, "RX_CONFIG" },
101 { GPRS_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" },
102 { GPRS_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" },
103 { GPRS_SNS_EV_RX_ADD, "RX_ADD" },
104 { GPRS_SNS_EV_RX_DELETE, "RX_DELETE" },
Harald Welteb9f23872021-03-02 20:48:54 +0100105 { GPRS_SNS_EV_RX_ACK, "RX_ACK" },
Alexander Couzens67725e22021-02-15 02:37:03 +0100106 { GPRS_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" },
107 { GPRS_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" },
108 { GPRS_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"},
109 { GPRS_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"},
110 { GPRS_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200111 { 0, NULL }
112};
113
Alexander Couzense769f522020-12-07 07:37:07 +0100114struct sns_endpoint {
115 struct llist_head list;
116 struct osmo_sockaddr saddr;
117};
118
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100119struct ns2_sns_bind {
120 struct llist_head list;
121 struct gprs_ns2_vc_bind *bind;
122};
123
Alexander Couzens6a161492020-07-12 13:45:50 +0200124struct ns2_sns_state {
125 struct gprs_ns2_nse *nse;
126
127 enum ns2_sns_type ip;
Harald Welte4f127462021-03-02 20:49:10 +0100128 enum ns2_sns_role role; /* local role: BSS or SGSN */
Alexander Couzens6a161492020-07-12 13:45:50 +0200129
Alexander Couzense769f522020-12-07 07:37:07 +0100130 /* holds the list of initial SNS endpoints */
131 struct llist_head sns_endpoints;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100132 /* list of used struct ns2_sns_bind */
133 struct llist_head binds;
134 /* pointer to the bind which was used to initiate the SNS connection */
135 struct ns2_sns_bind *initial_bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100136 /* prevent recursive reselection */
137 bool reselection_running;
138
139 /* The current initial SNS endpoints.
140 * The initial connection will be moved into the NSE
141 * if configured via SNS. Otherwise it will be removed
142 * in configured state. */
143 struct sns_endpoint *initial;
Alexander Couzens6a161492020-07-12 13:45:50 +0200144 /* all SNS PDU will be sent over this nsvc */
145 struct gprs_ns2_vc *sns_nsvc;
Alexander Couzens90ee9632020-12-07 06:18:32 +0100146 /* timer N */
147 int N;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100148 /* true if at least one nsvc is alive */
149 bool alive;
Alexander Couzens6a161492020-07-12 13:45:50 +0200150
151 /* local configuration to send to the remote end */
152 struct gprs_ns_ie_ip4_elem *ip4_local;
153 size_t num_ip4_local;
154
155 /* local configuration to send to the remote end */
156 struct gprs_ns_ie_ip6_elem *ip6_local;
157 size_t num_ip6_local;
158
159 /* local configuration about our capabilities in terms of connections to
160 * remote (SGSN) side */
161 size_t num_max_nsvcs;
162 size_t num_max_ip4_remote;
163 size_t num_max_ip6_remote;
164
165 /* remote configuration as received */
166 struct gprs_ns_ie_ip4_elem *ip4_remote;
167 unsigned int num_ip4_remote;
168
169 /* remote configuration as received */
170 struct gprs_ns_ie_ip6_elem *ip6_remote;
171 unsigned int num_ip6_remote;
172};
173
174static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi)
175{
176 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
177 return gss->nse;
178}
179
180/* helper function to compute the sum of all (data or signaling) weights */
181static int ip4_weight_sum(const struct gprs_ns_ie_ip4_elem *ip4, unsigned int num,
182 bool data_weight)
183{
184 unsigned int i;
185 int weight_sum = 0;
186
187 for (i = 0; i < num; i++) {
188 if (data_weight)
189 weight_sum += ip4[i].data_weight;
190 else
191 weight_sum += ip4[i].sig_weight;
192 }
193 return weight_sum;
194}
195#define ip4_weight_sum_data(x,y) ip4_weight_sum(x, y, true)
196#define ip4_weight_sum_sig(x,y) ip4_weight_sum(x, y, false)
197
198/* helper function to compute the sum of all (data or signaling) weights */
199static int ip6_weight_sum(const struct gprs_ns_ie_ip6_elem *ip6, unsigned int num,
200 bool data_weight)
201{
202 unsigned int i;
203 int weight_sum = 0;
204
205 for (i = 0; i < num; i++) {
206 if (data_weight)
207 weight_sum += ip6[i].data_weight;
208 else
209 weight_sum += ip6[i].sig_weight;
210 }
211 return weight_sum;
212}
213#define ip6_weight_sum_data(x,y) ip6_weight_sum(x, y, true)
214#define ip6_weight_sum_sig(x,y) ip6_weight_sum(x, y, false)
215
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100216static int nss_weight_sum(const struct ns2_sns_state *nss, bool data_weight)
217{
218 return ip4_weight_sum(nss->ip4_remote, nss->num_ip4_remote, data_weight) +
219 ip6_weight_sum(nss->ip6_remote, nss->num_ip6_remote, data_weight);
220}
221#define nss_weight_sum_data(nss) nss_weight_sum(nss, true)
222#define nss_weight_sum_sig(nss) nss_weight_sum(nss, false)
223
Alexander Couzens6a161492020-07-12 13:45:50 +0200224static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
225 const struct gprs_ns_ie_ip4_elem *ip4)
226{
227 struct osmo_sockaddr sa;
228 /* copy over. Both data structures use network byte order */
229 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
230 sa.u.sin.sin_port = ip4->udp_port;
231 sa.u.sin.sin_family = AF_INET;
232
Alexander Couzens38b19e82020-09-23 23:56:37 +0200233 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200234}
235
236static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
237 const struct gprs_ns_ie_ip6_elem *ip6)
238{
239 struct osmo_sockaddr sa;
240 /* copy over. Both data structures use network byte order */
241 sa.u.sin6.sin6_addr = ip6->ip_addr;
242 sa.u.sin6.sin6_port = ip6->udp_port;
243 sa.u.sin6.sin6_family = AF_INET;
244
Alexander Couzens38b19e82020-09-23 23:56:37 +0200245 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200246}
247
Alexander Couzens125298f2020-10-11 21:22:42 +0200248/*! Return the initial SNS remote socket address
249 * \param nse NS Entity
250 * \return address of the initial SNS connection; NULL in case of error
251 */
252const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
253{
254 struct ns2_sns_state *gss;
255
256 if (!nse->bss_sns_fi)
257 return NULL;
258
259 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100260 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200261}
262
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100263/*! called when a nsvc is beeing freed or the nsvc became dead */
264void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200265{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100266 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200267 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100268 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200269 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200270
271 if (!fi)
272 return;
273
274 gss = (struct ns2_sns_state *) fi->priv;
275 if (nsvc != gss->sns_nsvc)
276 return;
277
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100278 gss->sns_nsvc = NULL;
279 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200280 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100281 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200282 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100283 return;
284 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200285 }
286 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100287 /* the SNS is waiting for its first NS-VC to come up
288 * choose any other nsvc */
289 llist_for_each_entry(tmp, &nse->nsvc, list) {
290 if (nsvc != tmp) {
291 gss->sns_nsvc = tmp;
292 return;
293 }
294 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200295 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100296
Alexander Couzens67725e22021-02-15 02:37:03 +0100297 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200298}
299
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100300static void ns2_clear_ipv46_entries(struct ns2_sns_state *gss)
301{
302 TALLOC_FREE(gss->ip4_local);
303 TALLOC_FREE(gss->ip4_remote);
304 TALLOC_FREE(gss->ip6_local);
305 TALLOC_FREE(gss->ip6_remote);
306
307 gss->num_ip4_local = 0;
308 gss->num_ip4_remote = 0;
309 gss->num_ip6_local = 0;
310 gss->num_ip6_remote = 0;
311}
312
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100313static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
314 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200315{
316 struct gprs_ns2_inst *nsi = nse->nsi;
317 struct gprs_ns2_vc *nsvc;
318 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100319
320 /* for every bind, create a connection if bind type == IP */
321 llist_for_each_entry(bind, &nsi->binding, list) {
322 if (bind->ll != GPRS_NS2_LL_UDP)
323 continue;
324 /* ignore failed connection */
325 nsvc = gprs_ns2_ip_connect_inactive(bind,
326 remote,
327 nse, 0);
328 if (!nsvc) {
329 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
330 continue;
331 }
332
333 nsvc->sig_weight = sig_weight;
334 nsvc->data_weight = data_weight;
335 }
336}
337
338static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
339 struct gprs_ns2_nse *nse,
340 const struct gprs_ns_ie_ip4_elem *ip4)
341{
Alexander Couzensc068d862020-10-12 04:11:51 +0200342 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200343 /* copy over. Both data structures use network byte order */
344 remote.u.sin.sin_family = AF_INET;
345 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
346 remote.u.sin.sin_port = ip4->udp_port;
347
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100348 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200349}
350
351static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
352 struct gprs_ns2_nse *nse,
353 const struct gprs_ns_ie_ip6_elem *ip6)
354{
Alexander Couzens6a161492020-07-12 13:45:50 +0200355 struct osmo_sockaddr remote = {};
356 /* copy over. Both data structures use network byte order */
357 remote.u.sin6.sin6_family = AF_INET6;
358 remote.u.sin6.sin6_addr = ip6->ip_addr;
359 remote.u.sin6.sin6_port = ip6->udp_port;
360
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100361 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200362}
363
364
365static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
366{
367 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
368 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
369 struct gprs_ns2_vc *nsvc;
370 struct gprs_ns2_vc_bind *bind;
371 struct osmo_sockaddr remote = { };
372 unsigned int i;
373
374 for (i = 0; i < gss->num_ip4_remote; i++) {
375 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->ip4_remote[i];
376
377 remote.u.sin.sin_family = AF_INET;
378 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
379 remote.u.sin.sin_port = ip4->udp_port;
380
381 llist_for_each_entry(bind, &nse->nsi->binding, list) {
382 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100383 if (bind->ll != GPRS_NS2_LL_UDP)
384 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200385
386 llist_for_each_entry(nsvc, &nse->nsvc, list) {
387 if (nsvc->bind != bind)
388 continue;
389
Alexander Couzensc4229a42020-10-11 20:58:04 +0200390 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200391 found = true;
392 break;
393 }
394 }
395
396 if (!found) {
397 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
398 if (!nsvc) {
399 /* TODO: add to a list to send back a NS-STATUS */
400 continue;
401 }
402 }
403
404 /* update data / signalling weight */
405 nsvc->data_weight = ip4->data_weight;
406 nsvc->sig_weight = ip4->sig_weight;
407 nsvc->sns_only = false;
408 }
409 }
410
411 for (i = 0; i < gss->num_ip6_remote; i++) {
412 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->ip6_remote[i];
413
414 remote.u.sin6.sin6_family = AF_INET6;
415 remote.u.sin6.sin6_addr = ip6->ip_addr;
416 remote.u.sin6.sin6_port = ip6->udp_port;
417
418 llist_for_each_entry(bind, &nse->nsi->binding, list) {
419 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100420 if (bind->ll != GPRS_NS2_LL_UDP)
421 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200422
423 llist_for_each_entry(nsvc, &nse->nsvc, list) {
424 if (nsvc->bind != bind)
425 continue;
426
Alexander Couzensc4229a42020-10-11 20:58:04 +0200427 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200428 found = true;
429 break;
430 }
431 }
432
433 if (!found) {
434 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
435 if (!nsvc) {
436 /* TODO: add to a list to send back a NS-STATUS */
437 continue;
438 }
439 }
440
441 /* update data / signalling weight */
442 nsvc->data_weight = ip6->data_weight;
443 nsvc->sig_weight = ip6->sig_weight;
444 nsvc->sns_only = false;
445 }
446 }
447
448
449 return 0;
450}
451
452/* Add a given remote IPv4 element to gprs_sns_state */
453static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
454{
455 unsigned int i;
456
457 if (gss->num_ip4_remote >= gss->num_max_ip4_remote)
458 return -NS_CAUSE_INVAL_NR_NS_VC;
459
460 /* check for duplicates */
461 for (i = 0; i < gss->num_ip4_remote; i++) {
462 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
463 continue;
464 /* TODO: log message duplicate */
Alexander Couzens6a161492020-07-12 13:45:50 +0200465 return -NS_CAUSE_PROTO_ERR_UNSPEC;
466 }
467
468 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem,
469 gss->num_ip4_remote+1);
470 gss->ip4_remote[gss->num_ip4_remote] = *ip4;
471 gss->num_ip4_remote += 1;
472 return 0;
473}
474
475/* Remove a given remote IPv4 element from gprs_sns_state */
476static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
477{
478 unsigned int i;
479
480 for (i = 0; i < gss->num_ip4_remote; i++) {
481 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
482 continue;
483 /* all array elements < i remain as they are; all > i are shifted left by one */
484 memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1);
485 gss->num_ip4_remote -= 1;
486 return 0;
487 }
488 return -1;
489}
490
491/* update the weights for specified remote IPv4 */
492static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
493{
494 unsigned int i;
495
496 for (i = 0; i < gss->num_ip4_remote; i++) {
497 if (gss->ip4_remote[i].ip_addr != ip4->ip_addr ||
498 gss->ip4_remote[i].udp_port != ip4->udp_port)
499 continue;
500
501 gss->ip4_remote[i].sig_weight = ip4->sig_weight;
502 gss->ip4_remote[i].data_weight = ip4->data_weight;
503 return 0;
504 }
505 return -1;
506}
507
508/* Add a given remote IPv6 element to gprs_sns_state */
509static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
510{
511 if (gss->num_ip6_remote >= gss->num_max_ip6_remote)
512 return -NS_CAUSE_INVAL_NR_NS_VC;
513
514 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, struct gprs_ns_ie_ip6_elem,
515 gss->num_ip6_remote+1);
516 gss->ip6_remote[gss->num_ip6_remote] = *ip6;
517 gss->num_ip6_remote += 1;
518 return 0;
519}
520
521/* Remove a given remote IPv6 element from gprs_sns_state */
522static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
523{
524 unsigned int i;
525
526 for (i = 0; i < gss->num_ip6_remote; i++) {
527 if (memcmp(&gss->ip6_remote[i], ip6, sizeof(*ip6)))
528 continue;
529 /* all array elements < i remain as they are; all > i are shifted left by one */
530 memmove(&gss->ip6_remote[i], &gss->ip6_remote[i+1], gss->num_ip6_remote-i-1);
531 gss->num_ip6_remote -= 1;
532 return 0;
533 }
534 return -1;
535}
536
537/* update the weights for specified remote IPv6 */
538static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
539{
540 unsigned int i;
541
542 for (i = 0; i < gss->num_ip6_remote; i++) {
543 if (memcmp(&gss->ip6_remote[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
544 gss->ip6_remote[i].udp_port != ip6->udp_port)
545 continue;
546 gss->ip6_remote[i].sig_weight = ip6->sig_weight;
547 gss->ip6_remote[i].data_weight = ip6->data_weight;
548 return 0;
549 }
550 return -1;
551}
552
553static 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)
554{
555 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
556 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
557 struct gprs_ns2_vc *nsvc;
558 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200559 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200560 uint8_t new_signal;
561 uint8_t new_data;
562
563 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
564 * signalling weights of all the peer IP endpoints configured for this NSE is
565 * equal to zero or if the resulting sum of the data weights of all the peer IP
566 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
567 * SNS-ACK PDU with a cause code of "Invalid weights". */
568
569 if (ip4) {
570 if (update_remote_ip4_elem(gss, ip4))
571 return -NS_CAUSE_UNKN_IP_EP;
572
573 /* copy over. Both data structures use network byte order */
574 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
575 sa.u.sin.sin_port = ip4->udp_port;
576 sa.u.sin.sin_family = AF_INET;
577 new_signal = ip4->sig_weight;
578 new_data = ip4->data_weight;
579 } else if (ip6) {
580 if (update_remote_ip6_elem(gss, ip6))
581 return -NS_CAUSE_UNKN_IP_EP;
582
583 /* copy over. Both data structures use network byte order */
584 sa.u.sin6.sin6_addr = ip6->ip_addr;
585 sa.u.sin6.sin6_port = ip6->udp_port;
586 sa.u.sin6.sin6_family = AF_INET6;
587 new_signal = ip6->sig_weight;
588 new_data = ip6->data_weight;
589 } else {
590 OSMO_ASSERT(false);
591 }
592
593 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200594 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200595 /* all nsvc in NSE should be IP/UDP nsvc */
596 OSMO_ASSERT(remote);
597
598 if (osmo_sockaddr_cmp(&sa, remote))
599 continue;
600
601 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
602 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
603 nsvc->sig_weight, new_signal);
604
605 nsvc->data_weight = new_data;
606 nsvc->sig_weight = new_signal;
607 }
608
609 return 0;
610}
611
612static int do_sns_delete(struct osmo_fsm_inst *fi,
613 const struct gprs_ns_ie_ip4_elem *ip4,
614 const struct gprs_ns_ie_ip6_elem *ip6)
615{
616 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
617 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
618 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200619 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200620 struct osmo_sockaddr sa = {};
621
622 if (ip4) {
623 if (remove_remote_ip4_elem(gss, ip4) < 0)
624 return -NS_CAUSE_UNKN_IP_EP;
625 /* copy over. Both data structures use network byte order */
626 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
627 sa.u.sin.sin_port = ip4->udp_port;
628 sa.u.sin.sin_family = AF_INET;
629 } else if (ip6) {
630 if (remove_remote_ip6_elem(gss, ip6))
631 return -NS_CAUSE_UNKN_IP_EP;
632
633 /* copy over. Both data structures use network byte order */
634 sa.u.sin6.sin6_addr = ip6->ip_addr;
635 sa.u.sin6.sin6_port = ip6->udp_port;
636 sa.u.sin6.sin6_family = AF_INET6;
637 } else {
638 OSMO_ASSERT(false);
639 }
640
641 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200642 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200643 /* all nsvc in NSE should be IP/UDP nsvc */
644 OSMO_ASSERT(remote);
645 if (osmo_sockaddr_cmp(&sa, remote))
646 continue;
647
648 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
649 gprs_ns2_free_nsvc(nsvc);
650 }
651
652 return 0;
653}
654
655static int do_sns_add(struct osmo_fsm_inst *fi,
656 const struct gprs_ns_ie_ip4_elem *ip4,
657 const struct gprs_ns_ie_ip6_elem *ip6)
658{
659 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
660 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
661 struct gprs_ns2_vc *nsvc;
662 int rc = 0;
663
664 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
665 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
666 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
667 switch (gss->ip) {
668 case IPv4:
669 rc = add_remote_ip4_elem(gss, ip4);
670 break;
671 case IPv6:
672 rc = add_remote_ip6_elem(gss, ip6);
673 break;
674 default:
675 /* the gss->ip is initialized with the bss */
676 OSMO_ASSERT(false);
677 }
678
679 if (rc)
680 return rc;
681
682 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
683 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
684 * unspecified" */
685 switch (gss->ip) {
686 case IPv4:
687 nsvc = nsvc_by_ip4_elem(nse, ip4);
688 if (nsvc) {
689 /* the nsvc should be already in sync with the ip4 / ip6 elements */
690 return -NS_CAUSE_PROTO_ERR_UNSPEC;
691 }
692
693 /* TODO: failure case */
694 ns2_nsvc_create_ip4(fi, nse, ip4);
695 break;
696 case IPv6:
697 nsvc = nsvc_by_ip6_elem(nse, ip6);
698 if (nsvc) {
699 /* the nsvc should be already in sync with the ip4 / ip6 elements */
700 return -NS_CAUSE_PROTO_ERR_UNSPEC;
701 }
702
703 /* TODO: failure case */
704 ns2_nsvc_create_ip6(fi, nse, ip6);
705 break;
706 }
707
708 gprs_ns2_start_alive_all_nsvcs(nse);
709
710 return 0;
711}
712
713
714static void ns2_sns_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
715{
Harald Weltef61a9152021-03-02 22:20:17 +0100716 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
717 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzense769f522020-12-07 07:37:07 +0100718 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200719}
720
721static void ns2_sns_st_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
722{
Harald Weltef61a9152021-03-02 22:20:17 +0100723 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200724 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
725 struct gprs_ns2_inst *nsi = nse->nsi;
726 struct tlv_parsed *tp = NULL;
727
Harald Weltef61a9152021-03-02 22:20:17 +0100728 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
729
Alexander Couzens6a161492020-07-12 13:45:50 +0200730 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100731 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200732 tp = data;
733 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
734 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
735 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
736 /* TODO: What to do? */
737 } else {
738 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_BSS,
739 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
740 }
741 break;
742 default:
743 OSMO_ASSERT(0);
744 }
745}
746
Harald Welte24920e22021-03-04 13:03:27 +0100747static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200748{
749 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100750 struct gprs_ns_ie_ip4_elem *ip4_elems;
751 struct gprs_ns_ie_ip6_elem *ip6_elems;
752 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100753 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100754 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100755 const struct osmo_sockaddr *sa;
756 struct osmo_sockaddr local;
757 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200758
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100759 ns2_clear_ipv46_entries(gss);
760
Alexander Couzense769f522020-12-07 07:37:07 +0100761 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100762 if (gss->role == GPRS_SNS_ROLE_BSS) {
763 if (!gss->initial)
764 return;
765 remote = &gss->initial->saddr;
766 } else
767 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100768
769 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100770 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100771 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100772 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100773 return;
774 }
775
Alexander Couzense769f522020-12-07 07:37:07 +0100776 switch (gss->ip) {
777 case IPv4:
778 ip4_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip4_elem) * count);
779 if (!ip4_elems)
780 return;
781
782 gss->ip4_local = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100783 llist_for_each_entry(sbind, &gss->binds, list) {
784 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100785 sa = gprs_ns2_ip_bind_sockaddr(bind);
786 if (!sa)
787 continue;
788
789 if (sa->u.sas.ss_family != AF_INET)
790 continue;
791
792 /* check if this is an specific bind */
793 if (sa->u.sin.sin_addr.s_addr == 0) {
794 if (osmo_sockaddr_local_ip(&local, remote))
795 continue;
796
797 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
798 } else {
799 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
800 }
801
802 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100803 ip4_elems->sig_weight = bind->sns_sig_weight;
804 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100805 ip4_elems++;
806 }
807
808 gss->num_ip4_local = count;
809 gss->num_max_ip4_remote = 4;
810 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->num_ip4_local, 8);
811 break;
812 case IPv6:
813 /* IPv6 */
814 ip6_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip6_elem) * count);
815 if (!ip6_elems)
816 return;
817
818 gss->ip6_local = ip6_elems;
819
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100820 llist_for_each_entry(sbind, &gss->binds, list) {
821 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100822 sa = gprs_ns2_ip_bind_sockaddr(bind);
823 if (!sa)
824 continue;
825
826 if (sa->u.sas.ss_family != AF_INET6)
827 continue;
828
829 /* check if this is an specific bind */
830 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
831 if (osmo_sockaddr_local_ip(&local, remote))
832 continue;
833
834 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
835 } else {
836 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
837 }
838
839 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100840 ip6_elems->sig_weight = bind->sns_sig_weight;
841 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100842
843 ip6_elems++;
844 }
845 gss->num_ip6_local = count;
846 gss->num_max_ip6_remote = 4;
847 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->num_ip6_local, 8);
848 break;
849 }
Harald Welte24920e22021-03-04 13:03:27 +0100850}
851
852/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
853static void ns2_sns_st_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
854{
855 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
856
Harald Weltef61a9152021-03-02 22:20:17 +0100857 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
858
Harald Welte24920e22021-03-04 13:03:27 +0100859 /* on a generic failure, the timer callback will recover */
860 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
861 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
862 if (old_state != GPRS_SNS_ST_SIZE)
863 gss->N = 0;
864
865 gss->alive = false;
866
867 ns2_sns_compute_local_ep_from_binds(fi);
868
869 /* take the first bind or take the next bind */
870 if (!gss->initial_bind) {
871 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
872 } else {
873 if (gss->initial_bind->list.next != &gss->binds) {
874 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
875 } else {
876 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
877 }
878 }
879
880
881 /* setup the NSVC */
882 if (!gss->sns_nsvc) {
883 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
884 struct osmo_sockaddr *remote = &gss->initial->saddr;
885 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
886 if (!gss->sns_nsvc)
887 return;
888 gss->sns_nsvc->sns_only = true;
889 }
890
Alexander Couzense769f522020-12-07 07:37:07 +0100891
Alexander Couzens6a161492020-07-12 13:45:50 +0200892 if (gss->num_max_ip4_remote > 0)
893 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->num_max_ip4_remote, -1);
894 else
895 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 +0200896}
897
898static void ns2_sns_st_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
899{
Harald Weltef61a9152021-03-02 22:20:17 +0100900 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens3df58862021-02-05 17:18:08 +0100901 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Harald Weltef61a9152021-03-02 22:20:17 +0100902 struct tlv_parsed *tp = NULL;
903
904 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzens6a161492020-07-12 13:45:50 +0200905
906 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100907 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200908 tp = (struct tlv_parsed *) data;
909 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
910 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
911 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
912 /* TODO: What to do? */
913 } else {
Alexander Couzens3df58862021-02-05 17:18:08 +0100914 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_SGSN, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 3);
Alexander Couzens6a161492020-07-12 13:45:50 +0200915 }
916 break;
917 default:
918 OSMO_ASSERT(0);
919 }
920}
921
922static void ns2_sns_st_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
923{
924 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100925
Harald Weltef61a9152021-03-02 22:20:17 +0100926 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
927
Alexander Couzens790a9632021-02-05 17:18:39 +0100928 if (old_state != GPRS_SNS_ST_CONFIG_BSS)
929 gss->N = 0;
930
Alexander Couzens6a161492020-07-12 13:45:50 +0200931 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200932 switch (gss->ip) {
933 case IPv4:
934 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100935 gss->ip4_local, gss->num_ip4_local,
936 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200937 break;
938 case IPv6:
939 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100940 NULL, 0,
941 gss->ip6_local, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200942 break;
943 }
944}
945
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100946/* calculate the timeout of the configured state. the configured
947 * state will fail if not at least one NS-VC is alive within X second.
948 */
949static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
950{
951 int secs;
952 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
953 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
954 secs += nsi->timeout[NS_TOUT_TNS_TEST];
955
956 return secs;
957}
Alexander Couzens6a161492020-07-12 13:45:50 +0200958
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100959/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
960static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200961{
962 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200963
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100964 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
965 const struct gprs_ns_ie_ip4_elem *v4_list;
966 unsigned int num_v4;
967 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
968 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +0200969
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100970 if (num_v4 && gss->ip6_remote)
971 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +0200972
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100973 /* realloc to the new size */
974 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote,
975 struct gprs_ns_ie_ip4_elem,
976 gss->num_ip4_remote + num_v4);
977 /* append the new entries to the end of the list */
978 memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list));
979 gss->num_ip4_remote += num_v4;
980
981 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
982 gss->num_ip4_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200983 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200984
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100985 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
986 const struct gprs_ns_ie_ip6_elem *v6_list;
987 unsigned int num_v6;
988 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
989 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
990
991 if (num_v6 && gss->ip4_remote)
992 return -NS_CAUSE_INVAL_NR_IPv6_EP;
993
994 /* realloc to the new size */
995 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote,
996 struct gprs_ns_ie_ip6_elem,
997 gss->num_ip6_remote + num_v6);
998 /* append the new entries to the end of the list */
999 memcpy(&gss->ip6_remote[gss->num_ip6_remote], v6_list, num_v6*sizeof(*v6_list));
1000 gss->num_ip6_remote += num_v6;
1001
1002 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %u entries\n",
1003 gss->num_ip6_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +02001004 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001005
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001006 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001007}
1008
Alexander Couzens790a9632021-02-05 17:18:39 +01001009static void ns2_sns_st_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1010{
1011 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1012
Harald Weltef61a9152021-03-02 22:20:17 +01001013 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1014
Alexander Couzens790a9632021-02-05 17:18:39 +01001015 if (old_state != GPRS_SNS_ST_CONFIG_SGSN)
1016 gss->N = 0;
1017}
1018
Alexander Couzens6a161492020-07-12 13:45:50 +02001019static void ns2_sns_st_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1020{
1021 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001022 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1023 uint8_t cause;
1024 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001025
Harald Weltef61a9152021-03-02 22:20:17 +01001026 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1027
Alexander Couzens6a161492020-07-12 13:45:50 +02001028 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001029 case GPRS_SNS_EV_RX_CONFIG_END:
1030 case GPRS_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001031 rc = ns_sns_append_remote_eps(fi, data);
1032 if (rc < 0) {
1033 cause = -rc;
1034 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1035 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1036 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001037 }
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001038 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1039 /* check if sum of data / sig weights == 0 */
1040 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1041 cause = NS_CAUSE_INVAL_WEIGH;
1042 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1043 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1044 return;
1045 }
1046 create_missing_nsvcs(fi);
1047 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1048 /* start the test procedure on ALL NSVCs! */
1049 gprs_ns2_start_alive_all_nsvcs(nse);
1050 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1051 } else {
1052 /* just send CONFIG-ACK */
1053 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1054 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001055 }
1056 break;
1057 default:
1058 OSMO_ASSERT(0);
1059 }
1060}
1061
Alexander Couzens67725e22021-02-15 02:37:03 +01001062/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001063static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1064 struct ns2_sns_state *gss,
1065 struct tlv_parsed *tp)
1066{
1067 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1068 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1069 int num_v4 = 0, num_v6 = 0;
1070 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001071 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001072 int rc = 0;
1073
1074 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1075 * check uniqueness within the lists (no doublicate entries)
1076 * check not-known-by-us and sent back a list of unknown/known values
1077 * (abnormal behaviour according to 48.016)
1078 */
1079
1080 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1081 if (gss->ip == IPv4) {
1082 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1083 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1084 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1085 return;
1086 }
1087
1088 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1089 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001090 for (i = 0; i < num_v4; i++) {
1091 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001092 rc = do_sns_add(fi, &v4_list[i], NULL);
1093 if (rc < 0) {
1094 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001095 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001096 do_sns_delete(fi, &v4_list[j], NULL);
1097 cause = -rc;
1098 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1099 break;
1100 }
1101 }
1102 } else { /* IPv6 */
1103 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1104 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1105 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1106 return;
1107 }
1108
1109 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1110 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001111 for (i = 0; i < num_v6; i++) {
1112 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001113 rc = do_sns_add(fi, NULL, &v6_list[i]);
1114 if (rc < 0) {
1115 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001116 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001117 do_sns_delete(fi, NULL, &v6_list[j]);
1118 cause = -rc;
1119 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1120 break;
1121 }
1122 }
1123 }
1124
1125 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1126 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1127}
1128
1129static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1130 struct ns2_sns_state *gss,
1131 struct tlv_parsed *tp)
1132{
1133 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1134 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1135 int num_v4 = 0, num_v6 = 0;
1136 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001137 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001138 int rc = 0;
1139
1140 /* TODO: split up delete into v4 + v6
1141 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1142 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1143 */
1144 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1145 if (gss->ip == IPv4) {
1146 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1147 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1148 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001149 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001150 rc = do_sns_delete(fi, &v4_list[i], NULL);
1151 if (rc < 0) {
1152 cause = -rc;
1153 /* continue to delete others */
1154 }
1155 }
1156 if (cause != 0xff) {
1157 /* TODO: create list of not-deleted and return it */
1158 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1159 return;
1160 }
1161
1162 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1163 /* delete all NS-VCs for given IPv4 address */
1164 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1165 struct gprs_ns_ie_ip4_elem *ip4_remote;
1166 uint32_t ip_addr = *(uint32_t *)(ie+1);
1167 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1168 cause = NS_CAUSE_UNKN_IP_ADDR;
1169 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1170 return;
1171 }
1172 /* make a copy as do_sns_delete() will change the array underneath us */
1173 ip4_remote = talloc_memdup(fi, gss->ip4_remote,
1174 gss->num_ip4_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001175 for (i = 0; i < gss->num_ip4_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001176 if (ip4_remote[i].ip_addr == ip_addr) {
1177 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1178 if (rc < 0) {
1179 cause = -rc;
1180 /* continue to delete others */
1181 }
1182 }
1183 }
1184 talloc_free(ip4_remote);
1185 if (cause != 0xff) {
1186 /* TODO: create list of not-deleted and return it */
1187 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1188 return;
1189 }
1190 } else {
1191 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1192 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1193 return;
1194 }
1195 } else { /* IPv6 */
1196 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1197 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1198 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001199 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001200 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1201 if (rc < 0) {
1202 cause = -rc;
1203 /* continue to delete others */
1204 }
1205 }
1206 if (cause != 0xff) {
1207 /* TODO: create list of not-deleted and return it */
1208 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1209 return;
1210 }
1211 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1212 /* delete all NS-VCs for given IPv4 address */
1213 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1214 struct gprs_ns_ie_ip6_elem *ip6_remote;
1215 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001216 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001217 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1218 cause = NS_CAUSE_UNKN_IP_ADDR;
1219 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1220 return;
1221 }
1222 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1223 /* make a copy as do_sns_delete() will change the array underneath us */
1224 ip6_remote = talloc_memdup(fi, gss->ip6_remote,
1225 gss->num_ip6_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001226 for (i = 0; i < gss->num_ip6_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001227 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1228 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1229 if (rc < 0) {
1230 cause = -rc;
1231 /* continue to delete others */
1232 }
1233 }
1234 }
1235
1236 talloc_free(ip6_remote);
1237 if (cause != 0xff) {
1238 /* TODO: create list of not-deleted and return it */
1239 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1240 return;
1241 }
1242 } else {
1243 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1244 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1245 return;
1246 }
1247 }
1248 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1249}
1250
1251static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1252 struct ns2_sns_state *gss,
1253 struct tlv_parsed *tp)
1254{
1255 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1256 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1257 int num_v4 = 0, num_v6 = 0;
1258 uint8_t trans_id, cause = 0xff;
1259 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001260 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001261
1262 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1263 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1264 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1265 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001266 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001267 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1268 if (rc < 0) {
1269 cause = -rc;
1270 /* continue to others */
1271 }
1272 }
1273 if (cause != 0xff) {
1274 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1275 return;
1276 }
1277 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1278 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1279 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001280 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001281 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1282 if (rc < 0) {
1283 cause = -rc;
1284 /* continue to others */
1285 }
1286 }
1287 if (cause != 0xff) {
1288 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1289 return;
1290 }
1291 } else {
1292 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1293 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1294 return;
1295 }
1296 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1297}
1298
1299static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1300{
1301 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1302 struct tlv_parsed *tp = data;
1303
1304 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001305 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001306 ns2_sns_st_configured_add(fi, gss, tp);
1307 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001308 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001309 ns2_sns_st_configured_delete(fi, gss, tp);
1310 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001311 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001312 ns2_sns_st_configured_change(fi, gss, tp);
1313 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001314 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001315 osmo_timer_del(&fi->timer);
1316 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001317 }
1318}
1319
1320static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1321{
1322 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens138b96f2021-01-25 16:23:29 +01001323 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001324}
1325
1326static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1327 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001328 .in_event_mask = 0, /* handled by all_state_action */
Alexander Couzens6a161492020-07-12 13:45:50 +02001329 .out_state_mask = S(GPRS_SNS_ST_SIZE),
1330 .name = "UNCONFIGURED",
1331 .action = ns2_sns_st_unconfigured,
1332 },
1333 [GPRS_SNS_ST_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001334 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001335 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1336 S(GPRS_SNS_ST_SIZE) |
1337 S(GPRS_SNS_ST_CONFIG_BSS),
1338 .name = "SIZE",
1339 .action = ns2_sns_st_size,
1340 .onenter = ns2_sns_st_size_onenter,
1341 },
1342 [GPRS_SNS_ST_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001343 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001344 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1345 S(GPRS_SNS_ST_CONFIG_BSS) |
1346 S(GPRS_SNS_ST_CONFIG_SGSN) |
1347 S(GPRS_SNS_ST_SIZE),
1348 .name = "CONFIG_BSS",
1349 .action = ns2_sns_st_config_bss,
1350 .onenter = ns2_sns_st_config_bss_onenter,
1351 },
1352 [GPRS_SNS_ST_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001353 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1354 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001355 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1356 S(GPRS_SNS_ST_CONFIG_SGSN) |
1357 S(GPRS_SNS_ST_CONFIGURED) |
1358 S(GPRS_SNS_ST_SIZE),
1359 .name = "CONFIG_SGSN",
1360 .action = ns2_sns_st_config_sgsn,
Alexander Couzens790a9632021-02-05 17:18:39 +01001361 .onenter = ns2_sns_st_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001362 },
1363 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001364 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1365 S(GPRS_SNS_EV_RX_DELETE) |
1366 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1367 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001368 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1369 S(GPRS_SNS_ST_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001370 .name = "CONFIGURED",
1371 .action = ns2_sns_st_configured,
1372 .onenter = ns2_sns_st_configured_onenter,
1373 },
1374};
1375
1376static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1377{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001378 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001379 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1380 struct gprs_ns2_inst *nsi = nse->nsi;
1381
Alexander Couzens90ee9632020-12-07 06:18:32 +01001382 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001383 switch (fi->T) {
1384 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001385 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1386 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001387 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001388 } else {
Alexander Couzenscc65a252020-12-21 14:03:58 +01001389 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nsi->timeout[NS_TOUT_TSNS_PROV], 1);
Alexander Couzensa367d082020-12-21 14:06:24 +01001390 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001391 break;
1392 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001393 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001394 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 +01001395 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001396 } else {
Alexander Couzenscc65a252020-12-21 14:03:58 +01001397 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_BSS, nsi->timeout[NS_TOUT_TSNS_PROV], 2);
Alexander Couzensa367d082020-12-21 14:06:24 +01001398 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001399 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001400 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001401 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1402 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 +01001403 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001404 } else {
1405 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_SGSN, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
1406 }
1407 break;
1408 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001409 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 +01001410 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001411 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001412 }
1413 return 0;
1414}
1415
Harald Welte9e37bf42021-03-02 20:48:31 +01001416/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001417static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1418{
1419 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001420 struct ns2_sns_bind *sbind;
1421 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001422
Alexander Couzense769f522020-12-07 07:37:07 +01001423 switch (event) {
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001424 case GPRS_SNS_EV_REQ_ADD_BIND:
1425 sbind = data;
1426 switch (fi->state) {
1427 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001428 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001429 break;
1430 case GPRS_SNS_ST_SIZE:
1431 /* TODO: add the ip4 element to the list */
1432 break;
1433 case GPRS_SNS_ST_CONFIG_BSS:
1434 case GPRS_SNS_ST_CONFIG_SGSN:
1435 case GPRS_SNS_ST_CONFIGURED:
1436 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1437 break;
1438 }
1439 break;
1440 case GPRS_SNS_EV_REQ_DELETE_BIND:
1441 sbind = data;
1442 switch (fi->state) {
1443 case GPRS_SNS_ST_UNCONFIGURED:
1444 break;
1445 case GPRS_SNS_ST_SIZE:
1446 /* TODO: remove the ip4 element from the list */
1447 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1448 if (nsvc->bind == sbind->bind) {
1449 gprs_ns2_free_nsvc(nsvc);
1450 }
1451 }
1452 break;
1453 case GPRS_SNS_ST_CONFIG_BSS:
1454 case GPRS_SNS_ST_CONFIG_SGSN:
1455 case GPRS_SNS_ST_CONFIGURED:
1456 /* TODO: do an delete SNS-IP procedure */
1457 /* TODO: remove the ip4 element to the list */
1458 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1459 if (nsvc->bind == sbind->bind) {
1460 gprs_ns2_free_nsvc(nsvc);
1461 }
1462 }
1463 break;
1464 }
1465 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1466 talloc_free(sbind);
1467 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001468 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001469}
1470
Harald Welte9e37bf42021-03-02 20:48:31 +01001471/* allstate-action for BSS role */
1472static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1473{
1474 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1475 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1476
1477 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
1478 switch (event) {
1479 case GPRS_SNS_EV_REQ_NO_NSVC:
1480 /* ignore reselection running */
1481 if (gss->reselection_running)
1482 break;
1483
1484 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1485 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1486 break;
1487 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
1488 /* tear down previous state
1489 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1490 gss->reselection_running = true;
1491 gprs_ns2_free_nsvcs(nse);
1492 ns2_clear_ipv46_entries(gss);
1493
1494 /* Choose the next sns endpoint. */
1495 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds)) {
1496 gss->initial = NULL;
1497 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1498 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1499 return;
1500 } else if (!gss->initial) {
1501 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1502 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1503 /* last entry, continue with first */
1504 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1505 } else {
1506 /* next element is an entry */
1507 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1508 }
1509
1510 gss->reselection_running = false;
1511 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 1);
1512 break;
1513 default:
1514 ns2_sns_st_all_action(fi, event, data);
1515 break;
1516 }
1517}
1518
Alexander Couzens6a161492020-07-12 13:45:50 +02001519static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1520 .name = "GPRS-NS2-SNS-BSS",
1521 .states = ns2_sns_bss_states,
1522 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001523 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1524 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001525 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1526 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001527 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001528 .cleanup = NULL,
1529 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001530 .event_names = gprs_sns_event_names,
1531 .pre_term = NULL,
1532 .log_subsys = DLNS,
1533};
1534
Harald Welte5bef2cc2020-09-18 22:33:24 +02001535/*! Allocate an IP-SNS FSM for the BSS side.
1536 * \param[in] nse NS Entity in which the FSM runs
1537 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001538 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001539struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1540 const char *id)
1541{
1542 struct osmo_fsm_inst *fi;
1543 struct ns2_sns_state *gss;
1544
1545 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1546 if (!fi)
1547 return fi;
1548
1549 gss = talloc_zero(fi, struct ns2_sns_state);
1550 if (!gss)
1551 goto err;
1552
1553 fi->priv = gss;
1554 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001555 gss->role = GPRS_SNS_ROLE_BSS;
Alexander Couzense769f522020-12-07 07:37:07 +01001556 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001557 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001558
1559 return fi;
1560err:
1561 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1562 return NULL;
1563}
1564
Harald Welte5bef2cc2020-09-18 22:33:24 +02001565/*! main entry point for receiving SNS messages from the network.
1566 * \param[in] nsvc NS-VC on which the message was received
1567 * \param[in] msg message buffer of the IP-SNS message
1568 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001569 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001570int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001571{
1572 struct gprs_ns2_nse *nse = nsvc->nse;
1573 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1574 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001575 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001576 struct osmo_fsm_inst *fi;
1577
1578 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001579 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1580 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001581 return -EINVAL;
1582 }
1583
Alexander Couzens6a161492020-07-12 13:45:50 +02001584 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1585 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001586 gss = (struct ns2_sns_state *) fi->priv;
1587 if (!gss->sns_nsvc)
1588 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001589
Harald Weltef2949742021-01-20 14:54:14 +01001590 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1591 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1592
Alexander Couzens6a161492020-07-12 13:45:50 +02001593 switch (nsh->pdu_type) {
1594 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001595 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001596 break;
1597 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001598 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001599 break;
1600 case SNS_PDUT_CONFIG:
1601 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001602 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001603 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001604 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001605 break;
1606 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001607 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001608 break;
1609 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001610 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001611 break;
1612 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001613 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001614 break;
1615 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001616 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001617 break;
1618 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001619 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001620 break;
1621 default:
Harald Weltef2949742021-01-20 14:54:14 +01001622 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1623 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001624 return -EINVAL;
1625 }
1626
1627 return 0;
1628}
1629
1630#include <osmocom/vty/vty.h>
1631#include <osmocom/vty/misc.h>
1632
Harald Welte1262c4f2021-01-19 20:58:33 +01001633static 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 +02001634{
1635 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001636 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001637 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1638}
1639
Harald Welte1262c4f2021-01-19 20:58:33 +01001640static 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 +02001641{
1642 char ip_addr[INET6_ADDRSTRLEN] = {};
1643 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1644 strcpy(ip_addr, "Invalid IPv6");
1645
Harald Welte1262c4f2021-01-19 20:58:33 +01001646 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001647 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1648}
1649
Harald Welte5bef2cc2020-09-18 22:33:24 +02001650/*! Dump the IP-SNS state to a vty.
1651 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001652 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001653 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1654 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001655void 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 +02001656{
1657 struct ns2_sns_state *gss;
1658 unsigned int i;
1659
1660 if (!nse->bss_sns_fi)
1661 return;
1662
Harald Welte1262c4f2021-01-19 20:58:33 +01001663 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001664 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1665
Harald Welte1262c4f2021-01-19 20:58:33 +01001666 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1667 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001668
1669 if (gss->num_ip4_local && gss->num_ip4_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001670 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001671 for (i = 0; i < gss->num_ip4_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001672 vty_dump_sns_ip4(vty, prefix, &gss->ip4_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001673
Harald Welte1262c4f2021-01-19 20:58:33 +01001674 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001675 for (i = 0; i < gss->num_ip4_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001676 vty_dump_sns_ip4(vty, prefix, &gss->ip4_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001677 }
1678
1679 if (gss->num_ip6_local && gss->num_ip6_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001680 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001681 for (i = 0; i < gss->num_ip6_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001682 vty_dump_sns_ip6(vty, prefix, &gss->ip6_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001683
Harald Welte1262c4f2021-01-19 20:58:33 +01001684 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001685 for (i = 0; i < gss->num_ip6_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001686 vty_dump_sns_ip6(vty, prefix, &gss->ip6_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001687 }
1688}
1689
Alexander Couzens412bc342020-11-19 05:24:37 +01001690/*! write IP-SNS to a vty
1691 * \param[in] vty VTY to which the state shall be printed
1692 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001693void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001694{
1695 struct ns2_sns_state *gss;
1696 struct osmo_sockaddr_str addr_str;
1697 struct sns_endpoint *endpoint;
1698
1699 if (!nse->bss_sns_fi)
1700 return;
1701
1702 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1703 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001704 /* It's unlikely that an error happens, but let's better be safe. */
1705 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1706 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001707 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001708 }
1709}
1710
Alexander Couzense769f522020-12-07 07:37:07 +01001711static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1712 const struct osmo_sockaddr *saddr)
1713{
1714 struct sns_endpoint *endpoint;
1715
1716 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1717 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1718 return endpoint;
1719 }
1720
1721 return NULL;
1722}
1723
1724/*! gprs_ns2_sns_add_endpoint
1725 * \param[in] nse
1726 * \param[in] sockaddr
1727 * \return
1728 */
1729int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1730 const struct osmo_sockaddr *saddr)
1731{
1732 struct ns2_sns_state *gss;
1733 struct sns_endpoint *endpoint;
1734 bool do_selection = false;
1735
1736 if (nse->ll != GPRS_NS2_LL_UDP) {
1737 return -EINVAL;
1738 }
1739
Alexander Couzens138b96f2021-01-25 16:23:29 +01001740 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001741 return -EINVAL;
1742 }
1743
1744 gss = nse->bss_sns_fi->priv;
1745
1746 if (ns2_get_sns_endpoint(gss, saddr))
1747 return -EADDRINUSE;
1748
1749 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1750 if (!endpoint)
1751 return -ENOMEM;
1752
1753 endpoint->saddr = *saddr;
1754 if (llist_empty(&gss->sns_endpoints))
1755 do_selection = true;
1756
1757 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1758 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001759 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001760
1761 return 0;
1762}
1763
1764/*! gprs_ns2_sns_del_endpoint
1765 * \param[in] nse
1766 * \param[in] sockaddr
1767 * \return 0 on success, otherwise < 0
1768 */
1769int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1770 const struct osmo_sockaddr *saddr)
1771{
1772 struct ns2_sns_state *gss;
1773 struct sns_endpoint *endpoint;
1774
1775 if (nse->ll != GPRS_NS2_LL_UDP) {
1776 return -EINVAL;
1777 }
1778
Alexander Couzens138b96f2021-01-25 16:23:29 +01001779 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001780 return -EINVAL;
1781 }
1782
1783 gss = nse->bss_sns_fi->priv;
1784 endpoint = ns2_get_sns_endpoint(gss, saddr);
1785 if (!endpoint)
1786 return -ENOENT;
1787
1788 /* if this is an unused SNS endpoint it's done */
1789 if (gss->initial != endpoint) {
1790 llist_del(&endpoint->list);
1791 talloc_free(endpoint);
1792 return 0;
1793 }
1794
Alexander Couzens67725e22021-02-15 02:37:03 +01001795 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001796 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001797 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001798 "Closing all NS-VC and restart SNS-SIZE procedure"
1799 "with a remaining SNS endpoint.\n");
1800
1801 /* Continue with the next endpoint in the list.
1802 * Special case if the endpoint is at the start or end of the list */
1803 if (endpoint->list.prev == &gss->sns_endpoints ||
1804 endpoint->list.next == &gss->sns_endpoints)
1805 gss->initial = NULL;
1806 else
1807 gss->initial = llist_entry(endpoint->list.next->prev,
1808 struct sns_endpoint,
1809 list);
1810
1811 llist_del(&endpoint->list);
1812 gprs_ns2_free_nsvcs(nse);
1813 talloc_free(endpoint);
1814
1815 return 0;
1816}
1817
1818/*! gprs_ns2_sns_count
1819 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1820 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1821 */
1822int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1823{
1824 struct ns2_sns_state *gss;
1825 struct sns_endpoint *endpoint;
1826 int count = 0;
1827
1828 if (nse->ll != GPRS_NS2_LL_UDP) {
1829 return -EINVAL;
1830 }
1831
Alexander Couzens138b96f2021-01-25 16:23:29 +01001832 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001833 return -EINVAL;
1834 }
1835
1836 gss = nse->bss_sns_fi->priv;
1837 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1838 count++;
1839
1840 return count;
1841}
1842
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001843void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1844{
1845 struct ns2_sns_state *gss;
1846 struct gprs_ns2_vc *tmp;
1847
1848 if (!nse->bss_sns_fi)
1849 return;
1850
1851 gss = nse->bss_sns_fi->priv;
1852 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1853 return;
1854
1855 if (alive == gss->alive)
1856 return;
1857
1858 /* check if this is the current SNS NS-VC */
1859 if (nsvc == gss->sns_nsvc) {
1860 /* only replace the SNS NS-VC if there are other alive NS-VC.
1861 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1862 * and couldn't confirm yet if the NS-VC comes up */
1863 if (gss->alive && !alive)
1864 ns2_sns_replace_nsvc(nsvc);
1865 }
1866
1867 if (alive) {
1868 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001869 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001870 } else {
1871 /* is there at least another alive nsvc? */
1872 llist_for_each_entry(tmp, &nse->nsvc, list) {
1873 if (ns2_vc_is_unblocked(tmp))
1874 return;
1875 }
1876
1877 /* all NS-VC have failed */
1878 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001879 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001880 }
1881}
1882
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001883int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1884 struct gprs_ns2_vc_bind *bind)
1885{
1886 struct ns2_sns_state *gss;
1887 struct ns2_sns_bind *tmp;
1888
1889 OSMO_ASSERT(nse->bss_sns_fi);
1890 gss = nse->bss_sns_fi->priv;
1891
1892 if (!gprs_ns2_is_ip_bind(bind)) {
1893 return -EINVAL;
1894 }
1895
1896 if (!llist_empty(&gss->binds)) {
1897 llist_for_each_entry(tmp, &gss->binds, list) {
1898 if (tmp->bind == bind)
1899 return -EALREADY;
1900 }
1901 }
1902
1903 tmp = talloc_zero(gss, struct ns2_sns_bind);
1904 if (!tmp)
1905 return -ENOMEM;
1906 tmp->bind = bind;
1907 llist_add_tail(&tmp->list, &gss->binds);
1908
1909 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
1910 return 0;
1911}
1912
1913/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
1914int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
1915 struct gprs_ns2_vc_bind *bind)
1916{
1917 struct ns2_sns_state *gss;
1918 struct ns2_sns_bind *tmp, *tmp2;
1919 bool found = false;
1920
1921 if (!nse->bss_sns_fi)
1922 return -EINVAL;
1923
1924 gss = nse->bss_sns_fi->priv;
1925 if (gss->initial_bind && gss->initial_bind->bind == bind) {
1926 if (gss->initial_bind->list.prev == &gss->binds)
1927 gss->initial_bind = NULL;
1928 else
1929 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
1930 }
1931
1932 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
1933 if (tmp->bind == bind) {
1934 llist_del(&tmp->list);
1935 found = true;
1936 }
1937 }
1938
1939 if (!found)
1940 return -ENOENT;
1941
1942 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
1943 return 0;
1944}
1945
Alexander Couzensc4704762021-02-08 23:13:12 +01001946/* Update SNS weights
1947 * \param[in] nsvc the NSVC which should be updated
1948 */
1949void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
1950{
1951 /* TODO: implement weights after binds per sns implemented */
1952}
1953
Harald Welte4f127462021-03-02 20:49:10 +01001954
1955
1956
1957/***********************************************************************
1958 * SGSN role
1959 ***********************************************************************/
1960
1961static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1962{
1963 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1964 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
1965 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
1966}
1967
1968/* We're waiting for inbound SNS-CONFIG from the BSS */
1969static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1970{
1971 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1972 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1973 struct gprs_ns2_inst *nsi = nse->nsi;
1974 uint8_t cause;
1975 int rc;
1976
1977 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
1978
1979 switch (event) {
1980 case GPRS_SNS_EV_RX_CONFIG:
1981 case GPRS_SNS_EV_RX_CONFIG_END:
1982 rc = ns_sns_append_remote_eps(fi, data);
1983 if (rc < 0) {
1984 cause = -rc;
1985 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1986 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1987 return;
1988 }
1989 /* only change state if last CONFIG was received */
1990 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1991 /* ensure sum of data weight / sig weights is > 0 */
1992 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1993 cause = NS_CAUSE_INVAL_WEIGH;
1994 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1995 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1996 break;
1997 }
1998 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1999 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2000 } else {
2001 /* just send CONFIG-ACK */
2002 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2003 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2004 }
2005 break;
2006 }
2007}
2008
2009static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2010{
2011 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2012 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2013
2014 ns2_sns_compute_local_ep_from_binds(fi);
2015 /* transmit SGSN-oriented SNS-CONFIG */
2016 ns2_tx_sns_config(gss->sns_nsvc, true, gss->ip4_local, gss->num_ip4_local,
2017 gss->ip6_local, gss->num_ip6_local);
2018}
2019
2020/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2021static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2022{
2023 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2024 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2025 struct tlv_parsed *tp = NULL;
2026
2027 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2028
2029 switch (event) {
2030 case GPRS_SNS_EV_RX_CONFIG_ACK:
2031 tp = data;
2032 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2033 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2034 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2035 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2036 break;
2037 }
2038 /* we currently only send one SNS-CONFIG with END FLAG */
2039 if (true) {
2040 create_missing_nsvcs(fi);
2041 /* start the test procedure on ALL NSVCs! */
2042 gprs_ns2_start_alive_all_nsvcs(nse);
2043 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2044 }
2045 break;
2046 }
2047}
2048
2049/* SGSN-side SNS state machine */
2050static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2051 [GPRS_SNS_ST_UNCONFIGURED] = {
2052 .in_event_mask = 0, /* handled by all_state_action */
2053 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2054 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2055 .name = "UNCONFIGURED",
2056 .action = ns2_sns_st_sgsn_unconfigured,
2057 },
2058 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
2059 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
2060 S(GPRS_SNS_EV_RX_CONFIG_END),
2061 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2062 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2063 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2064 .name = "SGSN_WAIT_CONFIG",
2065 .action = ns2_sns_st_sgsn_wait_config,
2066 },
2067 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
2068 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
2069 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2070 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2071 S(GPRS_SNS_ST_CONFIGURED),
2072 .name = "SGSN_WAIT_CONFIG_ACK",
2073 .action = ns2_sns_st_sgsn_wait_config_ack,
2074 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2075 },
2076 [GPRS_SNS_ST_CONFIGURED] = {
2077 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
2078 S(GPRS_SNS_EV_RX_DELETE) |
2079 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
2080 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
2081 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2082 .name = "CONFIGURED",
2083 /* shared with BSS side; once configured there's no difference */
2084 .action = ns2_sns_st_configured,
2085 .onenter = ns2_sns_st_configured_onenter,
2086 },
2087};
2088
2089static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2090{
2091 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2092 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2093 struct gprs_ns2_inst *nsi = nse->nsi;
2094
2095 gss->N++;
2096 switch (fi->T) {
2097 case 3:
2098 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2099 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2100 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2101 } else {
2102 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2103 }
2104 break;
2105 case 4:
2106 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2107 break;
2108 }
2109 return 0;
2110}
2111
2112
2113/* allstate-action for SGSN role */
2114static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2115{
2116 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2117 struct tlv_parsed *tp = NULL;
2118 uint8_t flag;
2119
2120 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2121
2122 switch (event) {
2123 case GPRS_SNS_EV_RX_SIZE:
2124 tp = (struct tlv_parsed *) data;
2125 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1)) {
2126 uint8_t cause = NS_CAUSE_MISSING_ESSENT_IE;
2127 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2128 break;
2129 }
2130 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2131 if (flag & 1) {
2132 struct gprs_ns2_vc *nsvc, *nsvc2;
2133 /* clear all state */
2134 gss->N = 0;
2135 ns2_clear_ipv46_entries(gss);
2136 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2137 if (nsvc == gss->sns_nsvc) {
2138 /* keep the NSVC we need for SNS, but unconfigure it */
2139 nsvc->sig_weight = 0;
2140 nsvc->data_weight = 0;
2141 ns2_vc_force_unconfigured(nsvc);
2142 } else {
2143 /* free all other NS-VCs */
2144 gprs_ns2_free_nsvc(nsvc);
2145 }
2146 }
2147 }
2148 /* send SIZE_ACK */
2149 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2150 /* only wait for SNS-CONFIG in case of Reset flag */
2151 if (flag & 1)
2152 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2153 break;
2154 default:
2155 ns2_sns_st_all_action(fi, event, data);
2156 break;
2157 }
2158}
2159
2160static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2161 .name = "GPRS-NS2-SNS-SGSN",
2162 .states = ns2_sns_sgsn_states,
2163 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
2164 .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) |
2165 S(GPRS_SNS_EV_REQ_NO_NSVC) |
2166 S(GPRS_SNS_EV_REQ_ADD_BIND) |
2167 S(GPRS_SNS_EV_REQ_DELETE_BIND),
2168 .allstate_action = ns2_sns_st_all_action_sgsn,
2169 .cleanup = NULL,
2170 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2171 .event_names = gprs_sns_event_names,
2172 .pre_term = NULL,
2173 .log_subsys = DLNS,
2174};
2175
2176/*! Allocate an IP-SNS FSM for the SGSN side.
2177 * \param[in] nse NS Entity in which the FSM runs
2178 * \param[in] id string identifier
2179 * \returns FSM instance on success; NULL on error */
2180struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2181{
2182 struct osmo_fsm_inst *fi;
2183 struct ns2_sns_state *gss;
2184
2185 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2186 if (!fi)
2187 return fi;
2188
2189 gss = talloc_zero(fi, struct ns2_sns_state);
2190 if (!gss)
2191 goto err;
2192
2193 fi->priv = gss;
2194 gss->nse = nse;
2195 gss->role = GPRS_SNS_ROLE_SGSN;
2196 INIT_LLIST_HEAD(&gss->sns_endpoints);
2197 INIT_LLIST_HEAD(&gss->binds);
2198
2199 return fi;
2200err:
2201 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2202 return NULL;
2203}
2204
2205
2206
2207
Alexander Couzens6a161492020-07-12 13:45:50 +02002208/* initialize osmo_ctx on main tread */
2209static __attribute__((constructor)) void on_dso_load_ctx(void)
2210{
2211 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002212 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002213}