blob: 9e30f62ef48867296e6e061d7cad44885f040121 [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
Harald Welte4f127462021-03-02 20:49:10 +010059enum ns2_sns_role {
60 GPRS_SNS_ROLE_BSS,
61 GPRS_SNS_ROLE_SGSN,
62};
63
Harald Welte694dad52021-03-23 15:22:16 +010064/* BSS-side-only states _ST_BSS_; SGSN-side only states _ST_SGSN_; others shared */
Alexander Couzens6a161492020-07-12 13:45:50 +020065enum gprs_sns_bss_state {
66 GPRS_SNS_ST_UNCONFIGURED,
Harald Welte694dad52021-03-23 15:22:16 +010067 GPRS_SNS_ST_BSS_SIZE, /*!< SNS-SIZE procedure ongoing */
68 GPRS_SNS_ST_BSS_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */
69 GPRS_SNS_ST_BSS_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */
Alexander Couzens6a161492020-07-12 13:45:50 +020070 GPRS_SNS_ST_CONFIGURED,
Harald Welte4f127462021-03-02 20:49:10 +010071 GPRS_SNS_ST_SGSN_WAIT_CONFIG, /* !< SGSN role: Wait for CONFIG from BSS */
72 GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, /* !< SGSN role: Wait for CONFIG-ACK from BSS */
Alexander Couzens1f3193d2021-06-05 22:08:11 +020073 GPRS_SNS_ST_LOCAL_PROCEDURE, /*!< in process of a ADD/DEL/CHANGE procedure towards SGSN (BSS->SGSN) */
Alexander Couzens6a161492020-07-12 13:45:50 +020074};
75
Alexander Couzens6a161492020-07-12 13:45:50 +020076static const struct value_string gprs_sns_event_names[] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +020077 { NS2_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" },
78 { NS2_SNS_EV_RX_SIZE, "RX_SIZE" },
79 { NS2_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" },
80 { NS2_SNS_EV_RX_CONFIG, "RX_CONFIG" },
81 { NS2_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" },
82 { NS2_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" },
83 { NS2_SNS_EV_RX_ADD, "RX_ADD" },
84 { NS2_SNS_EV_RX_DELETE, "RX_DELETE" },
85 { NS2_SNS_EV_RX_ACK, "RX_ACK" },
86 { NS2_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" },
87 { NS2_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" },
Alexander Couzens83f06ce2021-08-06 19:50:09 +020088 { NS2_SNS_EV_REQ_FREE_NSVCS, "REQ_FREE_NSVCS" },
Alexander Couzens175eb7b2021-07-20 18:41:14 +020089 { NS2_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"},
90 { NS2_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"},
91 { NS2_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"},
Alexander Couzens1f3193d2021-06-05 22:08:11 +020092 { NS2_SNS_EV_REQ_CHANGE_WEIGHT, "REQ_CHANGE_WEIGHT"},
Alexander Couzens6a161492020-07-12 13:45:50 +020093 { 0, NULL }
94};
95
Alexander Couzens1f3193d2021-06-05 22:08:11 +020096enum sns_procedure {
97 SNS_PROC_NONE, /*!< used as invalid/idle value */
98 SNS_PROC_ADD,
99 SNS_PROC_DEL,
100 SNS_PROC_CHANGE_WEIGHT,
101};
102
Alexander Couzense769f522020-12-07 07:37:07 +0100103struct sns_endpoint {
104 struct llist_head list;
105 struct osmo_sockaddr saddr;
106};
107
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100108struct ns2_sns_bind {
109 struct llist_head list;
110 struct gprs_ns2_vc_bind *bind;
Alexander Couzens1f3193d2021-06-05 22:08:11 +0200111 uint8_t change_weight_state;
112};
113
114struct ns2_sns_procedure {
115 struct llist_head list;
116 struct ns2_sns_bind *sbind;
117 uint16_t sig_weight;
118 uint16_t data_weight;
119 /* copy entry to protect against changes of gss->local */
120 struct gprs_ns_ie_ip4_elem ip4;
121 struct gprs_ns_ie_ip6_elem ip6;
122 enum sns_procedure procedure;
123 uint8_t trans_id;
124 /* is the procedure in process */
125 bool running;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100126};
127
Alexander Couzens71128672021-06-05 18:44:01 +0200128struct ns2_sns_elems {
129 struct gprs_ns_ie_ip4_elem *ip4;
130 unsigned int num_ip4;
131 struct gprs_ns_ie_ip6_elem *ip6;
132 unsigned int num_ip6;
133};
134
Alexander Couzens6a161492020-07-12 13:45:50 +0200135struct ns2_sns_state {
136 struct gprs_ns2_nse *nse;
137
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200138 /* containing the address family AF_* */
139 int family;
Harald Welte4f127462021-03-02 20:49:10 +0100140 enum ns2_sns_role role; /* local role: BSS or SGSN */
Alexander Couzens6a161492020-07-12 13:45:50 +0200141
Alexander Couzense769f522020-12-07 07:37:07 +0100142 /* holds the list of initial SNS endpoints */
143 struct llist_head sns_endpoints;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100144 /* list of used struct ns2_sns_bind */
145 struct llist_head binds;
146 /* pointer to the bind which was used to initiate the SNS connection */
147 struct ns2_sns_bind *initial_bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100148 /* prevent recursive reselection */
149 bool reselection_running;
150
151 /* The current initial SNS endpoints.
152 * The initial connection will be moved into the NSE
153 * if configured via SNS. Otherwise it will be removed
154 * in configured state. */
155 struct sns_endpoint *initial;
Alexander Couzens6a161492020-07-12 13:45:50 +0200156 /* all SNS PDU will be sent over this nsvc */
157 struct gprs_ns2_vc *sns_nsvc;
Alexander Couzens90ee9632020-12-07 06:18:32 +0100158 /* timer N */
159 int N;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100160 /* true if at least one nsvc is alive */
161 bool alive;
Alexander Couzens6a161492020-07-12 13:45:50 +0200162
163 /* local configuration to send to the remote end */
Alexander Couzens71128672021-06-05 18:44:01 +0200164 struct ns2_sns_elems local;
Alexander Couzens6a161492020-07-12 13:45:50 +0200165
Alexander Couzens1f3193d2021-06-05 22:08:11 +0200166 /* local configuration after all local procedures applied */
167 struct ns2_sns_elems local_procedure;
168
Alexander Couzens71128672021-06-05 18:44:01 +0200169 /* remote configuration as received */
170 struct ns2_sns_elems remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200171
172 /* local configuration about our capabilities in terms of connections to
173 * remote (SGSN) side */
174 size_t num_max_nsvcs;
175 size_t num_max_ip4_remote;
176 size_t num_max_ip6_remote;
Alexander Couzens1f3193d2021-06-05 22:08:11 +0200177
178 struct llist_head procedures;
179 struct ns2_sns_procedure *current_procedure;
180 uint8_t trans_id;
Alexander Couzens6a161492020-07-12 13:45:50 +0200181};
182
183static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi)
184{
185 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
186 return gss->nse;
187}
188
Alexander Couzens652ab4d2021-06-12 23:09:46 +0200189/* The SNS has failed. Etither restart the SNS (BSS) or remove the SNS (SGSN) */
190#define sns_failed(fi, reason) \
191 _sns_failed(fi, reason, __FILE__, __LINE__)
192static void _sns_failed(struct osmo_fsm_inst *fi, const char *reason, const char *file, int line)
193{
194 struct ns2_sns_state *gss = fi->priv;
195
196 if (reason)
197 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SNS failed: %s\n", gss->nse->nsei, reason);
198
199 if (gss->role == GPRS_SNS_ROLE_SGSN) {
200 if (!gss->nse->persistent)
201 gprs_ns2_free_nse(gss->nse);
202 else
203 _osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0, file, line);
204 } else {
Alexander Couzens175eb7b2021-07-20 18:41:14 +0200205 _osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, NULL, file, line);
Alexander Couzens652ab4d2021-06-12 23:09:46 +0200206 }
207}
208
Alexander Couzens6a161492020-07-12 13:45:50 +0200209/* helper function to compute the sum of all (data or signaling) weights */
Alexander Couzens62310552021-06-06 02:43:14 +0200210static int ip4_weight_sum(const struct ns2_sns_elems *elems, bool data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200211{
212 unsigned int i;
213 int weight_sum = 0;
214
Alexander Couzens62310552021-06-06 02:43:14 +0200215 for (i = 0; i < elems->num_ip4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200216 if (data_weight)
Alexander Couzens62310552021-06-06 02:43:14 +0200217 weight_sum += elems->ip4[i].data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200218 else
Alexander Couzens62310552021-06-06 02:43:14 +0200219 weight_sum += elems->ip4[i].sig_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200220 }
221 return weight_sum;
222}
Alexander Couzens62310552021-06-06 02:43:14 +0200223#define ip4_weight_sum_data(elems) ip4_weight_sum(elems, true)
224#define ip4_weight_sum_sig(elems) ip4_weight_sum(elems, false)
Alexander Couzens6a161492020-07-12 13:45:50 +0200225
226/* helper function to compute the sum of all (data or signaling) weights */
Alexander Couzens62310552021-06-06 02:43:14 +0200227static int ip6_weight_sum(const struct ns2_sns_elems *elems, bool data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200228{
229 unsigned int i;
230 int weight_sum = 0;
231
Alexander Couzens62310552021-06-06 02:43:14 +0200232 for (i = 0; i < elems->num_ip6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200233 if (data_weight)
Alexander Couzens62310552021-06-06 02:43:14 +0200234 weight_sum += elems->ip6[i].data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200235 else
Alexander Couzens62310552021-06-06 02:43:14 +0200236 weight_sum += elems->ip6[i].sig_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200237 }
238 return weight_sum;
239}
Alexander Couzens62310552021-06-06 02:43:14 +0200240#define ip6_weight_sum_data(elems) ip6_weight_sum(elems, true)
241#define ip6_weight_sum_sig(elems) ip6_weight_sum(elems, false)
Alexander Couzens6a161492020-07-12 13:45:50 +0200242
Alexander Couzens019da4b2021-06-06 02:48:18 +0200243static int ip46_weight_sum(const struct ns2_sns_elems *elems, bool data_weight)
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100244{
Alexander Couzens019da4b2021-06-06 02:48:18 +0200245 return ip4_weight_sum(elems, data_weight) +
246 ip6_weight_sum(elems, data_weight);
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100247}
Alexander Couzens019da4b2021-06-06 02:48:18 +0200248#define ip46_weight_sum_data(elems) ip46_weight_sum(elems, true)
249#define ip46_weight_sum_sig(elems) ip46_weight_sum(elems, false)
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100250
Alexander Couzens6a161492020-07-12 13:45:50 +0200251static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
252 const struct gprs_ns_ie_ip4_elem *ip4)
253{
254 struct osmo_sockaddr sa;
255 /* copy over. Both data structures use network byte order */
256 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
257 sa.u.sin.sin_port = ip4->udp_port;
258 sa.u.sin.sin_family = AF_INET;
259
Alexander Couzens38b19e82020-09-23 23:56:37 +0200260 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200261}
262
263static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
264 const struct gprs_ns_ie_ip6_elem *ip6)
265{
266 struct osmo_sockaddr sa;
267 /* copy over. Both data structures use network byte order */
268 sa.u.sin6.sin6_addr = ip6->ip_addr;
269 sa.u.sin6.sin6_port = ip6->udp_port;
270 sa.u.sin6.sin6_family = AF_INET;
271
Alexander Couzens38b19e82020-09-23 23:56:37 +0200272 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200273}
274
Alexander Couzens125298f2020-10-11 21:22:42 +0200275/*! Return the initial SNS remote socket address
276 * \param nse NS Entity
277 * \return address of the initial SNS connection; NULL in case of error
278 */
279const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
280{
281 struct ns2_sns_state *gss;
282
283 if (!nse->bss_sns_fi)
284 return NULL;
285
286 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100287 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200288}
289
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100290/*! called when a nsvc is beeing freed or the nsvc became dead */
291void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200292{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100293 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200294 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100295 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200296 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200297
298 if (!fi)
299 return;
300
301 gss = (struct ns2_sns_state *) fi->priv;
302 if (nsvc != gss->sns_nsvc)
303 return;
304
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100305 gss->sns_nsvc = NULL;
306 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200307 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100308 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200309 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100310 return;
311 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200312 }
313 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100314 /* the SNS is waiting for its first NS-VC to come up
315 * choose any other nsvc */
316 llist_for_each_entry(tmp, &nse->nsvc, list) {
317 if (nsvc != tmp) {
318 gss->sns_nsvc = tmp;
319 return;
320 }
321 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200322 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100323
Alexander Couzens175eb7b2021-07-20 18:41:14 +0200324 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200325}
326
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200327static void ns2_clear_elems(struct ns2_sns_elems *elems)
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100328{
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200329 TALLOC_FREE(elems->ip4);
330 TALLOC_FREE(elems->ip6);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100331
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200332 elems->num_ip4 = 0;
333 elems->num_ip6 = 0;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100334}
335
Alexander Couzens1f3193d2021-06-05 22:08:11 +0200336static void ns2_clear_procedures(struct ns2_sns_state *gss)
337{
338 struct ns2_sns_procedure *procedure, *tmp;
339 gss->current_procedure = NULL;
340 llist_for_each_entry_safe(procedure, tmp, &gss->procedures, list) {
341 llist_del(&procedure->list);
342 talloc_free(procedure);
343 }
344}
345
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100346static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
347 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200348{
349 struct gprs_ns2_inst *nsi = nse->nsi;
350 struct gprs_ns2_vc *nsvc;
351 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100352
353 /* for every bind, create a connection if bind type == IP */
354 llist_for_each_entry(bind, &nsi->binding, list) {
355 if (bind->ll != GPRS_NS2_LL_UDP)
356 continue;
357 /* ignore failed connection */
358 nsvc = gprs_ns2_ip_connect_inactive(bind,
359 remote,
360 nse, 0);
361 if (!nsvc) {
362 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
363 continue;
364 }
365
366 nsvc->sig_weight = sig_weight;
367 nsvc->data_weight = data_weight;
368 }
369}
370
371static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
372 struct gprs_ns2_nse *nse,
373 const struct gprs_ns_ie_ip4_elem *ip4)
374{
Alexander Couzensc068d862020-10-12 04:11:51 +0200375 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200376 /* copy over. Both data structures use network byte order */
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
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100381 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200382}
383
384static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
385 struct gprs_ns2_nse *nse,
386 const struct gprs_ns_ie_ip6_elem *ip6)
387{
Alexander Couzens6a161492020-07-12 13:45:50 +0200388 struct osmo_sockaddr remote = {};
389 /* copy over. Both data structures use network byte order */
390 remote.u.sin6.sin6_family = AF_INET6;
391 remote.u.sin6.sin6_addr = ip6->ip_addr;
392 remote.u.sin6.sin6_port = ip6->udp_port;
393
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100394 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200395}
396
Harald Weltee8c61062021-03-24 13:16:27 +0100397static struct gprs_ns2_vc *nsvc_for_bind_and_remote(struct gprs_ns2_nse *nse,
398 struct gprs_ns2_vc_bind *bind,
399 const struct osmo_sockaddr *remote)
400{
401 struct gprs_ns2_vc *nsvc;
402
403 llist_for_each_entry(nsvc, &nse->nsvc, list) {
404 if (nsvc->bind != bind)
405 continue;
406
407 if (!osmo_sockaddr_cmp(remote, gprs_ns2_ip_vc_remote(nsvc)))
408 return nsvc;
409 }
410 return NULL;
411}
Alexander Couzens6a161492020-07-12 13:45:50 +0200412
413static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
414{
415 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
416 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
417 struct gprs_ns2_vc *nsvc;
Harald Welte3053bbb2021-03-24 13:22:18 +0100418 struct ns2_sns_bind *sbind;
Alexander Couzens6a161492020-07-12 13:45:50 +0200419 struct osmo_sockaddr remote = { };
420 unsigned int i;
421
Harald Weltee8c61062021-03-24 13:16:27 +0100422 /* iterate over all remote IPv4 endpoints */
Alexander Couzens71128672021-06-05 18:44:01 +0200423 for (i = 0; i < gss->remote.num_ip4; i++) {
424 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->remote.ip4[i];
Alexander Couzens6a161492020-07-12 13:45:50 +0200425
426 remote.u.sin.sin_family = AF_INET;
427 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
428 remote.u.sin.sin_port = ip4->udp_port;
429
Harald Welte3053bbb2021-03-24 13:22:18 +0100430 /* iterate over all local binds within this SNS */
431 llist_for_each_entry(sbind, &gss->binds, list) {
432 struct gprs_ns2_vc_bind *bind = sbind->bind;
433
Harald Weltee8c61062021-03-24 13:16:27 +0100434 /* we only care about UDP binds */
Daniel Willmann967e2c12021-01-14 16:58:17 +0100435 if (bind->ll != GPRS_NS2_LL_UDP)
436 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200437
Harald Weltee8c61062021-03-24 13:16:27 +0100438 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
439 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200440 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
441 if (!nsvc) {
442 /* TODO: add to a list to send back a NS-STATUS */
443 continue;
444 }
445 }
446
447 /* update data / signalling weight */
448 nsvc->data_weight = ip4->data_weight;
449 nsvc->sig_weight = ip4->sig_weight;
450 nsvc->sns_only = false;
451 }
452 }
453
Harald Weltee8c61062021-03-24 13:16:27 +0100454 /* iterate over all remote IPv4 endpoints */
Alexander Couzens71128672021-06-05 18:44:01 +0200455 for (i = 0; i < gss->remote.num_ip6; i++) {
456 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->remote.ip6[i];
Alexander Couzens6a161492020-07-12 13:45:50 +0200457
458 remote.u.sin6.sin6_family = AF_INET6;
459 remote.u.sin6.sin6_addr = ip6->ip_addr;
460 remote.u.sin6.sin6_port = ip6->udp_port;
461
Harald Welte3053bbb2021-03-24 13:22:18 +0100462 /* iterate over all local binds within this SNS */
463 llist_for_each_entry(sbind, &gss->binds, list) {
464 struct gprs_ns2_vc_bind *bind = sbind->bind;
465
Daniel Willmann967e2c12021-01-14 16:58:17 +0100466 if (bind->ll != GPRS_NS2_LL_UDP)
467 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200468
Harald Weltee8c61062021-03-24 13:16:27 +0100469 /* we only care about UDP binds */
470 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
471 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200472 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
473 if (!nsvc) {
474 /* TODO: add to a list to send back a NS-STATUS */
475 continue;
476 }
477 }
478
479 /* update data / signalling weight */
480 nsvc->data_weight = ip6->data_weight;
481 nsvc->sig_weight = ip6->sig_weight;
482 nsvc->sns_only = false;
483 }
484 }
485
486
487 return 0;
488}
489
490/* Add a given remote IPv4 element to gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200491static int add_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
492 const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +0200493{
Alexander Couzens6a161492020-07-12 13:45:50 +0200494 /* check for duplicates */
Alexander Couzenscc56ddc2021-06-06 03:33:35 +0200495 for (unsigned int i = 0; i < elems->num_ip4; i++) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200496 if (memcmp(&elems->ip4[i], ip4, sizeof(*ip4)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200497 continue;
Alexander Couzensd3507e82021-06-06 03:32:32 +0200498 return -1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200499 }
500
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200501 elems->ip4 = talloc_realloc(gss, elems->ip4, struct gprs_ns_ie_ip4_elem,
502 elems->num_ip4+1);
503 elems->ip4[elems->num_ip4] = *ip4;
504 elems->num_ip4 += 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200505 return 0;
506}
507
508/* Remove a given remote IPv4 element from gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200509static int remove_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
510 const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +0200511{
512 unsigned int i;
513
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200514 for (i = 0; i < elems->num_ip4; i++) {
515 if (memcmp(&elems->ip4[i], ip4, sizeof(*ip4)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200516 continue;
517 /* all array elements < i remain as they are; all > i are shifted left by one */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200518 memmove(&elems->ip4[i], &elems->ip4[i+1], elems->num_ip4-i-1);
519 elems->num_ip4 -= 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200520 return 0;
521 }
522 return -1;
523}
524
525/* update the weights for specified remote IPv4 */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200526static int update_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
527 const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +0200528{
529 unsigned int i;
530
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200531 for (i = 0; i < elems->num_ip4; i++) {
532 if (elems->ip4[i].ip_addr != ip4->ip_addr ||
533 elems->ip4[i].udp_port != ip4->udp_port)
Alexander Couzens6a161492020-07-12 13:45:50 +0200534 continue;
535
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200536 elems->ip4[i].sig_weight = ip4->sig_weight;
537 elems->ip4[i].data_weight = ip4->data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200538 return 0;
539 }
540 return -1;
541}
542
543/* Add a given remote IPv6 element to gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200544static int add_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
545 const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200546{
Alexander Couzenscc56ddc2021-06-06 03:33:35 +0200547 /* check for duplicates */
548 for (unsigned int i = 0; i < elems->num_ip6; i++) {
549 if (memcmp(&elems->ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
550 elems->ip6[i].udp_port != ip6->udp_port)
551 continue;
552 return -1;
553 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200554
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200555 elems->ip6 = talloc_realloc(gss, elems->ip6, struct gprs_ns_ie_ip6_elem,
556 elems->num_ip6+1);
557 elems->ip6[elems->num_ip6] = *ip6;
558 elems->num_ip6 += 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200559 return 0;
560}
561
562/* Remove a given remote IPv6 element from gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200563static int remove_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
564 const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200565{
566 unsigned int i;
567
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200568 for (i = 0; i < elems->num_ip6; i++) {
569 if (memcmp(&elems->ip6[i], ip6, sizeof(*ip6)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200570 continue;
571 /* all array elements < i remain as they are; all > i are shifted left by one */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200572 memmove(&elems->ip6[i], &elems->ip6[i+1], elems->num_ip6-i-1);
573 elems->num_ip6 -= 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200574 return 0;
575 }
576 return -1;
577}
578
579/* update the weights for specified remote IPv6 */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200580static int update_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
581 const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200582{
583 unsigned int i;
584
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200585 for (i = 0; i < elems->num_ip6; i++) {
586 if (memcmp(&elems->ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
587 elems->ip6[i].udp_port != ip6->udp_port)
Alexander Couzens6a161492020-07-12 13:45:50 +0200588 continue;
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200589 elems->ip6[i].sig_weight = ip6->sig_weight;
590 elems->ip6[i].data_weight = ip6->data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200591 return 0;
592 }
593 return -1;
594}
595
596static 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)
597{
598 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
599 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
600 struct gprs_ns2_vc *nsvc;
601 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200602 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200603 uint8_t new_signal;
604 uint8_t new_data;
605
606 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
607 * signalling weights of all the peer IP endpoints configured for this NSE is
608 * equal to zero or if the resulting sum of the data weights of all the peer IP
609 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
610 * SNS-ACK PDU with a cause code of "Invalid weights". */
611
612 if (ip4) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200613 if (update_ip4_elem(gss, &gss->remote, ip4))
Alexander Couzens6a161492020-07-12 13:45:50 +0200614 return -NS_CAUSE_UNKN_IP_EP;
615
616 /* copy over. Both data structures use network byte order */
617 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
618 sa.u.sin.sin_port = ip4->udp_port;
619 sa.u.sin.sin_family = AF_INET;
620 new_signal = ip4->sig_weight;
621 new_data = ip4->data_weight;
622 } else if (ip6) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200623 if (update_ip6_elem(gss, &gss->remote, ip6))
Alexander Couzens6a161492020-07-12 13:45:50 +0200624 return -NS_CAUSE_UNKN_IP_EP;
625
626 /* copy over. Both data structures use network byte order */
627 sa.u.sin6.sin6_addr = ip6->ip_addr;
628 sa.u.sin6.sin6_port = ip6->udp_port;
629 sa.u.sin6.sin6_family = AF_INET6;
630 new_signal = ip6->sig_weight;
631 new_data = ip6->data_weight;
632 } else {
633 OSMO_ASSERT(false);
634 }
635
636 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200637 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200638 /* all nsvc in NSE should be IP/UDP nsvc */
639 OSMO_ASSERT(remote);
640
641 if (osmo_sockaddr_cmp(&sa, remote))
642 continue;
643
644 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
645 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
646 nsvc->sig_weight, new_signal);
647
648 nsvc->data_weight = new_data;
649 nsvc->sig_weight = new_signal;
650 }
651
652 return 0;
653}
654
655static int do_sns_delete(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, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200662 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200663 struct osmo_sockaddr sa = {};
664
665 if (ip4) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200666 if (remove_ip4_elem(gss, &gss->remote, ip4) < 0)
Alexander Couzens6a161492020-07-12 13:45:50 +0200667 return -NS_CAUSE_UNKN_IP_EP;
668 /* copy over. Both data structures use network byte order */
669 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
670 sa.u.sin.sin_port = ip4->udp_port;
671 sa.u.sin.sin_family = AF_INET;
672 } else if (ip6) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200673 if (remove_ip6_elem(gss, &gss->remote, ip6))
Alexander Couzens6a161492020-07-12 13:45:50 +0200674 return -NS_CAUSE_UNKN_IP_EP;
675
676 /* copy over. Both data structures use network byte order */
677 sa.u.sin6.sin6_addr = ip6->ip_addr;
678 sa.u.sin6.sin6_port = ip6->udp_port;
679 sa.u.sin6.sin6_family = AF_INET6;
680 } else {
681 OSMO_ASSERT(false);
682 }
683
684 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200685 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200686 /* all nsvc in NSE should be IP/UDP nsvc */
687 OSMO_ASSERT(remote);
688 if (osmo_sockaddr_cmp(&sa, remote))
689 continue;
690
691 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
692 gprs_ns2_free_nsvc(nsvc);
693 }
694
695 return 0;
696}
697
698static int do_sns_add(struct osmo_fsm_inst *fi,
699 const struct gprs_ns_ie_ip4_elem *ip4,
700 const struct gprs_ns_ie_ip6_elem *ip6)
701{
702 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
703 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
704 struct gprs_ns2_vc *nsvc;
705 int rc = 0;
706
707 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
708 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
709 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200710 switch (gss->family) {
711 case AF_INET:
Alexander Couzensd3507e82021-06-06 03:32:32 +0200712 if (gss->remote.num_ip4 >= gss->num_max_ip4_remote)
713 return -NS_CAUSE_INVAL_NR_NS_VC;
714 /* TODO: log message duplicate */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200715 rc = add_ip4_elem(gss, &gss->remote, ip4);
Alexander Couzens6a161492020-07-12 13:45:50 +0200716 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200717 case AF_INET6:
Alexander Couzensd3507e82021-06-06 03:32:32 +0200718 if (gss->remote.num_ip6 >= gss->num_max_ip6_remote)
719 return -NS_CAUSE_INVAL_NR_NS_VC;
720 /* TODO: log message duplicate */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200721 rc = add_ip6_elem(gss, &gss->remote, ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200722 break;
723 default:
724 /* the gss->ip is initialized with the bss */
725 OSMO_ASSERT(false);
726 }
727
728 if (rc)
Alexander Couzensd3507e82021-06-06 03:32:32 +0200729 return -NS_CAUSE_PROTO_ERR_UNSPEC;
Alexander Couzens6a161492020-07-12 13:45:50 +0200730
731 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
732 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
733 * unspecified" */
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200734 switch (gss->family) {
735 case AF_INET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200736 nsvc = nsvc_by_ip4_elem(nse, ip4);
737 if (nsvc) {
738 /* the nsvc should be already in sync with the ip4 / ip6 elements */
739 return -NS_CAUSE_PROTO_ERR_UNSPEC;
740 }
741
742 /* TODO: failure case */
743 ns2_nsvc_create_ip4(fi, nse, ip4);
744 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200745 case AF_INET6:
Alexander Couzens6a161492020-07-12 13:45:50 +0200746 nsvc = nsvc_by_ip6_elem(nse, ip6);
747 if (nsvc) {
748 /* the nsvc should be already in sync with the ip4 / ip6 elements */
749 return -NS_CAUSE_PROTO_ERR_UNSPEC;
750 }
751
752 /* TODO: failure case */
753 ns2_nsvc_create_ip6(fi, nse, ip6);
754 break;
755 }
756
757 gprs_ns2_start_alive_all_nsvcs(nse);
758
759 return 0;
760}
761
762
Harald Welte694dad52021-03-23 15:22:16 +0100763static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200764{
Harald Weltef61a9152021-03-02 22:20:17 +0100765 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
766 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzense769f522020-12-07 07:37:07 +0100767 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200768}
769
Harald Welte694dad52021-03-23 15:22:16 +0100770static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200771{
Harald Weltef61a9152021-03-02 22:20:17 +0100772 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200773 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
774 struct gprs_ns2_inst *nsi = nse->nsi;
775 struct tlv_parsed *tp = NULL;
776
Harald Weltef61a9152021-03-02 22:20:17 +0100777 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
778
Alexander Couzens6a161492020-07-12 13:45:50 +0200779 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +0200780 case NS2_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200781 tp = data;
782 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
783 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
784 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
785 /* TODO: What to do? */
786 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100787 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200788 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
789 }
790 break;
791 default:
792 OSMO_ASSERT(0);
793 }
794}
795
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200796static int ns2_sns_count_num_local_ep(struct osmo_fsm_inst *fi, int ip_proto)
Harald Welte01fa6a32021-03-04 19:49:38 +0100797{
798 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
799 struct ns2_sns_bind *sbind;
800 int count = 0;
801
802 llist_for_each_entry(sbind, &gss->binds, list) {
803 const struct osmo_sockaddr *sa = gprs_ns2_ip_bind_sockaddr(sbind->bind);
804 if (!sa)
805 continue;
806
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200807 switch (ip_proto) {
808 case AF_INET:
Harald Welte01fa6a32021-03-04 19:49:38 +0100809 if (sa->u.sas.ss_family == AF_INET)
810 count++;
811 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200812 case AF_INET6:
Harald Welte01fa6a32021-03-04 19:49:38 +0100813 if (sa->u.sas.ss_family == AF_INET6)
814 count++;
815 break;
816 }
817 }
818 return count;
819}
820
Alexander Couzens1f3193d2021-06-05 22:08:11 +0200821static int ns2_sns_copy_local_endpoints(struct ns2_sns_state *gss)
822{
823 switch (gss->family) {
824 case AF_INET:
825 gss->local_procedure.ip4 = talloc_realloc(gss, gss->local_procedure.ip4, struct gprs_ns_ie_ip4_elem,
826 gss->local.num_ip4);
827 if (!gss->local_procedure.ip4)
828 return -ENOMEM;
829
830 gss->local_procedure.num_ip4 = gss->local.num_ip4;
831 memcpy(gss->local_procedure.ip4, gss->local.ip4,
832 sizeof(struct gprs_ns_ie_ip4_elem) * gss->local.num_ip4);
833 break;
834 case AF_INET6:
835 gss->local_procedure.ip6 = talloc_realloc(gss, gss->local_procedure.ip6, struct gprs_ns_ie_ip6_elem,
836 gss->local.num_ip6);
837 if (!gss->local_procedure.ip6)
838 return -ENOMEM;
839
840 gss->local_procedure.num_ip6 = gss->local.num_ip6;
841 memcpy(gss->local_procedure.ip6, gss->local.ip6,
842 sizeof(struct gprs_ns_ie_ip6_elem) * gss->local.num_ip6);
843 break;
844 default:
845 OSMO_ASSERT(0);
846 }
847
848 return 0;
849}
850
Harald Welte24920e22021-03-04 13:03:27 +0100851static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200852{
853 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100854 struct gprs_ns_ie_ip4_elem *ip4_elems;
855 struct gprs_ns_ie_ip6_elem *ip6_elems;
856 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100857 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100858 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100859 const struct osmo_sockaddr *sa;
860 struct osmo_sockaddr local;
861 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200862
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200863 ns2_clear_elems(&gss->local);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100864
Alexander Couzense769f522020-12-07 07:37:07 +0100865 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100866 if (gss->role == GPRS_SNS_ROLE_BSS) {
867 if (!gss->initial)
868 return;
869 remote = &gss->initial->saddr;
870 } else
871 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100872
873 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100874 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100875 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100876 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100877 return;
878 }
879
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200880 switch (gss->family) {
881 case AF_INET:
Alexander Couzens71128672021-06-05 18:44:01 +0200882 ip4_elems = talloc_realloc(fi, gss->local.ip4, struct gprs_ns_ie_ip4_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100883 if (!ip4_elems)
884 return;
885
Alexander Couzens71128672021-06-05 18:44:01 +0200886 gss->local.ip4 = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100887 llist_for_each_entry(sbind, &gss->binds, list) {
888 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100889 sa = gprs_ns2_ip_bind_sockaddr(bind);
890 if (!sa)
891 continue;
892
893 if (sa->u.sas.ss_family != AF_INET)
894 continue;
895
896 /* check if this is an specific bind */
897 if (sa->u.sin.sin_addr.s_addr == 0) {
898 if (osmo_sockaddr_local_ip(&local, remote))
899 continue;
900
901 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
902 } else {
903 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
904 }
905
906 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100907 ip4_elems->sig_weight = bind->sns_sig_weight;
908 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100909 ip4_elems++;
910 }
911
Alexander Couzens71128672021-06-05 18:44:01 +0200912 gss->local.num_ip4 = count;
913 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->local.num_ip4, 8);
Alexander Couzense769f522020-12-07 07:37:07 +0100914 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200915 case AF_INET6:
Alexander Couzense769f522020-12-07 07:37:07 +0100916 /* IPv6 */
Alexander Couzens71128672021-06-05 18:44:01 +0200917 ip6_elems = talloc_realloc(fi, gss->local.ip6, struct gprs_ns_ie_ip6_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100918 if (!ip6_elems)
919 return;
920
Alexander Couzens71128672021-06-05 18:44:01 +0200921 gss->local.ip6 = ip6_elems;
Alexander Couzense769f522020-12-07 07:37:07 +0100922
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100923 llist_for_each_entry(sbind, &gss->binds, list) {
924 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100925 sa = gprs_ns2_ip_bind_sockaddr(bind);
926 if (!sa)
927 continue;
928
929 if (sa->u.sas.ss_family != AF_INET6)
930 continue;
931
932 /* check if this is an specific bind */
933 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
934 if (osmo_sockaddr_local_ip(&local, remote))
935 continue;
936
937 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
938 } else {
939 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
940 }
941
942 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100943 ip6_elems->sig_weight = bind->sns_sig_weight;
944 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100945
946 ip6_elems++;
947 }
Alexander Couzens71128672021-06-05 18:44:01 +0200948 gss->local.num_ip6 = count;
949 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->local.num_ip6, 8);
Alexander Couzense769f522020-12-07 07:37:07 +0100950 break;
951 }
Alexander Couzens1f3193d2021-06-05 22:08:11 +0200952
953 ns2_sns_copy_local_endpoints(gss);
Harald Welte24920e22021-03-04 13:03:27 +0100954}
955
Alexander Couzens6608ce92021-04-26 20:39:46 +0200956static void ns2_sns_choose_next_bind(struct ns2_sns_state *gss)
957{
958 /* take the first bind or take the next bind */
959 if (!gss->initial_bind || gss->initial_bind->list.next == &gss->binds)
960 gss->initial_bind = llist_first_entry_or_null(&gss->binds, struct ns2_sns_bind, list);
961 else
962 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
963}
964
Harald Welte24920e22021-03-04 13:03:27 +0100965/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Harald Welte694dad52021-03-23 15:22:16 +0100966static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Harald Welte24920e22021-03-04 13:03:27 +0100967{
968 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
969
Harald Weltef61a9152021-03-02 22:20:17 +0100970 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
971
Harald Welte24920e22021-03-04 13:03:27 +0100972 /* on a generic failure, the timer callback will recover */
973 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
974 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Harald Welte694dad52021-03-23 15:22:16 +0100975 if (old_state != GPRS_SNS_ST_BSS_SIZE)
Harald Welte24920e22021-03-04 13:03:27 +0100976 gss->N = 0;
977
Alexander Couzens1f3193d2021-06-05 22:08:11 +0200978 ns2_clear_procedures(gss);
Harald Welte24920e22021-03-04 13:03:27 +0100979 gss->alive = false;
980
981 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens6608ce92021-04-26 20:39:46 +0200982 ns2_sns_choose_next_bind(gss);
Harald Welte24920e22021-03-04 13:03:27 +0100983
984 /* setup the NSVC */
985 if (!gss->sns_nsvc) {
986 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
987 struct osmo_sockaddr *remote = &gss->initial->saddr;
988 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
989 if (!gss->sns_nsvc)
990 return;
Harald Weltec962a2e2021-03-05 08:09:08 +0100991 /* A pre-configured endpoint shall not be used for NSE data or signalling traffic
992 * (with the exception of Size and Configuration procedures) unless it is configured
993 * by the SGSN using the auto-configuration procedures */
Harald Welte24920e22021-03-04 13:03:27 +0100994 gss->sns_nsvc->sns_only = true;
995 }
996
Alexander Couzens6a161492020-07-12 13:45:50 +0200997 if (gss->num_max_ip4_remote > 0)
Alexander Couzens71128672021-06-05 18:44:01 +0200998 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->local.num_ip4, -1);
Alexander Couzens6a161492020-07-12 13:45:50 +0200999 else
Alexander Couzens71128672021-06-05 18:44:01 +02001000 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->local.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +02001001}
1002
Harald Welte694dad52021-03-23 15:22:16 +01001003static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +02001004{
Harald Weltef61a9152021-03-02 22:20:17 +01001005 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens3df58862021-02-05 17:18:08 +01001006 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Harald Weltef61a9152021-03-02 22:20:17 +01001007 struct tlv_parsed *tp = NULL;
1008
1009 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzens6a161492020-07-12 13:45:50 +02001010
1011 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001012 case NS2_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +02001013 tp = (struct tlv_parsed *) data;
1014 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
1015 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
1016 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
1017 /* TODO: What to do? */
1018 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001019 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 +02001020 }
1021 break;
1022 default:
1023 OSMO_ASSERT(0);
1024 }
1025}
1026
Harald Welte694dad52021-03-23 15:22:16 +01001027static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +02001028{
1029 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +01001030
Harald Weltef61a9152021-03-02 22:20:17 +01001031 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1032
Harald Welte694dad52021-03-23 15:22:16 +01001033 if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS)
Alexander Couzens790a9632021-02-05 17:18:39 +01001034 gss->N = 0;
1035
Alexander Couzens6a161492020-07-12 13:45:50 +02001036 /* Transmit SNS-CONFIG */
Alexander Couzens077ce5a2021-06-06 01:32:45 +02001037 switch (gss->family) {
1038 case AF_INET:
Alexander Couzens6a161492020-07-12 13:45:50 +02001039 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzens71128672021-06-05 18:44:01 +02001040 gss->local.ip4, gss->local.num_ip4,
Alexander Couzense78207f2020-12-07 06:19:29 +01001041 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001042 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +02001043 case AF_INET6:
Alexander Couzens6a161492020-07-12 13:45:50 +02001044 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +01001045 NULL, 0,
Alexander Couzens71128672021-06-05 18:44:01 +02001046 gss->local.ip6, gss->local.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +02001047 break;
1048 }
1049}
1050
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001051/* calculate the timeout of the configured state. the configured
1052 * state will fail if not at least one NS-VC is alive within X second.
1053 */
1054static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
1055{
1056 int secs;
1057 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
1058 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
1059 secs += nsi->timeout[NS_TOUT_TNS_TEST];
1060
1061 return secs;
1062}
Alexander Couzens6a161492020-07-12 13:45:50 +02001063
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001064/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
1065static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001066{
1067 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001068
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001069 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1070 const struct gprs_ns_ie_ip4_elem *v4_list;
1071 unsigned int num_v4;
1072 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1073 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +02001074
Alexander Couzens71128672021-06-05 18:44:01 +02001075 if (num_v4 && gss->remote.ip6)
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001076 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +02001077
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001078 /* realloc to the new size */
Alexander Couzens71128672021-06-05 18:44:01 +02001079 gss->remote.ip4 = talloc_realloc(gss, gss->remote.ip4,
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001080 struct gprs_ns_ie_ip4_elem,
Alexander Couzens71128672021-06-05 18:44:01 +02001081 gss->remote.num_ip4 + num_v4);
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001082 /* append the new entries to the end of the list */
Alexander Couzens71128672021-06-05 18:44:01 +02001083 memcpy(&gss->remote.ip4[gss->remote.num_ip4], v4_list, num_v4*sizeof(*v4_list));
1084 gss->remote.num_ip4 += num_v4;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001085
1086 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
Alexander Couzens71128672021-06-05 18:44:01 +02001087 gss->remote.num_ip4);
Alexander Couzens6a161492020-07-12 13:45:50 +02001088 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001089
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001090 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1091 const struct gprs_ns_ie_ip6_elem *v6_list;
1092 unsigned int num_v6;
1093 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1094 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
1095
Alexander Couzens71128672021-06-05 18:44:01 +02001096 if (num_v6 && gss->remote.ip4)
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001097 return -NS_CAUSE_INVAL_NR_IPv6_EP;
1098
1099 /* realloc to the new size */
Alexander Couzens71128672021-06-05 18:44:01 +02001100 gss->remote.ip6 = talloc_realloc(gss, gss->remote.ip6,
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001101 struct gprs_ns_ie_ip6_elem,
Alexander Couzens71128672021-06-05 18:44:01 +02001102 gss->remote.num_ip6 + num_v6);
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001103 /* append the new entries to the end of the list */
Alexander Couzens71128672021-06-05 18:44:01 +02001104 memcpy(&gss->remote.ip6[gss->remote.num_ip6], v6_list, num_v6*sizeof(*v6_list));
1105 gss->remote.num_ip6 += num_v6;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001106
Alexander Couzens71128672021-06-05 18:44:01 +02001107 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %d entries\n",
1108 gss->remote.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +02001109 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001110
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001111 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001112}
1113
Harald Welte694dad52021-03-23 15:22:16 +01001114static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens790a9632021-02-05 17:18:39 +01001115{
1116 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1117
Harald Weltef61a9152021-03-02 22:20:17 +01001118 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1119
Harald Welte694dad52021-03-23 15:22:16 +01001120 if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN)
Alexander Couzens790a9632021-02-05 17:18:39 +01001121 gss->N = 0;
1122}
1123
Harald Welte694dad52021-03-23 15:22:16 +01001124static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +02001125{
1126 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001127 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1128 uint8_t cause;
1129 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001130
Harald Weltef61a9152021-03-02 22:20:17 +01001131 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1132
Alexander Couzens6a161492020-07-12 13:45:50 +02001133 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001134 case NS2_SNS_EV_RX_CONFIG_END:
1135 case NS2_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001136 rc = ns_sns_append_remote_eps(fi, data);
1137 if (rc < 0) {
1138 cause = -rc;
1139 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1140 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1141 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001142 }
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001143 if (event == NS2_SNS_EV_RX_CONFIG_END) {
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001144 /* check if sum of data / sig weights == 0 */
Alexander Couzens019da4b2021-06-06 02:48:18 +02001145 if (ip46_weight_sum_data(&gss->remote) == 0 || ip46_weight_sum_sig(&gss->remote) == 0) {
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001146 cause = NS_CAUSE_INVAL_WEIGH;
1147 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1148 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1149 return;
1150 }
1151 create_missing_nsvcs(fi);
1152 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1153 /* start the test procedure on ALL NSVCs! */
1154 gprs_ns2_start_alive_all_nsvcs(nse);
1155 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1156 } else {
1157 /* just send CONFIG-ACK */
1158 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1159 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001160 }
1161 break;
1162 default:
1163 OSMO_ASSERT(0);
1164 }
1165}
1166
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001167/* called when receiving NS2_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001168static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1169 struct ns2_sns_state *gss,
1170 struct tlv_parsed *tp)
1171{
1172 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1173 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1174 int num_v4 = 0, num_v6 = 0;
1175 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001176 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001177 int rc = 0;
1178
1179 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1180 * check uniqueness within the lists (no doublicate entries)
1181 * check not-known-by-us and sent back a list of unknown/known values
1182 * (abnormal behaviour according to 48.016)
1183 */
1184
1185 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
Alexander Couzens077ce5a2021-06-06 01:32:45 +02001186 if (gss->family == AF_INET) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001187 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1188 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1189 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1190 return;
1191 }
1192
1193 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1194 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001195 for (i = 0; i < num_v4; i++) {
1196 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001197 rc = do_sns_add(fi, &v4_list[i], NULL);
1198 if (rc < 0) {
1199 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001200 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001201 do_sns_delete(fi, &v4_list[j], NULL);
1202 cause = -rc;
1203 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1204 break;
1205 }
1206 }
1207 } else { /* IPv6 */
1208 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1209 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1210 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1211 return;
1212 }
1213
1214 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1215 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001216 for (i = 0; i < num_v6; i++) {
1217 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001218 rc = do_sns_add(fi, NULL, &v6_list[i]);
1219 if (rc < 0) {
1220 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001221 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001222 do_sns_delete(fi, NULL, &v6_list[j]);
1223 cause = -rc;
1224 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1225 break;
1226 }
1227 }
1228 }
1229
1230 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1231 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1232}
1233
1234static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1235 struct ns2_sns_state *gss,
1236 struct tlv_parsed *tp)
1237{
1238 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1239 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1240 int num_v4 = 0, num_v6 = 0;
1241 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001242 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001243 int rc = 0;
1244
1245 /* TODO: split up delete into v4 + v6
1246 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1247 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1248 */
1249 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
Alexander Couzens077ce5a2021-06-06 01:32:45 +02001250 if (gss->family == AF_INET) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001251 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1252 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1253 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001254 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001255 rc = do_sns_delete(fi, &v4_list[i], NULL);
1256 if (rc < 0) {
1257 cause = -rc;
1258 /* continue to delete others */
1259 }
1260 }
1261 if (cause != 0xff) {
1262 /* TODO: create list of not-deleted and return it */
1263 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1264 return;
1265 }
1266
1267 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1268 /* delete all NS-VCs for given IPv4 address */
1269 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1270 struct gprs_ns_ie_ip4_elem *ip4_remote;
1271 uint32_t ip_addr = *(uint32_t *)(ie+1);
1272 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1273 cause = NS_CAUSE_UNKN_IP_ADDR;
1274 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1275 return;
1276 }
1277 /* make a copy as do_sns_delete() will change the array underneath us */
Alexander Couzens71128672021-06-05 18:44:01 +02001278 ip4_remote = talloc_memdup(fi, gss->remote.ip4,
1279 gss->remote.num_ip4 * sizeof(*v4_list));
1280 for (i = 0; i < gss->remote.num_ip4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001281 if (ip4_remote[i].ip_addr == ip_addr) {
1282 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1283 if (rc < 0) {
1284 cause = -rc;
1285 /* continue to delete others */
1286 }
1287 }
1288 }
1289 talloc_free(ip4_remote);
1290 if (cause != 0xff) {
1291 /* TODO: create list of not-deleted and return it */
1292 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1293 return;
1294 }
1295 } else {
1296 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1297 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1298 return;
1299 }
1300 } else { /* IPv6 */
1301 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1302 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1303 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001304 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001305 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1306 if (rc < 0) {
1307 cause = -rc;
1308 /* continue to delete others */
1309 }
1310 }
1311 if (cause != 0xff) {
1312 /* TODO: create list of not-deleted and return it */
1313 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1314 return;
1315 }
1316 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1317 /* delete all NS-VCs for given IPv4 address */
1318 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1319 struct gprs_ns_ie_ip6_elem *ip6_remote;
1320 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001321 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001322 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1323 cause = NS_CAUSE_UNKN_IP_ADDR;
1324 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1325 return;
1326 }
1327 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1328 /* make a copy as do_sns_delete() will change the array underneath us */
Alexander Couzens71128672021-06-05 18:44:01 +02001329 ip6_remote = talloc_memdup(fi, gss->remote.ip6,
1330 gss->remote.num_ip6 * sizeof(*v4_list));
1331 for (i = 0; i < gss->remote.num_ip6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001332 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1333 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1334 if (rc < 0) {
1335 cause = -rc;
1336 /* continue to delete others */
1337 }
1338 }
1339 }
1340
1341 talloc_free(ip6_remote);
1342 if (cause != 0xff) {
1343 /* TODO: create list of not-deleted and return it */
1344 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1345 return;
1346 }
1347 } else {
1348 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1349 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1350 return;
1351 }
1352 }
1353 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1354}
1355
1356static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1357 struct ns2_sns_state *gss,
1358 struct tlv_parsed *tp)
1359{
1360 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1361 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1362 int num_v4 = 0, num_v6 = 0;
1363 uint8_t trans_id, cause = 0xff;
1364 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001365 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001366
1367 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1368 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1369 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1370 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001371 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001372 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1373 if (rc < 0) {
1374 cause = -rc;
1375 /* continue to others */
1376 }
1377 }
1378 if (cause != 0xff) {
1379 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1380 return;
1381 }
1382 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1383 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1384 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001385 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001386 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1387 if (rc < 0) {
1388 cause = -rc;
1389 /* continue to others */
1390 }
1391 }
1392 if (cause != 0xff) {
1393 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1394 return;
1395 }
1396 } else {
1397 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1398 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1399 return;
1400 }
1401 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1402}
1403
1404static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1405{
1406 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1407 struct tlv_parsed *tp = data;
1408
1409 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001410 case NS2_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001411 ns2_sns_st_configured_add(fi, gss, tp);
1412 break;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001413 case NS2_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001414 ns2_sns_st_configured_delete(fi, gss, tp);
1415 break;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001416 case NS2_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001417 ns2_sns_st_configured_change(fi, gss, tp);
1418 break;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001419 case NS2_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001420 osmo_timer_del(&fi->timer);
1421 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001422 }
1423}
1424
1425static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1426{
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001427 struct gprs_ns2_vc *nsvc;
1428 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001429 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001430 /* NS-VC status updates are only parsed in ST_CONFIGURED.
1431 * Do an initial check if there are any nsvc alive atm */
1432 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1433 if (ns2_vc_is_unblocked(nsvc)) {
1434 gss->alive = true;
1435 osmo_timer_del(&fi->timer);
1436 break;
1437 }
1438 }
1439
Alexander Couzens53e70092021-04-06 15:45:47 +02001440 /* remove the initial NSVC if the NSVC isn't part of the configuration */
1441 if (gss->sns_nsvc->sns_only)
1442 gprs_ns2_free_nsvc(gss->sns_nsvc);
1443
Alexander Couzens1f3193d2021-06-05 22:08:11 +02001444 if (old_state != GPRS_SNS_ST_LOCAL_PROCEDURE)
1445 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
1446
1447 if (!llist_empty(&gss->procedures)) {
1448 osmo_fsm_inst_state_chg(gss->nse->bss_sns_fi, GPRS_SNS_ST_LOCAL_PROCEDURE,
1449 gss->nse->nsi->timeout[NS_TOUT_TSNS_PROV], 5);
1450 }
1451}
1452
1453static void ns2_sns_st_local_procedure_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1454{
1455 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1456
1457 /* check if resend or not */
1458 if (!gss->current_procedure) {
1459 /* take next procedure */
1460 gss->current_procedure = llist_first_entry_or_null(&gss->procedures,
1461 struct ns2_sns_procedure, list);
1462 if (!gss->current_procedure) {
1463 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1464 return;
1465 }
1466 gss->N = 0;
1467 gss->current_procedure->running = true;
1468 gss->current_procedure->trans_id = ++gss->trans_id;
1469 if (gss->trans_id == 0)
1470 gss->trans_id = gss->current_procedure->trans_id = 1;
1471
1472 }
1473
1474 /* also takes care of retransmitting */
1475 switch (gss->current_procedure->procedure) {
1476 case SNS_PROC_CHANGE_WEIGHT:
1477 if (gss->family == AF_INET)
1478 ns2_tx_sns_change_weight(gss->sns_nsvc, gss->current_procedure->trans_id, &gss->current_procedure->ip4, 1, NULL, 0);
1479 else
1480 ns2_tx_sns_change_weight(gss->sns_nsvc, gss->current_procedure->trans_id, NULL, 0, &gss->current_procedure->ip6, 1);
1481 break;
1482 default:
1483 break;
1484 }
1485}
1486
1487static void ns2_sns_st_local_procedure(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1488{
1489 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1490 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1491 struct gprs_ns_ie_ip4_elem *ip4, *proc4;
1492 struct gprs_ns_ie_ip6_elem *ip6, *proc6;
1493 struct tlv_parsed *tp = data;
1494 uint8_t trans_id;
1495 uint8_t cause;
1496
1497 switch (event) {
1498 case NS2_SNS_EV_RX_ADD:
1499 ns2_sns_st_configured_add(fi, gss, tp);
1500 break;
1501 case NS2_SNS_EV_RX_DELETE:
1502 ns2_sns_st_configured_delete(fi, gss, tp);
1503 break;
1504 case NS2_SNS_EV_RX_CHANGE_WEIGHT:
1505 ns2_sns_st_configured_change(fi, gss, tp);
1506 break;
1507 case NS2_SNS_EV_RX_ACK:
1508 /* presence of trans_id is already checked here */
1509 trans_id = tlvp_val8(tp, NS_IE_TRANS_ID, 0);
1510 if (trans_id != gss->current_procedure->trans_id) {
1511 LOGPFSML(fi, LOGL_INFO, "NSEI=%u Rx SNS ACK with invalid transaction id %d. Valid %d\n",
1512 nse->nsei, trans_id, gss->current_procedure->trans_id);
1513 break;
1514 }
1515
1516 if (TLVP_PRESENT(tp, NS_IE_CAUSE)) {
1517 /* what happend on error cause? return to size? */
1518 cause = tlvp_val8(tp, NS_IE_CAUSE, 0);
1519 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx SNS ACK trans %d with cause code %d.\n",
1520 nse->nsei, trans_id, cause);
1521 sns_failed(fi, NULL);
1522 break;
1523 }
1524
1525 switch (gss->current_procedure->procedure) {
1526 case SNS_PROC_CHANGE_WEIGHT:
1527 switch (gss->family) {
1528 case AF_INET:
1529 proc4 = &gss->current_procedure->ip4;
1530 for (unsigned int i=0; i<gss->local.num_ip4; i++) {
1531 ip4 = &gss->local.ip4[i];
1532 if (ip4->ip_addr != proc4->ip_addr ||
1533 ip4->udp_port != proc4->udp_port)
1534 continue;
1535 ip4->sig_weight = proc4->sig_weight;
1536 ip4->data_weight = proc4->data_weight;
1537 break;
1538 }
1539 break;
1540 case AF_INET6:
1541 proc6 = &gss->current_procedure->ip6;
1542 for (unsigned int i=0; i<gss->local.num_ip6; i++) {
1543 ip6 = &gss->local.ip6[i];
1544 if (memcmp(&ip6->ip_addr, &proc6->ip_addr, sizeof(proc6->ip_addr)) ||
1545 ip6->udp_port != proc6->udp_port) {
1546 continue;
1547 }
1548 ip6->sig_weight = proc6->sig_weight;
1549 ip6->data_weight = proc6->data_weight;
1550 break;
1551 }
1552 break;
1553 default:
1554 OSMO_ASSERT(0);
1555 }
1556 break;
1557 default:
1558 break;
1559 }
1560
1561 llist_del(&gss->current_procedure->list);
1562 talloc_free(gss->current_procedure);
1563 gss->current_procedure = NULL;
1564
1565 if (llist_empty(&gss->procedures))
1566 osmo_fsm_inst_state_chg(gss->nse->bss_sns_fi, GPRS_SNS_ST_CONFIGURED,
1567 0, 0);
1568 else
1569 osmo_fsm_inst_state_chg(gss->nse->bss_sns_fi, GPRS_SNS_ST_LOCAL_PROCEDURE,
1570 gss->nse->nsi->timeout[NS_TOUT_TSNS_PROV], 5);
1571 break;
1572 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001573}
1574
1575static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1576 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001577 .in_event_mask = 0, /* handled by all_state_action */
Alexander Couzens0a7c5ee2021-04-10 18:20:21 +02001578 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1579 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001580 .name = "UNCONFIGURED",
Harald Welte694dad52021-03-23 15:22:16 +01001581 .action = ns2_sns_st_bss_unconfigured,
Alexander Couzens6a161492020-07-12 13:45:50 +02001582 },
Harald Welte694dad52021-03-23 15:22:16 +01001583 [GPRS_SNS_ST_BSS_SIZE] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001584 .in_event_mask = S(NS2_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001585 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001586 S(GPRS_SNS_ST_BSS_SIZE) |
1587 S(GPRS_SNS_ST_BSS_CONFIG_BSS),
1588 .name = "BSS_SIZE",
1589 .action = ns2_sns_st_bss_size,
1590 .onenter = ns2_sns_st_bss_size_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001591 },
Harald Welte694dad52021-03-23 15:22:16 +01001592 [GPRS_SNS_ST_BSS_CONFIG_BSS] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001593 .in_event_mask = S(NS2_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001594 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001595 S(GPRS_SNS_ST_BSS_CONFIG_BSS) |
1596 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
1597 S(GPRS_SNS_ST_BSS_SIZE),
1598 .name = "BSS_CONFIG_BSS",
1599 .action = ns2_sns_st_bss_config_bss,
1600 .onenter = ns2_sns_st_bss_config_bss_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001601 },
Harald Welte694dad52021-03-23 15:22:16 +01001602 [GPRS_SNS_ST_BSS_CONFIG_SGSN] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001603 .in_event_mask = S(NS2_SNS_EV_RX_CONFIG) |
1604 S(NS2_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001605 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001606 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens6a161492020-07-12 13:45:50 +02001607 S(GPRS_SNS_ST_CONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001608 S(GPRS_SNS_ST_BSS_SIZE),
1609 .name = "BSS_CONFIG_SGSN",
1610 .action = ns2_sns_st_bss_config_sgsn,
1611 .onenter = ns2_sns_st_bss_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001612 },
1613 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001614 .in_event_mask = S(NS2_SNS_EV_RX_ADD) |
1615 S(NS2_SNS_EV_RX_DELETE) |
1616 S(NS2_SNS_EV_RX_CHANGE_WEIGHT) |
1617 S(NS2_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001618 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Alexander Couzens1f3193d2021-06-05 22:08:11 +02001619 S(GPRS_SNS_ST_BSS_SIZE) |
1620 S(GPRS_SNS_ST_LOCAL_PROCEDURE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001621 .name = "CONFIGURED",
1622 .action = ns2_sns_st_configured,
1623 .onenter = ns2_sns_st_configured_onenter,
1624 },
Alexander Couzens1f3193d2021-06-05 22:08:11 +02001625 [GPRS_SNS_ST_LOCAL_PROCEDURE] = {
1626 .in_event_mask = S(NS2_SNS_EV_RX_ADD) |
1627 S(NS2_SNS_EV_RX_DELETE) |
1628 S(NS2_SNS_EV_RX_CHANGE_WEIGHT) |
1629 S(NS2_SNS_EV_RX_ACK) |
1630 S(NS2_SNS_EV_REQ_NSVC_ALIVE),
1631 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1632 S(GPRS_SNS_ST_BSS_SIZE) |
1633 S(GPRS_SNS_ST_CONFIGURED) |
1634 S(GPRS_SNS_ST_LOCAL_PROCEDURE),
1635 .name = "LOCAL_PROCEDURE",
1636 .action = ns2_sns_st_local_procedure,
1637 .onenter = ns2_sns_st_local_procedure_onenter,
1638 },
1639
Alexander Couzens6a161492020-07-12 13:45:50 +02001640};
1641
1642static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1643{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001644 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001645 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1646 struct gprs_ns2_inst *nsi = nse->nsi;
1647
Alexander Couzens90ee9632020-12-07 06:18:32 +01001648 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001649 switch (fi->T) {
1650 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001651 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
Alexander Couzens652ab4d2021-06-12 23:09:46 +02001652 sns_failed(fi, "Size retries failed. Selecting next IP-SNS endpoint.");
Alexander Couzensa367d082020-12-21 14:06:24 +01001653 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001654 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 +01001655 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001656 break;
1657 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001658 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens652ab4d2021-06-12 23:09:46 +02001659 sns_failed(fi, "BSS Config retries failed. Selecting next IP-SNS endpoint");
Alexander Couzensa367d082020-12-21 14:06:24 +01001660 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001661 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 +01001662 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001663 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001664 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001665 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens652ab4d2021-06-12 23:09:46 +02001666 sns_failed(fi, "SGSN Config retries failed. Selecting next IP-SNS endpoint.");
Alexander Couzens3df58862021-02-05 17:18:08 +01001667 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001668 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 +01001669 }
1670 break;
1671 case 4:
Alexander Couzens652ab4d2021-06-12 23:09:46 +02001672 sns_failed(fi, "Config succeeded but no NS-VC came online. Selecting next IP-SNS endpoint.");
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001673 break;
Alexander Couzens1f3193d2021-06-05 22:08:11 +02001674 case 5:
1675 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1676 sns_failed(fi, "SNS Procedure retries failed.");
1677 } else {
1678 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_LOCAL_PROCEDURE, nsi->timeout[NS_TOUT_TSNS_PROV], 5);
1679 }
1680 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001681 }
1682 return 0;
1683}
1684
Alexander Couzens1f3193d2021-06-05 22:08:11 +02001685static struct gprs_ns_ie_ip4_elem *ns2_get_sbind_ip4_entry(struct ns2_sns_state *gss,
1686 struct ns2_sns_bind *sbind,
1687 struct ns2_sns_elems *endpoints)
1688{
1689 const struct osmo_sockaddr *addr;
1690 struct gprs_ns_ie_ip4_elem *ip4;
1691
1692 if (gss->family != AF_INET)
1693 return NULL;
1694
1695 addr = gprs_ns2_ip_bind_sockaddr(sbind->bind);
1696 if (addr->u.sa.sa_family != AF_INET)
1697 return NULL;
1698
1699 for (unsigned int i=0; i<endpoints->num_ip4; i++) {
1700 ip4 = &endpoints->ip4[i];
1701 if (ip4->ip_addr == addr->u.sin.sin_addr.s_addr &&
1702 ip4->udp_port == addr->u.sin.sin_port)
1703 return ip4;
1704 }
1705
1706 return NULL;
1707}
1708
1709static struct gprs_ns_ie_ip6_elem *ns2_get_sbind_ip6_entry(struct ns2_sns_state *gss,
1710 struct ns2_sns_bind *sbind,
1711 struct ns2_sns_elems *endpoints)
1712{
1713 const struct osmo_sockaddr *addr;
1714 struct gprs_ns_ie_ip6_elem *ip6;
1715
1716 if (gss->family != AF_INET6)
1717 return NULL;
1718
1719 addr = gprs_ns2_ip_bind_sockaddr(sbind->bind);
1720 if (addr->u.sa.sa_family != AF_INET6)
1721 return NULL;
1722
1723 for (unsigned int i=0; i<endpoints->num_ip6; i++) {
1724 ip6 = &endpoints->ip6[i];
1725 if (memcmp(&ip6->ip_addr, &addr->u.sin6.sin6_addr, sizeof(ip6->ip_addr)) ||
1726 ip6->udp_port != addr->u.sin6.sin6_port)
1727 return ip6;
1728 }
1729
1730 return NULL;
1731}
1732
1733/* return != 0 if the resulting weight is invalid. return 1 if sbind doesn't have an entry */
1734static int ns2_update_weight_entry(struct ns2_sns_state *gss, struct ns2_sns_bind *sbind,
1735 struct ns2_sns_elems *endpoints)
1736{
1737 struct gprs_ns_ie_ip4_elem *ip4;
1738 struct gprs_ns_ie_ip6_elem *ip6;
1739
1740 switch (gss->family) {
1741 case AF_INET:
1742 ip4 = ns2_get_sbind_ip4_entry(gss, sbind, endpoints);
1743 if (!ip4)
1744 return 1;
1745 ip4->sig_weight = sbind->bind->sns_sig_weight;
1746 ip4->data_weight = sbind->bind->sns_data_weight;
1747 return (ip4_weight_sum_sig(endpoints) != 0 && ip4_weight_sum_data(endpoints) != 0);
1748 break;
1749 case AF_INET6:
1750 ip6 = ns2_get_sbind_ip6_entry(gss, sbind, endpoints);
1751 if (!ip6)
1752 return 1;
1753 ip6->sig_weight = sbind->bind->sns_sig_weight;
1754 ip6->data_weight = sbind->bind->sns_data_weight;
1755 return (ip6_weight_sum_sig(endpoints) != 0 && ip6_weight_sum_data(endpoints) != 0);
1756 break;
1757 default:
1758 OSMO_ASSERT(0);
1759 }
1760}
1761
1762static void ns2_add_procedure(struct ns2_sns_state *gss, struct ns2_sns_bind *sbind,
1763 enum sns_procedure procedure_type)
1764{
1765 struct ns2_sns_procedure *procedure = NULL;
1766 const struct osmo_sockaddr *saddr;
1767 saddr = gprs_ns2_ip_bind_sockaddr(sbind->bind);
1768
1769 if (saddr->u.sa.sa_family != gss->family)
1770 return;
1771
1772 switch (procedure_type) {
1773 case SNS_PROC_CHANGE_WEIGHT:
1774 llist_for_each_entry(procedure, &gss->procedures, list) {
1775 if (procedure->sbind == sbind && procedure->procedure == procedure_type &&
1776 !procedure->running) {
1777 switch(gss->family) {
1778 case AF_INET:
1779 /* merge it with a previous procedure */
1780 procedure->ip4.ip_addr = sbind->bind->sns_sig_weight;
1781 procedure->ip4.data_weight = sbind->bind->sns_data_weight;
1782 break;
1783 case AF_INET6:
1784 /* merge it with a previous procedure */
1785 procedure->ip6.sig_weight = sbind->bind->sns_sig_weight;
1786 procedure->ip6.data_weight = sbind->bind->sns_data_weight;
1787 break;
1788 default:
1789 OSMO_ASSERT(0);
1790 }
1791 return;
1792 }
1793 }
1794
1795 procedure = talloc_zero(gss, struct ns2_sns_procedure);
1796 if (!procedure)
1797 return;
1798
1799 procedure->sbind = sbind;
1800 procedure->procedure = procedure_type;
1801 procedure->sig_weight = sbind->bind->sns_sig_weight;
1802 procedure->data_weight = sbind->bind->sns_data_weight;
1803
1804 switch(gss->family) {
1805 case AF_INET:
1806
1807 procedure->ip4.ip_addr = saddr->u.sin.sin_addr.s_addr;
1808 procedure->ip4.udp_port = saddr->u.sin.sin_port;
1809 procedure->ip4.sig_weight = sbind->bind->sns_sig_weight;
1810 procedure->ip4.data_weight = sbind->bind->sns_data_weight;
1811 break;
1812 case AF_INET6:
1813
1814 memcpy(&procedure->ip6.ip_addr, &saddr->u.sin6.sin6_addr, sizeof(struct in6_addr));
1815 procedure->ip6.udp_port = saddr->u.sin.sin_port;
1816 procedure->ip6.sig_weight = sbind->bind->sns_sig_weight;
1817 procedure->ip6.data_weight = sbind->bind->sns_data_weight;
1818 break;
1819 default:
1820 OSMO_ASSERT(0);
1821 }
1822
1823 llist_add_tail(&procedure->list, &gss->procedures);
1824 break;
1825 default:
1826 return;
1827 }
1828
1829 if (gss->nse->bss_sns_fi->state == GPRS_SNS_ST_CONFIGURED) {
1830 osmo_fsm_inst_state_chg(gss->nse->bss_sns_fi, GPRS_SNS_ST_LOCAL_PROCEDURE,
1831 gss->nse->nsi->timeout[NS_TOUT_TSNS_PROV], 5);
1832 }
1833}
1834
1835
Harald Welte9e37bf42021-03-02 20:48:31 +01001836/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001837static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1838{
1839 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens1f3193d2021-06-05 22:08:11 +02001840 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001841 struct ns2_sns_bind *sbind;
1842 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001843
Alexander Couzense769f522020-12-07 07:37:07 +01001844 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001845 case NS2_SNS_EV_REQ_ADD_BIND:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001846 sbind = data;
1847 switch (fi->state) {
1848 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001849 osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001850 break;
Harald Welte694dad52021-03-23 15:22:16 +01001851 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001852 /* TODO: add the ip4 element to the list */
1853 break;
Harald Welte694dad52021-03-23 15:22:16 +01001854 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1855 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001856 case GPRS_SNS_ST_CONFIGURED:
1857 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1858 break;
1859 }
1860 break;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001861 case NS2_SNS_EV_REQ_DELETE_BIND:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001862 sbind = data;
1863 switch (fi->state) {
1864 case GPRS_SNS_ST_UNCONFIGURED:
1865 break;
Harald Welte694dad52021-03-23 15:22:16 +01001866 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001867 /* TODO: remove the ip4 element from the list */
1868 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1869 if (nsvc->bind == sbind->bind) {
1870 gprs_ns2_free_nsvc(nsvc);
1871 }
1872 }
1873 break;
Harald Welte694dad52021-03-23 15:22:16 +01001874 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1875 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001876 case GPRS_SNS_ST_CONFIGURED:
1877 /* TODO: do an delete SNS-IP procedure */
1878 /* TODO: remove the ip4 element to the list */
1879 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1880 if (nsvc->bind == sbind->bind) {
1881 gprs_ns2_free_nsvc(nsvc);
1882 }
1883 }
1884 break;
1885 }
1886 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1887 talloc_free(sbind);
1888 break;
Alexander Couzens1f3193d2021-06-05 22:08:11 +02001889 case NS2_SNS_EV_REQ_CHANGE_WEIGHT:
1890 sbind = data;
1891 switch (fi->state) {
1892 case GPRS_SNS_ST_UNCONFIGURED:
1893 /* select_endpoint will check if this is a valid configuration */
1894 if (gss->role == GPRS_SNS_ROLE_BSS)
1895 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1896 break;
1897 case GPRS_SNS_ST_BSS_SIZE:
1898 /* invalid weight? */
1899 if (!ns2_update_weight_entry(gss, sbind, &gss->local))
1900 sns_failed(fi, "updating weights results in an invalid configuration.");
1901 break;
1902 default:
1903 if (!ns2_update_weight_entry(gss, sbind, &gss->local_procedure)) {
1904 sns_failed(fi, "updating weights results in an invalid configuration.");
1905 break;
1906 }
1907 ns2_add_procedure(gss, sbind, SNS_PROC_CHANGE_WEIGHT);
1908 break;
1909 }
Alexander Couzense769f522020-12-07 07:37:07 +01001910 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001911}
1912
Alexander Couzens31d52e12021-06-05 20:04:04 +02001913/* validate the bss configuration (sns endpoint and binds)
1914 * - no endpoints -> invalid
1915 * - no binds -> invalid
1916 * - only v4 sns endpoints, only v6 binds -> invalid
1917 * - only v4 sns endpoints, but v4 sig weights == 0 -> invalid ...
1918 */
1919static int ns2_sns_bss_valid_configuration(struct ns2_sns_state *gss)
1920{
1921 struct ns2_sns_bind *sbind;
1922 struct sns_endpoint *endpoint;
1923 const struct osmo_sockaddr *addr;
1924 int v4_sig = 0, v4_data = 0, v6_sig = 0, v6_data = 0;
1925 bool v4_endpoints = false;
1926 bool v6_endpoints = false;
1927
1928 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds))
1929 return 0;
1930
1931 llist_for_each_entry(sbind, &gss->binds, list) {
1932 addr = gprs_ns2_ip_bind_sockaddr(sbind->bind);
1933 if (!addr)
1934 continue;
1935 switch (addr->u.sa.sa_family) {
1936 case AF_INET:
1937 v4_sig += sbind->bind->sns_sig_weight;
1938 v4_data += sbind->bind->sns_data_weight;
1939 break;
1940 case AF_INET6:
1941 v6_sig += sbind->bind->sns_sig_weight;
1942 v6_data += sbind->bind->sns_data_weight;
1943 break;
1944 }
1945 }
1946
1947 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
1948 switch (endpoint->saddr.u.sa.sa_family) {
1949 case AF_INET:
1950 v4_endpoints = true;
1951 break;
1952 case AF_INET6:
1953 v6_endpoints = true;
1954 break;
1955 }
1956 }
1957
1958 return (v4_endpoints && v4_sig && v4_data) || (v6_endpoints && v6_sig && v6_data);
1959}
1960
Harald Welte9e37bf42021-03-02 20:48:31 +01001961/* allstate-action for BSS role */
1962static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1963{
1964 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1965 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1966
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001967 /* reset when receiving NS2_SNS_EV_REQ_NO_NSVC */
Harald Welte9e37bf42021-03-02 20:48:31 +01001968 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001969 case NS2_SNS_EV_REQ_NO_NSVC:
Harald Welte9e37bf42021-03-02 20:48:31 +01001970 /* ignore reselection running */
1971 if (gss->reselection_running)
1972 break;
1973
Alexander Couzens652ab4d2021-06-12 23:09:46 +02001974 sns_failed(fi, "no remaining NSVC, resetting SNS FSM");
Harald Welte9e37bf42021-03-02 20:48:31 +01001975 break;
Alexander Couzens83f06ce2021-08-06 19:50:09 +02001976 case NS2_SNS_EV_REQ_FREE_NSVCS:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001977 case NS2_SNS_EV_REQ_SELECT_ENDPOINT:
Harald Welte9e37bf42021-03-02 20:48:31 +01001978 /* tear down previous state
1979 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1980 gss->reselection_running = true;
Alexander Couzensf0746592021-07-20 19:05:45 +02001981 ns2_free_nsvcs(nse);
Alexander Couzensd2c6c492021-06-06 01:57:52 +02001982 ns2_clear_elems(&gss->local);
1983 ns2_clear_elems(&gss->remote);
Harald Welte9e37bf42021-03-02 20:48:31 +01001984
1985 /* Choose the next sns endpoint. */
Alexander Couzens31d52e12021-06-05 20:04:04 +02001986 if (!ns2_sns_bss_valid_configuration(gss)) {
Harald Welte9e37bf42021-03-02 20:48:31 +01001987 gss->initial = NULL;
1988 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1989 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1990 return;
1991 } else if (!gss->initial) {
1992 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1993 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1994 /* last entry, continue with first */
1995 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1996 } else {
1997 /* next element is an entry */
1998 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1999 }
2000
Alexander Couzens68ab9c42021-06-06 03:03:40 +02002001 gss->family = gss->initial->saddr.u.sa.sa_family;
Harald Welte9e37bf42021-03-02 20:48:31 +01002002 gss->reselection_running = false;
Harald Welte694dad52021-03-23 15:22:16 +01002003 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 +01002004 break;
2005 default:
2006 ns2_sns_st_all_action(fi, event, data);
2007 break;
2008 }
2009}
2010
Alexander Couzens6a161492020-07-12 13:45:50 +02002011static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
2012 .name = "GPRS-NS2-SNS-BSS",
2013 .states = ns2_sns_bss_states,
2014 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002015 .allstate_event_mask = S(NS2_SNS_EV_REQ_NO_NSVC) |
Alexander Couzens83f06ce2021-08-06 19:50:09 +02002016 S(NS2_SNS_EV_REQ_FREE_NSVCS) |
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002017 S(NS2_SNS_EV_REQ_SELECT_ENDPOINT) |
2018 S(NS2_SNS_EV_REQ_ADD_BIND) |
Alexander Couzens1f3193d2021-06-05 22:08:11 +02002019 S(NS2_SNS_EV_REQ_CHANGE_WEIGHT) |
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002020 S(NS2_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01002021 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02002022 .cleanup = NULL,
2023 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02002024 .event_names = gprs_sns_event_names,
2025 .pre_term = NULL,
2026 .log_subsys = DLNS,
2027};
2028
Harald Welte5bef2cc2020-09-18 22:33:24 +02002029/*! Allocate an IP-SNS FSM for the BSS side.
2030 * \param[in] nse NS Entity in which the FSM runs
2031 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01002032 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02002033struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
2034 const char *id)
2035{
2036 struct osmo_fsm_inst *fi;
2037 struct ns2_sns_state *gss;
2038
2039 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
2040 if (!fi)
2041 return fi;
2042
2043 gss = talloc_zero(fi, struct ns2_sns_state);
2044 if (!gss)
2045 goto err;
2046
2047 fi->priv = gss;
2048 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01002049 gss->role = GPRS_SNS_ROLE_BSS;
Harald Welte24f4df52021-03-04 18:02:54 +01002050 /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */
2051 gss->num_max_ip4_remote = 8192;
2052 gss->num_max_ip6_remote = 8192;
Alexander Couzense769f522020-12-07 07:37:07 +01002053 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002054 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens1f3193d2021-06-05 22:08:11 +02002055 INIT_LLIST_HEAD(&gss->procedures);
Alexander Couzens6a161492020-07-12 13:45:50 +02002056
2057 return fi;
2058err:
2059 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2060 return NULL;
2061}
2062
Harald Welte5bef2cc2020-09-18 22:33:24 +02002063/*! main entry point for receiving SNS messages from the network.
2064 * \param[in] nsvc NS-VC on which the message was received
2065 * \param[in] msg message buffer of the IP-SNS message
2066 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01002067 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01002068int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02002069{
2070 struct gprs_ns2_nse *nse = nsvc->nse;
2071 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
2072 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01002073 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02002074 struct osmo_fsm_inst *fi;
Alexander Couzens7619ed42021-03-24 17:44:03 +01002075 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02002076
2077 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01002078 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
2079 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01002080 rc = -EINVAL;
2081 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +02002082 }
2083
Alexander Couzens6a161492020-07-12 13:45:50 +02002084 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
2085 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01002086 gss = (struct ns2_sns_state *) fi->priv;
2087 if (!gss->sns_nsvc)
2088 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02002089
Harald Weltef2949742021-01-20 14:54:14 +01002090 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
2091 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
2092
Alexander Couzens6a161492020-07-12 13:45:50 +02002093 switch (nsh->pdu_type) {
2094 case SNS_PDUT_SIZE:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002095 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02002096 break;
2097 case SNS_PDUT_SIZE_ACK:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002098 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02002099 break;
2100 case SNS_PDUT_CONFIG:
2101 if (nsh->data[0] & 0x01)
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002102 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02002103 else
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002104 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02002105 break;
2106 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002107 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02002108 break;
2109 case SNS_PDUT_ADD:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002110 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02002111 break;
2112 case SNS_PDUT_DELETE:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002113 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02002114 break;
2115 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002116 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02002117 break;
2118 case SNS_PDUT_ACK:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002119 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02002120 break;
2121 default:
Harald Weltef2949742021-01-20 14:54:14 +01002122 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
2123 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01002124 rc = -EINVAL;
Alexander Couzens6a161492020-07-12 13:45:50 +02002125 }
2126
Alexander Couzens7619ed42021-03-24 17:44:03 +01002127out:
2128 msgb_free(msg);
2129
2130 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02002131}
2132
2133#include <osmocom/vty/vty.h>
2134#include <osmocom/vty/misc.h>
2135
Harald Welte1262c4f2021-01-19 20:58:33 +01002136static 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 +02002137{
2138 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01002139 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02002140 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
2141}
2142
Harald Welte1262c4f2021-01-19 20:58:33 +01002143static 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 +02002144{
2145 char ip_addr[INET6_ADDRSTRLEN] = {};
2146 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
2147 strcpy(ip_addr, "Invalid IPv6");
2148
Harald Welte1262c4f2021-01-19 20:58:33 +01002149 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02002150 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
2151}
2152
Harald Welte5bef2cc2020-09-18 22:33:24 +02002153/*! Dump the IP-SNS state to a vty.
2154 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01002155 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02002156 * \param[in] nse NS Entity whose IP-SNS state shall be printed
2157 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01002158void 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 +02002159{
2160 struct ns2_sns_state *gss;
2161 unsigned int i;
2162
2163 if (!nse->bss_sns_fi)
2164 return;
2165
Harald Welte1262c4f2021-01-19 20:58:33 +01002166 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02002167 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
2168
Harald Welte1262c4f2021-01-19 20:58:33 +01002169 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
2170 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02002171
Alexander Couzens71128672021-06-05 18:44:01 +02002172 if (gss->local.num_ip4 && gss->remote.num_ip4) {
Harald Welte1262c4f2021-01-19 20:58:33 +01002173 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02002174 for (i = 0; i < gss->local.num_ip4; i++)
2175 vty_dump_sns_ip4(vty, prefix, &gss->local.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02002176
Harald Welte1262c4f2021-01-19 20:58:33 +01002177 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02002178 for (i = 0; i < gss->remote.num_ip4; i++)
2179 vty_dump_sns_ip4(vty, prefix, &gss->remote.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02002180 }
2181
Alexander Couzens71128672021-06-05 18:44:01 +02002182 if (gss->local.num_ip6 && gss->remote.num_ip6) {
Harald Welte1262c4f2021-01-19 20:58:33 +01002183 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02002184 for (i = 0; i < gss->local.num_ip6; i++)
2185 vty_dump_sns_ip6(vty, prefix, &gss->local.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02002186
Harald Welte1262c4f2021-01-19 20:58:33 +01002187 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02002188 for (i = 0; i < gss->remote.num_ip6; i++)
2189 vty_dump_sns_ip6(vty, prefix, &gss->remote.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02002190 }
2191}
2192
Alexander Couzens412bc342020-11-19 05:24:37 +01002193/*! write IP-SNS to a vty
2194 * \param[in] vty VTY to which the state shall be printed
2195 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01002196void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01002197{
2198 struct ns2_sns_state *gss;
2199 struct osmo_sockaddr_str addr_str;
2200 struct sns_endpoint *endpoint;
2201
2202 if (!nse->bss_sns_fi)
2203 return;
2204
2205 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
2206 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01002207 /* It's unlikely that an error happens, but let's better be safe. */
2208 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
2209 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01002210 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01002211 }
2212}
2213
Alexander Couzense769f522020-12-07 07:37:07 +01002214static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
2215 const struct osmo_sockaddr *saddr)
2216{
2217 struct sns_endpoint *endpoint;
2218
2219 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
2220 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
2221 return endpoint;
2222 }
2223
2224 return NULL;
2225}
2226
2227/*! gprs_ns2_sns_add_endpoint
2228 * \param[in] nse
2229 * \param[in] sockaddr
2230 * \return
2231 */
2232int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
2233 const struct osmo_sockaddr *saddr)
2234{
2235 struct ns2_sns_state *gss;
2236 struct sns_endpoint *endpoint;
2237 bool do_selection = false;
2238
2239 if (nse->ll != GPRS_NS2_LL_UDP) {
2240 return -EINVAL;
2241 }
2242
Alexander Couzens138b96f2021-01-25 16:23:29 +01002243 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01002244 return -EINVAL;
2245 }
2246
2247 gss = nse->bss_sns_fi->priv;
2248
2249 if (ns2_get_sns_endpoint(gss, saddr))
2250 return -EADDRINUSE;
2251
2252 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
2253 if (!endpoint)
2254 return -ENOMEM;
2255
2256 endpoint->saddr = *saddr;
2257 if (llist_empty(&gss->sns_endpoints))
2258 do_selection = true;
2259
2260 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
2261 if (do_selection)
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002262 osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01002263
2264 return 0;
2265}
2266
2267/*! gprs_ns2_sns_del_endpoint
2268 * \param[in] nse
2269 * \param[in] sockaddr
2270 * \return 0 on success, otherwise < 0
2271 */
2272int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
2273 const struct osmo_sockaddr *saddr)
2274{
2275 struct ns2_sns_state *gss;
2276 struct sns_endpoint *endpoint;
2277
2278 if (nse->ll != GPRS_NS2_LL_UDP) {
2279 return -EINVAL;
2280 }
2281
Alexander Couzens138b96f2021-01-25 16:23:29 +01002282 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01002283 return -EINVAL;
2284 }
2285
2286 gss = nse->bss_sns_fi->priv;
2287 endpoint = ns2_get_sns_endpoint(gss, saddr);
2288 if (!endpoint)
2289 return -ENOENT;
2290
2291 /* if this is an unused SNS endpoint it's done */
2292 if (gss->initial != endpoint) {
2293 llist_del(&endpoint->list);
2294 talloc_free(endpoint);
2295 return 0;
2296 }
2297
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002298 /* gprs_ns2_free_nsvcs() will trigger NS2_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01002299 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01002300 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01002301 "Closing all NS-VC and restart SNS-SIZE procedure"
2302 "with a remaining SNS endpoint.\n");
2303
2304 /* Continue with the next endpoint in the list.
2305 * Special case if the endpoint is at the start or end of the list */
2306 if (endpoint->list.prev == &gss->sns_endpoints ||
2307 endpoint->list.next == &gss->sns_endpoints)
2308 gss->initial = NULL;
2309 else
2310 gss->initial = llist_entry(endpoint->list.next->prev,
2311 struct sns_endpoint,
2312 list);
2313
2314 llist_del(&endpoint->list);
2315 gprs_ns2_free_nsvcs(nse);
2316 talloc_free(endpoint);
2317
2318 return 0;
2319}
2320
2321/*! gprs_ns2_sns_count
2322 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
2323 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
2324 */
2325int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
2326{
2327 struct ns2_sns_state *gss;
2328 struct sns_endpoint *endpoint;
2329 int count = 0;
2330
2331 if (nse->ll != GPRS_NS2_LL_UDP) {
2332 return -EINVAL;
2333 }
2334
Alexander Couzens138b96f2021-01-25 16:23:29 +01002335 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01002336 return -EINVAL;
2337 }
2338
2339 gss = nse->bss_sns_fi->priv;
2340 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
2341 count++;
2342
2343 return count;
2344}
2345
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01002346void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
2347{
2348 struct ns2_sns_state *gss;
2349 struct gprs_ns2_vc *tmp;
2350
2351 if (!nse->bss_sns_fi)
2352 return;
2353
2354 gss = nse->bss_sns_fi->priv;
Alexander Couzens1f3193d2021-06-05 22:08:11 +02002355 if (nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED && nse->bss_sns_fi->state != GPRS_SNS_ST_LOCAL_PROCEDURE)
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01002356 return;
2357
2358 if (alive == gss->alive)
2359 return;
2360
2361 /* check if this is the current SNS NS-VC */
2362 if (nsvc == gss->sns_nsvc) {
2363 /* only replace the SNS NS-VC if there are other alive NS-VC.
2364 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
2365 * and couldn't confirm yet if the NS-VC comes up */
2366 if (gss->alive && !alive)
2367 ns2_sns_replace_nsvc(nsvc);
2368 }
2369
2370 if (alive) {
2371 gss->alive = true;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002372 osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01002373 } else {
2374 /* is there at least another alive nsvc? */
2375 llist_for_each_entry(tmp, &nse->nsvc, list) {
2376 if (ns2_vc_is_unblocked(tmp))
2377 return;
2378 }
2379
2380 /* all NS-VC have failed */
2381 gss->alive = false;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002382 osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01002383 }
2384}
2385
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002386int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
2387 struct gprs_ns2_vc_bind *bind)
2388{
2389 struct ns2_sns_state *gss;
2390 struct ns2_sns_bind *tmp;
2391
2392 OSMO_ASSERT(nse->bss_sns_fi);
2393 gss = nse->bss_sns_fi->priv;
2394
2395 if (!gprs_ns2_is_ip_bind(bind)) {
2396 return -EINVAL;
2397 }
2398
2399 if (!llist_empty(&gss->binds)) {
2400 llist_for_each_entry(tmp, &gss->binds, list) {
2401 if (tmp->bind == bind)
2402 return -EALREADY;
2403 }
2404 }
2405
2406 tmp = talloc_zero(gss, struct ns2_sns_bind);
2407 if (!tmp)
2408 return -ENOMEM;
2409 tmp->bind = bind;
2410 llist_add_tail(&tmp->list, &gss->binds);
2411
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002412 osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_ADD_BIND, tmp);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002413 return 0;
2414}
2415
2416/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
2417int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
2418 struct gprs_ns2_vc_bind *bind)
2419{
2420 struct ns2_sns_state *gss;
2421 struct ns2_sns_bind *tmp, *tmp2;
2422 bool found = false;
2423
2424 if (!nse->bss_sns_fi)
2425 return -EINVAL;
2426
2427 gss = nse->bss_sns_fi->priv;
2428 if (gss->initial_bind && gss->initial_bind->bind == bind) {
2429 if (gss->initial_bind->list.prev == &gss->binds)
2430 gss->initial_bind = NULL;
2431 else
2432 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
2433 }
2434
2435 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
2436 if (tmp->bind == bind) {
2437 llist_del(&tmp->list);
2438 found = true;
Alexander Couzensa35c2962021-04-19 03:30:15 +02002439 break;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002440 }
2441 }
2442
2443 if (!found)
2444 return -ENOENT;
2445
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002446 osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_DELETE_BIND, tmp);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002447 return 0;
2448}
2449
Alexander Couzens71128672021-06-05 18:44:01 +02002450/* Update SNS weights for a bind (local endpoint).
2451 * \param[in] bind the bind which has been updated
Alexander Couzensc4704762021-02-08 23:13:12 +01002452 */
2453void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
2454{
Alexander Couzens1f3193d2021-06-05 22:08:11 +02002455 struct ns2_sns_bind *sbind;
2456 struct gprs_ns2_nse *nse;
2457 struct ns2_sns_state *gss;
2458 const struct osmo_sockaddr *addr = gprs_ns2_ip_bind_sockaddr(bind);
2459
2460 llist_for_each_entry(nse, &bind->nsi->nse, list) {
2461 if (!nse->bss_sns_fi)
2462 continue;
2463
2464 gss = nse->bss_sns_fi->priv;
2465 if (addr->u.sa.sa_family != gss->family)
2466 return;
2467
2468 llist_for_each_entry(sbind, &gss->binds, list) {
2469 if (sbind->bind == bind) {
2470 osmo_fsm_inst_dispatch(gss->nse->bss_sns_fi, NS2_SNS_EV_REQ_CHANGE_WEIGHT, sbind);
2471 break;
2472 }
2473 }
2474 }
Alexander Couzensc4704762021-02-08 23:13:12 +01002475}
2476
Harald Welte4f127462021-03-02 20:49:10 +01002477
2478
2479
2480/***********************************************************************
2481 * SGSN role
2482 ***********************************************************************/
2483
Alexander Couzensa2707822021-07-20 18:59:40 +02002484/* cleanup all state. If nsvc is given, don't remove this nsvc. (nsvc is given when a SIZE PDU received) */
2485static void ns2_clear_sgsn(struct ns2_sns_state *gss, struct gprs_ns2_vc *size_nsvc)
2486{
2487 struct gprs_ns2_vc *nsvc, *nsvc2;
2488
Alexander Couzens1f3193d2021-06-05 22:08:11 +02002489 ns2_clear_procedures(gss);
Alexander Couzensa2707822021-07-20 18:59:40 +02002490 ns2_clear_elems(&gss->local);
2491 ns2_clear_elems(&gss->remote);
2492 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2493 /* Ignore the NSVC over which the SIZE PDU got received */
2494 if (size_nsvc && size_nsvc == nsvc)
2495 continue;
2496
2497 gprs_ns2_free_nsvc(nsvc);
2498 }
2499}
2500
2501static void ns2_sns_st_sgsn_unconfigured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2502{
2503 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2504
2505 ns2_clear_sgsn(gss, NULL);
2506}
2507
Harald Welte4f127462021-03-02 20:49:10 +01002508static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2509{
2510 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2511 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2512 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
2513}
2514
2515/* We're waiting for inbound SNS-CONFIG from the BSS */
2516static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2517{
2518 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2519 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2520 struct gprs_ns2_inst *nsi = nse->nsi;
2521 uint8_t cause;
2522 int rc;
2523
2524 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2525
2526 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002527 case NS2_SNS_EV_RX_CONFIG:
2528 case NS2_SNS_EV_RX_CONFIG_END:
Harald Welte4f127462021-03-02 20:49:10 +01002529 rc = ns_sns_append_remote_eps(fi, data);
2530 if (rc < 0) {
2531 cause = -rc;
2532 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2533 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2534 return;
2535 }
2536 /* only change state if last CONFIG was received */
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002537 if (event == NS2_SNS_EV_RX_CONFIG_END) {
Harald Welte4f127462021-03-02 20:49:10 +01002538 /* ensure sum of data weight / sig weights is > 0 */
Alexander Couzens019da4b2021-06-06 02:48:18 +02002539 if (ip46_weight_sum_data(&gss->remote) == 0 || ip46_weight_sum_sig(&gss->remote) == 0) {
Harald Welte4f127462021-03-02 20:49:10 +01002540 cause = NS_CAUSE_INVAL_WEIGH;
2541 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2542 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2543 break;
2544 }
2545 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2546 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2547 } else {
2548 /* just send CONFIG-ACK */
2549 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2550 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2551 }
2552 break;
2553 }
2554}
2555
2556static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2557{
2558 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2559 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2560
Harald Welte4f127462021-03-02 20:49:10 +01002561 /* transmit SGSN-oriented SNS-CONFIG */
Alexander Couzens71128672021-06-05 18:44:01 +02002562 ns2_tx_sns_config(gss->sns_nsvc, true, gss->local.ip4, gss->local.num_ip4,
2563 gss->local.ip6, gss->local.num_ip6);
Harald Welte4f127462021-03-02 20:49:10 +01002564}
2565
2566/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2567static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2568{
2569 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2570 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2571 struct tlv_parsed *tp = NULL;
2572
2573 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2574
2575 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002576 case NS2_SNS_EV_RX_CONFIG_ACK:
Harald Welte4f127462021-03-02 20:49:10 +01002577 tp = data;
2578 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2579 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2580 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2581 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2582 break;
2583 }
2584 /* we currently only send one SNS-CONFIG with END FLAG */
2585 if (true) {
2586 create_missing_nsvcs(fi);
2587 /* start the test procedure on ALL NSVCs! */
2588 gprs_ns2_start_alive_all_nsvcs(nse);
2589 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2590 }
2591 break;
2592 }
2593}
2594
2595/* SGSN-side SNS state machine */
2596static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2597 [GPRS_SNS_ST_UNCONFIGURED] = {
2598 .in_event_mask = 0, /* handled by all_state_action */
2599 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2600 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2601 .name = "UNCONFIGURED",
2602 .action = ns2_sns_st_sgsn_unconfigured,
Alexander Couzensa2707822021-07-20 18:59:40 +02002603 .onenter = ns2_sns_st_sgsn_unconfigured_onenter,
Harald Welte4f127462021-03-02 20:49:10 +01002604 },
2605 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002606 .in_event_mask = S(NS2_SNS_EV_RX_CONFIG) |
2607 S(NS2_SNS_EV_RX_CONFIG_END),
Harald Welte4f127462021-03-02 20:49:10 +01002608 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2609 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2610 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2611 .name = "SGSN_WAIT_CONFIG",
2612 .action = ns2_sns_st_sgsn_wait_config,
2613 },
2614 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002615 .in_event_mask = S(NS2_SNS_EV_RX_CONFIG_ACK),
Harald Welte4f127462021-03-02 20:49:10 +01002616 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Alexander Couzensa2707822021-07-20 18:59:40 +02002617 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
Harald Welte4f127462021-03-02 20:49:10 +01002618 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2619 S(GPRS_SNS_ST_CONFIGURED),
2620 .name = "SGSN_WAIT_CONFIG_ACK",
2621 .action = ns2_sns_st_sgsn_wait_config_ack,
2622 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2623 },
2624 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002625 .in_event_mask = S(NS2_SNS_EV_RX_ADD) |
2626 S(NS2_SNS_EV_RX_DELETE) |
2627 S(NS2_SNS_EV_RX_CHANGE_WEIGHT) |
2628 S(NS2_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzensa2707822021-07-20 18:59:40 +02002629 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Alexander Couzens1f3193d2021-06-05 22:08:11 +02002630 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2631 S(GPRS_SNS_ST_LOCAL_PROCEDURE),
Harald Welte4f127462021-03-02 20:49:10 +01002632 .name = "CONFIGURED",
2633 /* shared with BSS side; once configured there's no difference */
2634 .action = ns2_sns_st_configured,
2635 .onenter = ns2_sns_st_configured_onenter,
2636 },
Alexander Couzens1f3193d2021-06-05 22:08:11 +02002637 [GPRS_SNS_ST_LOCAL_PROCEDURE] = {
2638 .in_event_mask = S(NS2_SNS_EV_RX_ADD) |
2639 S(NS2_SNS_EV_RX_DELETE) |
2640 S(NS2_SNS_EV_RX_CHANGE_WEIGHT) |
2641 S(NS2_SNS_EV_RX_ACK) |
2642 S(NS2_SNS_EV_REQ_CHANGE_WEIGHT) |
2643 S(NS2_SNS_EV_REQ_NSVC_ALIVE),
2644 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2645 S(GPRS_SNS_ST_CONFIGURED) |
2646 S(GPRS_SNS_ST_LOCAL_PROCEDURE),
2647 .name = "LOCAL_PROCEDURE",
2648 /* shared with BSS side; once configured there's no difference */
2649 .action = ns2_sns_st_local_procedure,
2650 .onenter = ns2_sns_st_local_procedure_onenter,
2651 },
Harald Welte4f127462021-03-02 20:49:10 +01002652};
2653
2654static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2655{
2656 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2657 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2658 struct gprs_ns2_inst *nsi = nse->nsi;
2659
2660 gss->N++;
2661 switch (fi->T) {
2662 case 3:
2663 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2664 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2665 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2666 } else {
2667 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2668 }
2669 break;
2670 case 4:
2671 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2672 break;
Alexander Couzens1f3193d2021-06-05 22:08:11 +02002673 case 5:
2674 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_PROCEDURES_RETRIES]) {
2675 sns_failed(fi, "SNS Procedure retries failed.");
2676 } else {
2677 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_LOCAL_PROCEDURE, nsi->timeout[NS_TOUT_TSNS_PROV],
2678 fi->T);
2679 }
2680 break;
Harald Welte4f127462021-03-02 20:49:10 +01002681 }
Alexander Couzens1f3193d2021-06-05 22:08:11 +02002682
Harald Welte4f127462021-03-02 20:49:10 +01002683 return 0;
2684}
2685
Harald Welte4f127462021-03-02 20:49:10 +01002686/* allstate-action for SGSN role */
2687static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2688{
2689 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2690 struct tlv_parsed *tp = NULL;
Harald Welte01fa6a32021-03-04 19:49:38 +01002691 size_t num_local_eps, num_remote_eps;
Harald Welte4f127462021-03-02 20:49:10 +01002692 uint8_t flag;
Harald Weltea2c5af52021-03-04 17:59:35 +01002693 uint8_t cause;
Harald Welte4f127462021-03-02 20:49:10 +01002694
2695 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2696
2697 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002698 case NS2_SNS_EV_RX_SIZE:
Harald Welte4f127462021-03-02 20:49:10 +01002699 tp = (struct tlv_parsed *) data;
Harald Weltea2c5af52021-03-04 17:59:35 +01002700 /* check for mandatory / conditional IEs */
2701 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) ||
2702 !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) {
2703 cause = NS_CAUSE_MISSING_ESSENT_IE;
2704 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
Alexander Couzens1c405252021-06-13 00:14:48 +02002705 if (fi->state == GPRS_SNS_ST_UNCONFIGURED)
2706 sns_failed(fi, "Rx Size: Missing essential IE");
Harald Weltea2c5af52021-03-04 17:59:35 +01002707 break;
2708 }
2709 if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) &&
2710 !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) {
2711 cause = NS_CAUSE_MISSING_ESSENT_IE;
Harald Welte4f127462021-03-02 20:49:10 +01002712 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
Alexander Couzens1c405252021-06-13 00:14:48 +02002713 if (fi->state == GPRS_SNS_ST_UNCONFIGURED)
2714 sns_failed(fi, "Rx Size: Missing essential IE");
Harald Welte4f127462021-03-02 20:49:10 +01002715 break;
2716 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002717 if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2))
2718 gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR);
2719 if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2))
2720 gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR);
2721 /* decide if we go for IPv4 or IPv6 */
Alexander Couzens077ce5a2021-06-06 01:32:45 +02002722 if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, AF_INET6)) {
2723 gss->family = AF_INET6;
Harald Welte2d807b62021-03-24 01:57:30 +01002724 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002725 num_local_eps = gss->local.num_ip6;
Harald Welte01fa6a32021-03-04 19:49:38 +01002726 num_remote_eps = gss->num_max_ip6_remote;
Alexander Couzens077ce5a2021-06-06 01:32:45 +02002727 } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, AF_INET)) {
2728 gss->family = AF_INET;
Harald Welte2d807b62021-03-24 01:57:30 +01002729 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002730 num_local_eps = gss->local.num_ip4;
Harald Welte01fa6a32021-03-04 19:49:38 +01002731 num_remote_eps = gss->num_max_ip4_remote;
2732 } else {
Alexander Couzens71128672021-06-05 18:44:01 +02002733 if (gss->local.num_ip4 && !gss->num_max_ip4_remote)
Harald Welte01fa6a32021-03-04 19:49:38 +01002734 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
2735 else
2736 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
2737 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
Alexander Couzens1c405252021-06-13 00:14:48 +02002738 if (fi->state == GPRS_SNS_ST_UNCONFIGURED)
2739 sns_failed(fi, "Rx Size: Invalid Nr of IPv4/IPv6 EPs");
Harald Welte01fa6a32021-03-04 19:49:38 +01002740 break;
2741 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002742 /* ensure number of NS-VCs is sufficient for full mesh */
2743 gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC);
2744 if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) {
2745 LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, "
2746 "but BSS supports only %zu maximum NS-VCs\n", num_local_eps,
2747 num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs);
2748 cause = NS_CAUSE_INVAL_NR_NS_VC;
2749 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
Alexander Couzens1c405252021-06-13 00:14:48 +02002750 if (fi->state == GPRS_SNS_ST_UNCONFIGURED)
2751 sns_failed(fi, NULL);
Harald Welte01fa6a32021-03-04 19:49:38 +01002752 break;
2753 }
2754 /* perform state reset, if requested */
Harald Welte4f127462021-03-02 20:49:10 +01002755 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2756 if (flag & 1) {
Harald Welte4f127462021-03-02 20:49:10 +01002757 /* clear all state */
Alexander Couzensa2707822021-07-20 18:59:40 +02002758 /* TODO: ensure gss->sns_nsvc is always the NSVC on which we received the SIZE PDU */
Harald Welte4f127462021-03-02 20:49:10 +01002759 gss->N = 0;
Alexander Couzensa2707822021-07-20 18:59:40 +02002760 ns2_clear_sgsn(gss, gss->sns_nsvc);
2761 /* keep the NSVC we need for SNS, but unconfigure it */
2762 gss->sns_nsvc->sig_weight = 0;
2763 gss->sns_nsvc->data_weight = 0;
2764 ns2_vc_force_unconfigured(gss->sns_nsvc);
Harald Welte2d807b62021-03-24 01:57:30 +01002765 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte4f127462021-03-02 20:49:10 +01002766 }
Alexander Couzens1c405252021-06-13 00:14:48 +02002767
2768 if (fi->state == GPRS_SNS_ST_UNCONFIGURED && !(flag & 1)) {
2769 sns_failed(fi, "Rx Size without Reset flag, but NSE is unknown");
2770 break;
2771 }
2772
Harald Welte4f127462021-03-02 20:49:10 +01002773 /* send SIZE_ACK */
2774 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2775 /* only wait for SNS-CONFIG in case of Reset flag */
2776 if (flag & 1)
2777 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2778 break;
Alexander Couzens83f06ce2021-08-06 19:50:09 +02002779 case NS2_SNS_EV_REQ_FREE_NSVCS:
2780 sns_failed(fi, "On user request to free all NSVCs");
2781 break;
Harald Welte4f127462021-03-02 20:49:10 +01002782 default:
2783 ns2_sns_st_all_action(fi, event, data);
2784 break;
2785 }
2786}
2787
2788static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2789 .name = "GPRS-NS2-SNS-SGSN",
2790 .states = ns2_sns_sgsn_states,
2791 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002792 .allstate_event_mask = S(NS2_SNS_EV_RX_SIZE) |
2793 S(NS2_SNS_EV_REQ_NO_NSVC) |
Alexander Couzens83f06ce2021-08-06 19:50:09 +02002794 S(NS2_SNS_EV_REQ_FREE_NSVCS) |
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002795 S(NS2_SNS_EV_REQ_ADD_BIND) |
Alexander Couzens1f3193d2021-06-05 22:08:11 +02002796 S(NS2_SNS_EV_REQ_CHANGE_WEIGHT) |
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002797 S(NS2_SNS_EV_REQ_DELETE_BIND),
Harald Welte4f127462021-03-02 20:49:10 +01002798 .allstate_action = ns2_sns_st_all_action_sgsn,
2799 .cleanup = NULL,
2800 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2801 .event_names = gprs_sns_event_names,
2802 .pre_term = NULL,
2803 .log_subsys = DLNS,
2804};
2805
2806/*! Allocate an IP-SNS FSM for the SGSN side.
2807 * \param[in] nse NS Entity in which the FSM runs
2808 * \param[in] id string identifier
2809 * \returns FSM instance on success; NULL on error */
2810struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2811{
2812 struct osmo_fsm_inst *fi;
2813 struct ns2_sns_state *gss;
2814
2815 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2816 if (!fi)
2817 return fi;
2818
2819 gss = talloc_zero(fi, struct ns2_sns_state);
2820 if (!gss)
2821 goto err;
2822
2823 fi->priv = gss;
2824 gss->nse = nse;
2825 gss->role = GPRS_SNS_ROLE_SGSN;
2826 INIT_LLIST_HEAD(&gss->sns_endpoints);
2827 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens1f3193d2021-06-05 22:08:11 +02002828 INIT_LLIST_HEAD(&gss->procedures);
Harald Welte4f127462021-03-02 20:49:10 +01002829
2830 return fi;
2831err:
2832 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2833 return NULL;
2834}
2835
2836
2837
2838
Alexander Couzens6a161492020-07-12 13:45:50 +02002839/* initialize osmo_ctx on main tread */
2840static __attribute__((constructor)) void on_dso_load_ctx(void)
2841{
2842 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002843 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002844}