blob: 181d1a3ee6e28b6488c7f10e6f9e9f544e34bd8d [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 Couzens6a161492020-07-12 13:45:50 +020073};
74
Alexander Couzens6a161492020-07-12 13:45:50 +020075static const struct value_string gprs_sns_event_names[] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +020076 { NS2_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" },
77 { NS2_SNS_EV_RX_SIZE, "RX_SIZE" },
78 { NS2_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" },
79 { NS2_SNS_EV_RX_CONFIG, "RX_CONFIG" },
80 { NS2_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" },
81 { NS2_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" },
82 { NS2_SNS_EV_RX_ADD, "RX_ADD" },
83 { NS2_SNS_EV_RX_DELETE, "RX_DELETE" },
84 { NS2_SNS_EV_RX_ACK, "RX_ACK" },
85 { NS2_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" },
86 { NS2_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" },
87 { NS2_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"},
88 { NS2_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"},
89 { NS2_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"},
Alexander Couzens6a161492020-07-12 13:45:50 +020090 { 0, NULL }
91};
92
Alexander Couzense769f522020-12-07 07:37:07 +010093struct sns_endpoint {
94 struct llist_head list;
95 struct osmo_sockaddr saddr;
96};
97
Alexander Couzens6b9d2322021-02-12 03:17:59 +010098struct ns2_sns_bind {
99 struct llist_head list;
100 struct gprs_ns2_vc_bind *bind;
101};
102
Alexander Couzens71128672021-06-05 18:44:01 +0200103struct ns2_sns_elems {
104 struct gprs_ns_ie_ip4_elem *ip4;
105 unsigned int num_ip4;
106 struct gprs_ns_ie_ip6_elem *ip6;
107 unsigned int num_ip6;
108};
109
Alexander Couzens6a161492020-07-12 13:45:50 +0200110struct ns2_sns_state {
111 struct gprs_ns2_nse *nse;
112
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200113 /* containing the address family AF_* */
114 int family;
Harald Welte4f127462021-03-02 20:49:10 +0100115 enum ns2_sns_role role; /* local role: BSS or SGSN */
Alexander Couzens6a161492020-07-12 13:45:50 +0200116
Alexander Couzense769f522020-12-07 07:37:07 +0100117 /* holds the list of initial SNS endpoints */
118 struct llist_head sns_endpoints;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100119 /* list of used struct ns2_sns_bind */
120 struct llist_head binds;
121 /* pointer to the bind which was used to initiate the SNS connection */
122 struct ns2_sns_bind *initial_bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100123 /* prevent recursive reselection */
124 bool reselection_running;
125
126 /* The current initial SNS endpoints.
127 * The initial connection will be moved into the NSE
128 * if configured via SNS. Otherwise it will be removed
129 * in configured state. */
130 struct sns_endpoint *initial;
Alexander Couzens6a161492020-07-12 13:45:50 +0200131 /* all SNS PDU will be sent over this nsvc */
132 struct gprs_ns2_vc *sns_nsvc;
Alexander Couzens90ee9632020-12-07 06:18:32 +0100133 /* timer N */
134 int N;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100135 /* true if at least one nsvc is alive */
136 bool alive;
Alexander Couzens6a161492020-07-12 13:45:50 +0200137
138 /* local configuration to send to the remote end */
Alexander Couzens71128672021-06-05 18:44:01 +0200139 struct ns2_sns_elems local;
Alexander Couzens6a161492020-07-12 13:45:50 +0200140
Alexander Couzens71128672021-06-05 18:44:01 +0200141 /* remote configuration as received */
142 struct ns2_sns_elems remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200143
144 /* local configuration about our capabilities in terms of connections to
145 * remote (SGSN) side */
146 size_t num_max_nsvcs;
147 size_t num_max_ip4_remote;
148 size_t num_max_ip6_remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200149};
150
151static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi)
152{
153 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
154 return gss->nse;
155}
156
Alexander Couzens652ab4d2021-06-12 23:09:46 +0200157/* The SNS has failed. Etither restart the SNS (BSS) or remove the SNS (SGSN) */
158#define sns_failed(fi, reason) \
159 _sns_failed(fi, reason, __FILE__, __LINE__)
160static void _sns_failed(struct osmo_fsm_inst *fi, const char *reason, const char *file, int line)
161{
162 struct ns2_sns_state *gss = fi->priv;
163
164 if (reason)
165 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SNS failed: %s\n", gss->nse->nsei, reason);
166
167 if (gss->role == GPRS_SNS_ROLE_SGSN) {
168 if (!gss->nse->persistent)
169 gprs_ns2_free_nse(gss->nse);
170 else
171 _osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0, file, line);
172 } else {
Alexander Couzens175eb7b2021-07-20 18:41:14 +0200173 _osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, NULL, file, line);
Alexander Couzens652ab4d2021-06-12 23:09:46 +0200174 }
175}
176
Alexander Couzens6a161492020-07-12 13:45:50 +0200177/* helper function to compute the sum of all (data or signaling) weights */
Alexander Couzens62310552021-06-06 02:43:14 +0200178static int ip4_weight_sum(const struct ns2_sns_elems *elems, bool data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200179{
180 unsigned int i;
181 int weight_sum = 0;
182
Alexander Couzens62310552021-06-06 02:43:14 +0200183 for (i = 0; i < elems->num_ip4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200184 if (data_weight)
Alexander Couzens62310552021-06-06 02:43:14 +0200185 weight_sum += elems->ip4[i].data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200186 else
Alexander Couzens62310552021-06-06 02:43:14 +0200187 weight_sum += elems->ip4[i].sig_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200188 }
189 return weight_sum;
190}
Alexander Couzens62310552021-06-06 02:43:14 +0200191#define ip4_weight_sum_data(elems) ip4_weight_sum(elems, true)
192#define ip4_weight_sum_sig(elems) ip4_weight_sum(elems, false)
Alexander Couzens6a161492020-07-12 13:45:50 +0200193
194/* helper function to compute the sum of all (data or signaling) weights */
Alexander Couzens62310552021-06-06 02:43:14 +0200195static int ip6_weight_sum(const struct ns2_sns_elems *elems, bool data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200196{
197 unsigned int i;
198 int weight_sum = 0;
199
Alexander Couzens62310552021-06-06 02:43:14 +0200200 for (i = 0; i < elems->num_ip6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200201 if (data_weight)
Alexander Couzens62310552021-06-06 02:43:14 +0200202 weight_sum += elems->ip6[i].data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200203 else
Alexander Couzens62310552021-06-06 02:43:14 +0200204 weight_sum += elems->ip6[i].sig_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200205 }
206 return weight_sum;
207}
Alexander Couzens62310552021-06-06 02:43:14 +0200208#define ip6_weight_sum_data(elems) ip6_weight_sum(elems, true)
209#define ip6_weight_sum_sig(elems) ip6_weight_sum(elems, false)
Alexander Couzens6a161492020-07-12 13:45:50 +0200210
Alexander Couzens019da4b2021-06-06 02:48:18 +0200211static int ip46_weight_sum(const struct ns2_sns_elems *elems, bool data_weight)
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100212{
Alexander Couzens019da4b2021-06-06 02:48:18 +0200213 return ip4_weight_sum(elems, data_weight) +
214 ip6_weight_sum(elems, data_weight);
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100215}
Alexander Couzens019da4b2021-06-06 02:48:18 +0200216#define ip46_weight_sum_data(elems) ip46_weight_sum(elems, true)
217#define ip46_weight_sum_sig(elems) ip46_weight_sum(elems, false)
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100218
Alexander Couzens6a161492020-07-12 13:45:50 +0200219static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
220 const struct gprs_ns_ie_ip4_elem *ip4)
221{
222 struct osmo_sockaddr sa;
223 /* copy over. Both data structures use network byte order */
224 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
225 sa.u.sin.sin_port = ip4->udp_port;
226 sa.u.sin.sin_family = AF_INET;
227
Alexander Couzens38b19e82020-09-23 23:56:37 +0200228 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200229}
230
231static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
232 const struct gprs_ns_ie_ip6_elem *ip6)
233{
234 struct osmo_sockaddr sa;
235 /* copy over. Both data structures use network byte order */
236 sa.u.sin6.sin6_addr = ip6->ip_addr;
237 sa.u.sin6.sin6_port = ip6->udp_port;
238 sa.u.sin6.sin6_family = AF_INET;
239
Alexander Couzens38b19e82020-09-23 23:56:37 +0200240 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200241}
242
Alexander Couzens125298f2020-10-11 21:22:42 +0200243/*! Return the initial SNS remote socket address
244 * \param nse NS Entity
245 * \return address of the initial SNS connection; NULL in case of error
246 */
247const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
248{
249 struct ns2_sns_state *gss;
250
251 if (!nse->bss_sns_fi)
252 return NULL;
253
254 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100255 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200256}
257
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100258/*! called when a nsvc is beeing freed or the nsvc became dead */
259void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200260{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100261 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200262 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100263 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200264 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200265
266 if (!fi)
267 return;
268
269 gss = (struct ns2_sns_state *) fi->priv;
270 if (nsvc != gss->sns_nsvc)
271 return;
272
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100273 gss->sns_nsvc = NULL;
274 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200275 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100276 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200277 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100278 return;
279 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200280 }
281 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100282 /* the SNS is waiting for its first NS-VC to come up
283 * choose any other nsvc */
284 llist_for_each_entry(tmp, &nse->nsvc, list) {
285 if (nsvc != tmp) {
286 gss->sns_nsvc = tmp;
287 return;
288 }
289 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200290 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100291
Alexander Couzens175eb7b2021-07-20 18:41:14 +0200292 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200293}
294
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200295static void ns2_clear_elems(struct ns2_sns_elems *elems)
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100296{
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200297 TALLOC_FREE(elems->ip4);
298 TALLOC_FREE(elems->ip6);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100299
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200300 elems->num_ip4 = 0;
301 elems->num_ip6 = 0;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100302}
303
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100304static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
305 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200306{
307 struct gprs_ns2_inst *nsi = nse->nsi;
308 struct gprs_ns2_vc *nsvc;
309 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100310
311 /* for every bind, create a connection if bind type == IP */
312 llist_for_each_entry(bind, &nsi->binding, list) {
313 if (bind->ll != GPRS_NS2_LL_UDP)
314 continue;
315 /* ignore failed connection */
316 nsvc = gprs_ns2_ip_connect_inactive(bind,
317 remote,
318 nse, 0);
319 if (!nsvc) {
320 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
321 continue;
322 }
323
324 nsvc->sig_weight = sig_weight;
325 nsvc->data_weight = data_weight;
326 }
327}
328
329static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
330 struct gprs_ns2_nse *nse,
331 const struct gprs_ns_ie_ip4_elem *ip4)
332{
Alexander Couzensc068d862020-10-12 04:11:51 +0200333 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200334 /* copy over. Both data structures use network byte order */
335 remote.u.sin.sin_family = AF_INET;
336 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
337 remote.u.sin.sin_port = ip4->udp_port;
338
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100339 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200340}
341
342static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
343 struct gprs_ns2_nse *nse,
344 const struct gprs_ns_ie_ip6_elem *ip6)
345{
Alexander Couzens6a161492020-07-12 13:45:50 +0200346 struct osmo_sockaddr remote = {};
347 /* copy over. Both data structures use network byte order */
348 remote.u.sin6.sin6_family = AF_INET6;
349 remote.u.sin6.sin6_addr = ip6->ip_addr;
350 remote.u.sin6.sin6_port = ip6->udp_port;
351
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100352 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200353}
354
Harald Weltee8c61062021-03-24 13:16:27 +0100355static struct gprs_ns2_vc *nsvc_for_bind_and_remote(struct gprs_ns2_nse *nse,
356 struct gprs_ns2_vc_bind *bind,
357 const struct osmo_sockaddr *remote)
358{
359 struct gprs_ns2_vc *nsvc;
360
361 llist_for_each_entry(nsvc, &nse->nsvc, list) {
362 if (nsvc->bind != bind)
363 continue;
364
365 if (!osmo_sockaddr_cmp(remote, gprs_ns2_ip_vc_remote(nsvc)))
366 return nsvc;
367 }
368 return NULL;
369}
Alexander Couzens6a161492020-07-12 13:45:50 +0200370
371static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
372{
373 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
374 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
375 struct gprs_ns2_vc *nsvc;
Harald Welte3053bbb2021-03-24 13:22:18 +0100376 struct ns2_sns_bind *sbind;
Alexander Couzens6a161492020-07-12 13:45:50 +0200377 struct osmo_sockaddr remote = { };
378 unsigned int i;
379
Harald Weltee8c61062021-03-24 13:16:27 +0100380 /* iterate over all remote IPv4 endpoints */
Alexander Couzens71128672021-06-05 18:44:01 +0200381 for (i = 0; i < gss->remote.num_ip4; i++) {
382 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->remote.ip4[i];
Alexander Couzens6a161492020-07-12 13:45:50 +0200383
384 remote.u.sin.sin_family = AF_INET;
385 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
386 remote.u.sin.sin_port = ip4->udp_port;
387
Harald Welte3053bbb2021-03-24 13:22:18 +0100388 /* iterate over all local binds within this SNS */
389 llist_for_each_entry(sbind, &gss->binds, list) {
390 struct gprs_ns2_vc_bind *bind = sbind->bind;
391
Harald Weltee8c61062021-03-24 13:16:27 +0100392 /* we only care about UDP binds */
Daniel Willmann967e2c12021-01-14 16:58:17 +0100393 if (bind->ll != GPRS_NS2_LL_UDP)
394 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200395
Harald Weltee8c61062021-03-24 13:16:27 +0100396 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
397 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200398 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
399 if (!nsvc) {
400 /* TODO: add to a list to send back a NS-STATUS */
401 continue;
402 }
403 }
404
405 /* update data / signalling weight */
406 nsvc->data_weight = ip4->data_weight;
407 nsvc->sig_weight = ip4->sig_weight;
408 nsvc->sns_only = false;
409 }
410 }
411
Harald Weltee8c61062021-03-24 13:16:27 +0100412 /* iterate over all remote IPv4 endpoints */
Alexander Couzens71128672021-06-05 18:44:01 +0200413 for (i = 0; i < gss->remote.num_ip6; i++) {
414 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->remote.ip6[i];
Alexander Couzens6a161492020-07-12 13:45:50 +0200415
416 remote.u.sin6.sin6_family = AF_INET6;
417 remote.u.sin6.sin6_addr = ip6->ip_addr;
418 remote.u.sin6.sin6_port = ip6->udp_port;
419
Harald Welte3053bbb2021-03-24 13:22:18 +0100420 /* iterate over all local binds within this SNS */
421 llist_for_each_entry(sbind, &gss->binds, list) {
422 struct gprs_ns2_vc_bind *bind = sbind->bind;
423
Daniel Willmann967e2c12021-01-14 16:58:17 +0100424 if (bind->ll != GPRS_NS2_LL_UDP)
425 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200426
Harald Weltee8c61062021-03-24 13:16:27 +0100427 /* we only care about UDP binds */
428 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
429 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200430 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
431 if (!nsvc) {
432 /* TODO: add to a list to send back a NS-STATUS */
433 continue;
434 }
435 }
436
437 /* update data / signalling weight */
438 nsvc->data_weight = ip6->data_weight;
439 nsvc->sig_weight = ip6->sig_weight;
440 nsvc->sns_only = false;
441 }
442 }
443
444
445 return 0;
446}
447
448/* Add a given remote IPv4 element to gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200449static int add_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
450 const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +0200451{
Alexander Couzens6a161492020-07-12 13:45:50 +0200452 /* check for duplicates */
Alexander Couzenscc56ddc2021-06-06 03:33:35 +0200453 for (unsigned int i = 0; i < elems->num_ip4; i++) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200454 if (memcmp(&elems->ip4[i], ip4, sizeof(*ip4)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200455 continue;
Alexander Couzensd3507e82021-06-06 03:32:32 +0200456 return -1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200457 }
458
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200459 elems->ip4 = talloc_realloc(gss, elems->ip4, struct gprs_ns_ie_ip4_elem,
460 elems->num_ip4+1);
461 elems->ip4[elems->num_ip4] = *ip4;
462 elems->num_ip4 += 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200463 return 0;
464}
465
466/* Remove a given remote IPv4 element from gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200467static int remove_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
468 const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +0200469{
470 unsigned int i;
471
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200472 for (i = 0; i < elems->num_ip4; i++) {
473 if (memcmp(&elems->ip4[i], ip4, sizeof(*ip4)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200474 continue;
475 /* all array elements < i remain as they are; all > i are shifted left by one */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200476 memmove(&elems->ip4[i], &elems->ip4[i+1], elems->num_ip4-i-1);
477 elems->num_ip4 -= 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200478 return 0;
479 }
480 return -1;
481}
482
483/* update the weights for specified remote IPv4 */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200484static int update_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
485 const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +0200486{
487 unsigned int i;
488
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200489 for (i = 0; i < elems->num_ip4; i++) {
490 if (elems->ip4[i].ip_addr != ip4->ip_addr ||
491 elems->ip4[i].udp_port != ip4->udp_port)
Alexander Couzens6a161492020-07-12 13:45:50 +0200492 continue;
493
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200494 elems->ip4[i].sig_weight = ip4->sig_weight;
495 elems->ip4[i].data_weight = ip4->data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200496 return 0;
497 }
498 return -1;
499}
500
501/* Add a given remote IPv6 element to gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200502static int add_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
503 const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200504{
Alexander Couzenscc56ddc2021-06-06 03:33:35 +0200505 /* check for duplicates */
506 for (unsigned int i = 0; i < elems->num_ip6; i++) {
507 if (memcmp(&elems->ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
508 elems->ip6[i].udp_port != ip6->udp_port)
509 continue;
510 return -1;
511 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200512
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200513 elems->ip6 = talloc_realloc(gss, elems->ip6, struct gprs_ns_ie_ip6_elem,
514 elems->num_ip6+1);
515 elems->ip6[elems->num_ip6] = *ip6;
516 elems->num_ip6 += 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200517 return 0;
518}
519
520/* Remove a given remote IPv6 element from gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200521static int remove_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
522 const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200523{
524 unsigned int i;
525
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200526 for (i = 0; i < elems->num_ip6; i++) {
527 if (memcmp(&elems->ip6[i], ip6, sizeof(*ip6)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200528 continue;
529 /* all array elements < i remain as they are; all > i are shifted left by one */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200530 memmove(&elems->ip6[i], &elems->ip6[i+1], elems->num_ip6-i-1);
531 elems->num_ip6 -= 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200532 return 0;
533 }
534 return -1;
535}
536
537/* update the weights for specified remote IPv6 */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200538static int update_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
539 const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200540{
541 unsigned int i;
542
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200543 for (i = 0; i < elems->num_ip6; i++) {
544 if (memcmp(&elems->ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
545 elems->ip6[i].udp_port != ip6->udp_port)
Alexander Couzens6a161492020-07-12 13:45:50 +0200546 continue;
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200547 elems->ip6[i].sig_weight = ip6->sig_weight;
548 elems->ip6[i].data_weight = ip6->data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200549 return 0;
550 }
551 return -1;
552}
553
554static int do_sns_change_weight(struct osmo_fsm_inst *fi, const struct gprs_ns_ie_ip4_elem *ip4, const struct gprs_ns_ie_ip6_elem *ip6)
555{
556 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
557 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
558 struct gprs_ns2_vc *nsvc;
559 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200560 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200561 uint8_t new_signal;
562 uint8_t new_data;
563
564 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
565 * signalling weights of all the peer IP endpoints configured for this NSE is
566 * equal to zero or if the resulting sum of the data weights of all the peer IP
567 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
568 * SNS-ACK PDU with a cause code of "Invalid weights". */
569
570 if (ip4) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200571 if (update_ip4_elem(gss, &gss->remote, ip4))
Alexander Couzens6a161492020-07-12 13:45:50 +0200572 return -NS_CAUSE_UNKN_IP_EP;
573
574 /* copy over. Both data structures use network byte order */
575 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
576 sa.u.sin.sin_port = ip4->udp_port;
577 sa.u.sin.sin_family = AF_INET;
578 new_signal = ip4->sig_weight;
579 new_data = ip4->data_weight;
580 } else if (ip6) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200581 if (update_ip6_elem(gss, &gss->remote, ip6))
Alexander Couzens6a161492020-07-12 13:45:50 +0200582 return -NS_CAUSE_UNKN_IP_EP;
583
584 /* copy over. Both data structures use network byte order */
585 sa.u.sin6.sin6_addr = ip6->ip_addr;
586 sa.u.sin6.sin6_port = ip6->udp_port;
587 sa.u.sin6.sin6_family = AF_INET6;
588 new_signal = ip6->sig_weight;
589 new_data = ip6->data_weight;
590 } else {
591 OSMO_ASSERT(false);
592 }
593
594 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200595 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200596 /* all nsvc in NSE should be IP/UDP nsvc */
597 OSMO_ASSERT(remote);
598
599 if (osmo_sockaddr_cmp(&sa, remote))
600 continue;
601
602 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
603 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
604 nsvc->sig_weight, new_signal);
605
606 nsvc->data_weight = new_data;
607 nsvc->sig_weight = new_signal;
608 }
609
610 return 0;
611}
612
613static int do_sns_delete(struct osmo_fsm_inst *fi,
614 const struct gprs_ns_ie_ip4_elem *ip4,
615 const struct gprs_ns_ie_ip6_elem *ip6)
616{
617 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
618 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
619 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200620 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200621 struct osmo_sockaddr sa = {};
622
623 if (ip4) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200624 if (remove_ip4_elem(gss, &gss->remote, ip4) < 0)
Alexander Couzens6a161492020-07-12 13:45:50 +0200625 return -NS_CAUSE_UNKN_IP_EP;
626 /* copy over. Both data structures use network byte order */
627 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
628 sa.u.sin.sin_port = ip4->udp_port;
629 sa.u.sin.sin_family = AF_INET;
630 } else if (ip6) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200631 if (remove_ip6_elem(gss, &gss->remote, ip6))
Alexander Couzens6a161492020-07-12 13:45:50 +0200632 return -NS_CAUSE_UNKN_IP_EP;
633
634 /* copy over. Both data structures use network byte order */
635 sa.u.sin6.sin6_addr = ip6->ip_addr;
636 sa.u.sin6.sin6_port = ip6->udp_port;
637 sa.u.sin6.sin6_family = AF_INET6;
638 } else {
639 OSMO_ASSERT(false);
640 }
641
642 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200643 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200644 /* all nsvc in NSE should be IP/UDP nsvc */
645 OSMO_ASSERT(remote);
646 if (osmo_sockaddr_cmp(&sa, remote))
647 continue;
648
649 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
650 gprs_ns2_free_nsvc(nsvc);
651 }
652
653 return 0;
654}
655
656static int do_sns_add(struct osmo_fsm_inst *fi,
657 const struct gprs_ns_ie_ip4_elem *ip4,
658 const struct gprs_ns_ie_ip6_elem *ip6)
659{
660 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
661 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
662 struct gprs_ns2_vc *nsvc;
663 int rc = 0;
664
665 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
666 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
667 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200668 switch (gss->family) {
669 case AF_INET:
Alexander Couzensd3507e82021-06-06 03:32:32 +0200670 if (gss->remote.num_ip4 >= gss->num_max_ip4_remote)
671 return -NS_CAUSE_INVAL_NR_NS_VC;
672 /* TODO: log message duplicate */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200673 rc = add_ip4_elem(gss, &gss->remote, ip4);
Alexander Couzens6a161492020-07-12 13:45:50 +0200674 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200675 case AF_INET6:
Alexander Couzensd3507e82021-06-06 03:32:32 +0200676 if (gss->remote.num_ip6 >= gss->num_max_ip6_remote)
677 return -NS_CAUSE_INVAL_NR_NS_VC;
678 /* TODO: log message duplicate */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200679 rc = add_ip6_elem(gss, &gss->remote, ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200680 break;
681 default:
682 /* the gss->ip is initialized with the bss */
683 OSMO_ASSERT(false);
684 }
685
686 if (rc)
Alexander Couzensd3507e82021-06-06 03:32:32 +0200687 return -NS_CAUSE_PROTO_ERR_UNSPEC;
Alexander Couzens6a161492020-07-12 13:45:50 +0200688
689 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
690 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
691 * unspecified" */
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200692 switch (gss->family) {
693 case AF_INET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200694 nsvc = nsvc_by_ip4_elem(nse, ip4);
695 if (nsvc) {
696 /* the nsvc should be already in sync with the ip4 / ip6 elements */
697 return -NS_CAUSE_PROTO_ERR_UNSPEC;
698 }
699
700 /* TODO: failure case */
701 ns2_nsvc_create_ip4(fi, nse, ip4);
702 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200703 case AF_INET6:
Alexander Couzens6a161492020-07-12 13:45:50 +0200704 nsvc = nsvc_by_ip6_elem(nse, ip6);
705 if (nsvc) {
706 /* the nsvc should be already in sync with the ip4 / ip6 elements */
707 return -NS_CAUSE_PROTO_ERR_UNSPEC;
708 }
709
710 /* TODO: failure case */
711 ns2_nsvc_create_ip6(fi, nse, ip6);
712 break;
713 }
714
715 gprs_ns2_start_alive_all_nsvcs(nse);
716
717 return 0;
718}
719
720
Harald Welte694dad52021-03-23 15:22:16 +0100721static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200722{
Harald Weltef61a9152021-03-02 22:20:17 +0100723 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
724 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzense769f522020-12-07 07:37:07 +0100725 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200726}
727
Harald Welte694dad52021-03-23 15:22:16 +0100728static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200729{
Harald Weltef61a9152021-03-02 22:20:17 +0100730 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200731 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
732 struct gprs_ns2_inst *nsi = nse->nsi;
733 struct tlv_parsed *tp = NULL;
734
Harald Weltef61a9152021-03-02 22:20:17 +0100735 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
736
Alexander Couzens6a161492020-07-12 13:45:50 +0200737 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +0200738 case NS2_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200739 tp = data;
740 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
741 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
742 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
743 /* TODO: What to do? */
744 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100745 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200746 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
747 }
748 break;
749 default:
750 OSMO_ASSERT(0);
751 }
752}
753
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200754static int ns2_sns_count_num_local_ep(struct osmo_fsm_inst *fi, int ip_proto)
Harald Welte01fa6a32021-03-04 19:49:38 +0100755{
756 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
757 struct ns2_sns_bind *sbind;
758 int count = 0;
759
760 llist_for_each_entry(sbind, &gss->binds, list) {
761 const struct osmo_sockaddr *sa = gprs_ns2_ip_bind_sockaddr(sbind->bind);
762 if (!sa)
763 continue;
764
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200765 switch (ip_proto) {
766 case AF_INET:
Harald Welte01fa6a32021-03-04 19:49:38 +0100767 if (sa->u.sas.ss_family == AF_INET)
768 count++;
769 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200770 case AF_INET6:
Harald Welte01fa6a32021-03-04 19:49:38 +0100771 if (sa->u.sas.ss_family == AF_INET6)
772 count++;
773 break;
774 }
775 }
776 return count;
777}
778
Harald Welte24920e22021-03-04 13:03:27 +0100779static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200780{
781 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100782 struct gprs_ns_ie_ip4_elem *ip4_elems;
783 struct gprs_ns_ie_ip6_elem *ip6_elems;
784 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100785 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100786 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100787 const struct osmo_sockaddr *sa;
788 struct osmo_sockaddr local;
789 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200790
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200791 ns2_clear_elems(&gss->local);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100792
Alexander Couzense769f522020-12-07 07:37:07 +0100793 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100794 if (gss->role == GPRS_SNS_ROLE_BSS) {
795 if (!gss->initial)
796 return;
797 remote = &gss->initial->saddr;
798 } else
799 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100800
801 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100802 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100803 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100804 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100805 return;
806 }
807
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200808 switch (gss->family) {
809 case AF_INET:
Alexander Couzens71128672021-06-05 18:44:01 +0200810 ip4_elems = talloc_realloc(fi, gss->local.ip4, struct gprs_ns_ie_ip4_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100811 if (!ip4_elems)
812 return;
813
Alexander Couzens71128672021-06-05 18:44:01 +0200814 gss->local.ip4 = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100815 llist_for_each_entry(sbind, &gss->binds, list) {
816 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100817 sa = gprs_ns2_ip_bind_sockaddr(bind);
818 if (!sa)
819 continue;
820
821 if (sa->u.sas.ss_family != AF_INET)
822 continue;
823
824 /* check if this is an specific bind */
825 if (sa->u.sin.sin_addr.s_addr == 0) {
826 if (osmo_sockaddr_local_ip(&local, remote))
827 continue;
828
829 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
830 } else {
831 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
832 }
833
834 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100835 ip4_elems->sig_weight = bind->sns_sig_weight;
836 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100837 ip4_elems++;
838 }
839
Alexander Couzens71128672021-06-05 18:44:01 +0200840 gss->local.num_ip4 = count;
841 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->local.num_ip4, 8);
Alexander Couzense769f522020-12-07 07:37:07 +0100842 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200843 case AF_INET6:
Alexander Couzense769f522020-12-07 07:37:07 +0100844 /* IPv6 */
Alexander Couzens71128672021-06-05 18:44:01 +0200845 ip6_elems = talloc_realloc(fi, gss->local.ip6, struct gprs_ns_ie_ip6_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100846 if (!ip6_elems)
847 return;
848
Alexander Couzens71128672021-06-05 18:44:01 +0200849 gss->local.ip6 = ip6_elems;
Alexander Couzense769f522020-12-07 07:37:07 +0100850
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100851 llist_for_each_entry(sbind, &gss->binds, list) {
852 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100853 sa = gprs_ns2_ip_bind_sockaddr(bind);
854 if (!sa)
855 continue;
856
857 if (sa->u.sas.ss_family != AF_INET6)
858 continue;
859
860 /* check if this is an specific bind */
861 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
862 if (osmo_sockaddr_local_ip(&local, remote))
863 continue;
864
865 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
866 } else {
867 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
868 }
869
870 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100871 ip6_elems->sig_weight = bind->sns_sig_weight;
872 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100873
874 ip6_elems++;
875 }
Alexander Couzens71128672021-06-05 18:44:01 +0200876 gss->local.num_ip6 = count;
877 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->local.num_ip6, 8);
Alexander Couzense769f522020-12-07 07:37:07 +0100878 break;
879 }
Harald Welte24920e22021-03-04 13:03:27 +0100880}
881
Alexander Couzens6608ce92021-04-26 20:39:46 +0200882static void ns2_sns_choose_next_bind(struct ns2_sns_state *gss)
883{
884 /* take the first bind or take the next bind */
885 if (!gss->initial_bind || gss->initial_bind->list.next == &gss->binds)
886 gss->initial_bind = llist_first_entry_or_null(&gss->binds, struct ns2_sns_bind, list);
887 else
888 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
889}
890
Harald Welte24920e22021-03-04 13:03:27 +0100891/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Harald Welte694dad52021-03-23 15:22:16 +0100892static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Harald Welte24920e22021-03-04 13:03:27 +0100893{
894 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
895
Harald Weltef61a9152021-03-02 22:20:17 +0100896 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
897
Harald Welte24920e22021-03-04 13:03:27 +0100898 /* on a generic failure, the timer callback will recover */
899 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
900 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Harald Welte694dad52021-03-23 15:22:16 +0100901 if (old_state != GPRS_SNS_ST_BSS_SIZE)
Harald Welte24920e22021-03-04 13:03:27 +0100902 gss->N = 0;
903
904 gss->alive = false;
905
906 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens6608ce92021-04-26 20:39:46 +0200907 ns2_sns_choose_next_bind(gss);
Harald Welte24920e22021-03-04 13:03:27 +0100908
909 /* setup the NSVC */
910 if (!gss->sns_nsvc) {
911 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
912 struct osmo_sockaddr *remote = &gss->initial->saddr;
913 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
914 if (!gss->sns_nsvc)
915 return;
Harald Weltec962a2e2021-03-05 08:09:08 +0100916 /* A pre-configured endpoint shall not be used for NSE data or signalling traffic
917 * (with the exception of Size and Configuration procedures) unless it is configured
918 * by the SGSN using the auto-configuration procedures */
Harald Welte24920e22021-03-04 13:03:27 +0100919 gss->sns_nsvc->sns_only = true;
920 }
921
Alexander Couzens6a161492020-07-12 13:45:50 +0200922 if (gss->num_max_ip4_remote > 0)
Alexander Couzens71128672021-06-05 18:44:01 +0200923 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->local.num_ip4, -1);
Alexander Couzens6a161492020-07-12 13:45:50 +0200924 else
Alexander Couzens71128672021-06-05 18:44:01 +0200925 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->local.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200926}
927
Harald Welte694dad52021-03-23 15:22:16 +0100928static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200929{
Harald Weltef61a9152021-03-02 22:20:17 +0100930 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens3df58862021-02-05 17:18:08 +0100931 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Harald Weltef61a9152021-03-02 22:20:17 +0100932 struct tlv_parsed *tp = NULL;
933
934 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzens6a161492020-07-12 13:45:50 +0200935
936 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +0200937 case NS2_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200938 tp = (struct tlv_parsed *) data;
939 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
940 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
941 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
942 /* TODO: What to do? */
943 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100944 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 +0200945 }
946 break;
947 default:
948 OSMO_ASSERT(0);
949 }
950}
951
Harald Welte694dad52021-03-23 15:22:16 +0100952static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200953{
954 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100955
Harald Weltef61a9152021-03-02 22:20:17 +0100956 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
957
Harald Welte694dad52021-03-23 15:22:16 +0100958 if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS)
Alexander Couzens790a9632021-02-05 17:18:39 +0100959 gss->N = 0;
960
Alexander Couzens6a161492020-07-12 13:45:50 +0200961 /* Transmit SNS-CONFIG */
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200962 switch (gss->family) {
963 case AF_INET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200964 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzens71128672021-06-05 18:44:01 +0200965 gss->local.ip4, gss->local.num_ip4,
Alexander Couzense78207f2020-12-07 06:19:29 +0100966 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200967 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200968 case AF_INET6:
Alexander Couzens6a161492020-07-12 13:45:50 +0200969 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100970 NULL, 0,
Alexander Couzens71128672021-06-05 18:44:01 +0200971 gss->local.ip6, gss->local.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200972 break;
973 }
974}
975
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100976/* calculate the timeout of the configured state. the configured
977 * state will fail if not at least one NS-VC is alive within X second.
978 */
979static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
980{
981 int secs;
982 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
983 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
984 secs += nsi->timeout[NS_TOUT_TNS_TEST];
985
986 return secs;
987}
Alexander Couzens6a161492020-07-12 13:45:50 +0200988
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100989/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
990static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200991{
992 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200993
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100994 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
995 const struct gprs_ns_ie_ip4_elem *v4_list;
996 unsigned int num_v4;
997 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
998 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +0200999
Alexander Couzens71128672021-06-05 18:44:01 +02001000 if (num_v4 && gss->remote.ip6)
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001001 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +02001002
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001003 /* realloc to the new size */
Alexander Couzens71128672021-06-05 18:44:01 +02001004 gss->remote.ip4 = talloc_realloc(gss, gss->remote.ip4,
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001005 struct gprs_ns_ie_ip4_elem,
Alexander Couzens71128672021-06-05 18:44:01 +02001006 gss->remote.num_ip4 + num_v4);
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001007 /* append the new entries to the end of the list */
Alexander Couzens71128672021-06-05 18:44:01 +02001008 memcpy(&gss->remote.ip4[gss->remote.num_ip4], v4_list, num_v4*sizeof(*v4_list));
1009 gss->remote.num_ip4 += num_v4;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001010
1011 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
Alexander Couzens71128672021-06-05 18:44:01 +02001012 gss->remote.num_ip4);
Alexander Couzens6a161492020-07-12 13:45:50 +02001013 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001014
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001015 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1016 const struct gprs_ns_ie_ip6_elem *v6_list;
1017 unsigned int num_v6;
1018 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1019 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
1020
Alexander Couzens71128672021-06-05 18:44:01 +02001021 if (num_v6 && gss->remote.ip4)
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001022 return -NS_CAUSE_INVAL_NR_IPv6_EP;
1023
1024 /* realloc to the new size */
Alexander Couzens71128672021-06-05 18:44:01 +02001025 gss->remote.ip6 = talloc_realloc(gss, gss->remote.ip6,
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001026 struct gprs_ns_ie_ip6_elem,
Alexander Couzens71128672021-06-05 18:44:01 +02001027 gss->remote.num_ip6 + num_v6);
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001028 /* append the new entries to the end of the list */
Alexander Couzens71128672021-06-05 18:44:01 +02001029 memcpy(&gss->remote.ip6[gss->remote.num_ip6], v6_list, num_v6*sizeof(*v6_list));
1030 gss->remote.num_ip6 += num_v6;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001031
Alexander Couzens71128672021-06-05 18:44:01 +02001032 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %d entries\n",
1033 gss->remote.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +02001034 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001035
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001036 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001037}
1038
Harald Welte694dad52021-03-23 15:22:16 +01001039static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens790a9632021-02-05 17:18:39 +01001040{
1041 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1042
Harald Weltef61a9152021-03-02 22:20:17 +01001043 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1044
Harald Welte694dad52021-03-23 15:22:16 +01001045 if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN)
Alexander Couzens790a9632021-02-05 17:18:39 +01001046 gss->N = 0;
1047}
1048
Harald Welte694dad52021-03-23 15:22:16 +01001049static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +02001050{
1051 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001052 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1053 uint8_t cause;
1054 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001055
Harald Weltef61a9152021-03-02 22:20:17 +01001056 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1057
Alexander Couzens6a161492020-07-12 13:45:50 +02001058 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001059 case NS2_SNS_EV_RX_CONFIG_END:
1060 case NS2_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001061 rc = ns_sns_append_remote_eps(fi, data);
1062 if (rc < 0) {
1063 cause = -rc;
1064 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1065 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1066 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001067 }
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001068 if (event == NS2_SNS_EV_RX_CONFIG_END) {
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001069 /* check if sum of data / sig weights == 0 */
Alexander Couzens019da4b2021-06-06 02:48:18 +02001070 if (ip46_weight_sum_data(&gss->remote) == 0 || ip46_weight_sum_sig(&gss->remote) == 0) {
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001071 cause = NS_CAUSE_INVAL_WEIGH;
1072 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1073 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1074 return;
1075 }
1076 create_missing_nsvcs(fi);
1077 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1078 /* start the test procedure on ALL NSVCs! */
1079 gprs_ns2_start_alive_all_nsvcs(nse);
1080 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1081 } else {
1082 /* just send CONFIG-ACK */
1083 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1084 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001085 }
1086 break;
1087 default:
1088 OSMO_ASSERT(0);
1089 }
1090}
1091
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001092/* called when receiving NS2_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001093static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1094 struct ns2_sns_state *gss,
1095 struct tlv_parsed *tp)
1096{
1097 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1098 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1099 int num_v4 = 0, num_v6 = 0;
1100 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001101 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001102 int rc = 0;
1103
1104 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1105 * check uniqueness within the lists (no doublicate entries)
1106 * check not-known-by-us and sent back a list of unknown/known values
1107 * (abnormal behaviour according to 48.016)
1108 */
1109
1110 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
Alexander Couzens077ce5a2021-06-06 01:32:45 +02001111 if (gss->family == AF_INET) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001112 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1113 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1114 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1115 return;
1116 }
1117
1118 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1119 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001120 for (i = 0; i < num_v4; i++) {
1121 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001122 rc = do_sns_add(fi, &v4_list[i], NULL);
1123 if (rc < 0) {
1124 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001125 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001126 do_sns_delete(fi, &v4_list[j], NULL);
1127 cause = -rc;
1128 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1129 break;
1130 }
1131 }
1132 } else { /* IPv6 */
1133 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1134 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1135 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1136 return;
1137 }
1138
1139 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1140 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001141 for (i = 0; i < num_v6; i++) {
1142 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001143 rc = do_sns_add(fi, NULL, &v6_list[i]);
1144 if (rc < 0) {
1145 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001146 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001147 do_sns_delete(fi, NULL, &v6_list[j]);
1148 cause = -rc;
1149 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1150 break;
1151 }
1152 }
1153 }
1154
1155 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1156 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1157}
1158
1159static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1160 struct ns2_sns_state *gss,
1161 struct tlv_parsed *tp)
1162{
1163 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1164 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1165 int num_v4 = 0, num_v6 = 0;
1166 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001167 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001168 int rc = 0;
1169
1170 /* TODO: split up delete into v4 + v6
1171 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1172 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1173 */
1174 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
Alexander Couzens077ce5a2021-06-06 01:32:45 +02001175 if (gss->family == AF_INET) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001176 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1177 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1178 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001179 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001180 rc = do_sns_delete(fi, &v4_list[i], NULL);
1181 if (rc < 0) {
1182 cause = -rc;
1183 /* continue to delete others */
1184 }
1185 }
1186 if (cause != 0xff) {
1187 /* TODO: create list of not-deleted and return it */
1188 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1189 return;
1190 }
1191
1192 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1193 /* delete all NS-VCs for given IPv4 address */
1194 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1195 struct gprs_ns_ie_ip4_elem *ip4_remote;
1196 uint32_t ip_addr = *(uint32_t *)(ie+1);
1197 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1198 cause = NS_CAUSE_UNKN_IP_ADDR;
1199 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1200 return;
1201 }
1202 /* make a copy as do_sns_delete() will change the array underneath us */
Alexander Couzens71128672021-06-05 18:44:01 +02001203 ip4_remote = talloc_memdup(fi, gss->remote.ip4,
1204 gss->remote.num_ip4 * sizeof(*v4_list));
1205 for (i = 0; i < gss->remote.num_ip4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001206 if (ip4_remote[i].ip_addr == ip_addr) {
1207 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1208 if (rc < 0) {
1209 cause = -rc;
1210 /* continue to delete others */
1211 }
1212 }
1213 }
1214 talloc_free(ip4_remote);
1215 if (cause != 0xff) {
1216 /* TODO: create list of not-deleted and return it */
1217 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1218 return;
1219 }
1220 } else {
1221 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1222 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1223 return;
1224 }
1225 } else { /* IPv6 */
1226 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1227 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1228 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001229 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001230 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1231 if (rc < 0) {
1232 cause = -rc;
1233 /* continue to delete others */
1234 }
1235 }
1236 if (cause != 0xff) {
1237 /* TODO: create list of not-deleted and return it */
1238 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1239 return;
1240 }
1241 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1242 /* delete all NS-VCs for given IPv4 address */
1243 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1244 struct gprs_ns_ie_ip6_elem *ip6_remote;
1245 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001246 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001247 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1248 cause = NS_CAUSE_UNKN_IP_ADDR;
1249 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1250 return;
1251 }
1252 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1253 /* make a copy as do_sns_delete() will change the array underneath us */
Alexander Couzens71128672021-06-05 18:44:01 +02001254 ip6_remote = talloc_memdup(fi, gss->remote.ip6,
1255 gss->remote.num_ip6 * sizeof(*v4_list));
1256 for (i = 0; i < gss->remote.num_ip6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001257 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1258 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1259 if (rc < 0) {
1260 cause = -rc;
1261 /* continue to delete others */
1262 }
1263 }
1264 }
1265
1266 talloc_free(ip6_remote);
1267 if (cause != 0xff) {
1268 /* TODO: create list of not-deleted and return it */
1269 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1270 return;
1271 }
1272 } else {
1273 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1274 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1275 return;
1276 }
1277 }
1278 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1279}
1280
1281static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1282 struct ns2_sns_state *gss,
1283 struct tlv_parsed *tp)
1284{
1285 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1286 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1287 int num_v4 = 0, num_v6 = 0;
1288 uint8_t trans_id, cause = 0xff;
1289 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001290 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001291
1292 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1293 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1294 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1295 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001296 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001297 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1298 if (rc < 0) {
1299 cause = -rc;
1300 /* continue to others */
1301 }
1302 }
1303 if (cause != 0xff) {
1304 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1305 return;
1306 }
1307 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1308 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1309 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001310 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001311 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1312 if (rc < 0) {
1313 cause = -rc;
1314 /* continue to others */
1315 }
1316 }
1317 if (cause != 0xff) {
1318 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1319 return;
1320 }
1321 } else {
1322 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1323 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1324 return;
1325 }
1326 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1327}
1328
1329static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1330{
1331 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1332 struct tlv_parsed *tp = data;
1333
1334 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001335 case NS2_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001336 ns2_sns_st_configured_add(fi, gss, tp);
1337 break;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001338 case NS2_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001339 ns2_sns_st_configured_delete(fi, gss, tp);
1340 break;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001341 case NS2_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001342 ns2_sns_st_configured_change(fi, gss, tp);
1343 break;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001344 case NS2_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001345 osmo_timer_del(&fi->timer);
1346 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001347 }
1348}
1349
1350static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1351{
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001352 struct gprs_ns2_vc *nsvc;
1353 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001354 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001355 /* NS-VC status updates are only parsed in ST_CONFIGURED.
1356 * Do an initial check if there are any nsvc alive atm */
1357 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1358 if (ns2_vc_is_unblocked(nsvc)) {
1359 gss->alive = true;
1360 osmo_timer_del(&fi->timer);
1361 break;
1362 }
1363 }
1364
Alexander Couzens53e70092021-04-06 15:45:47 +02001365 /* remove the initial NSVC if the NSVC isn't part of the configuration */
1366 if (gss->sns_nsvc->sns_only)
1367 gprs_ns2_free_nsvc(gss->sns_nsvc);
1368
Alexander Couzens138b96f2021-01-25 16:23:29 +01001369 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001370}
1371
1372static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1373 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001374 .in_event_mask = 0, /* handled by all_state_action */
Alexander Couzens0a7c5ee2021-04-10 18:20:21 +02001375 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1376 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001377 .name = "UNCONFIGURED",
Harald Welte694dad52021-03-23 15:22:16 +01001378 .action = ns2_sns_st_bss_unconfigured,
Alexander Couzens6a161492020-07-12 13:45:50 +02001379 },
Harald Welte694dad52021-03-23 15:22:16 +01001380 [GPRS_SNS_ST_BSS_SIZE] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001381 .in_event_mask = S(NS2_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001382 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001383 S(GPRS_SNS_ST_BSS_SIZE) |
1384 S(GPRS_SNS_ST_BSS_CONFIG_BSS),
1385 .name = "BSS_SIZE",
1386 .action = ns2_sns_st_bss_size,
1387 .onenter = ns2_sns_st_bss_size_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001388 },
Harald Welte694dad52021-03-23 15:22:16 +01001389 [GPRS_SNS_ST_BSS_CONFIG_BSS] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001390 .in_event_mask = S(NS2_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001391 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001392 S(GPRS_SNS_ST_BSS_CONFIG_BSS) |
1393 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
1394 S(GPRS_SNS_ST_BSS_SIZE),
1395 .name = "BSS_CONFIG_BSS",
1396 .action = ns2_sns_st_bss_config_bss,
1397 .onenter = ns2_sns_st_bss_config_bss_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001398 },
Harald Welte694dad52021-03-23 15:22:16 +01001399 [GPRS_SNS_ST_BSS_CONFIG_SGSN] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001400 .in_event_mask = S(NS2_SNS_EV_RX_CONFIG) |
1401 S(NS2_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001402 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001403 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens6a161492020-07-12 13:45:50 +02001404 S(GPRS_SNS_ST_CONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001405 S(GPRS_SNS_ST_BSS_SIZE),
1406 .name = "BSS_CONFIG_SGSN",
1407 .action = ns2_sns_st_bss_config_sgsn,
1408 .onenter = ns2_sns_st_bss_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001409 },
1410 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001411 .in_event_mask = S(NS2_SNS_EV_RX_ADD) |
1412 S(NS2_SNS_EV_RX_DELETE) |
1413 S(NS2_SNS_EV_RX_CHANGE_WEIGHT) |
1414 S(NS2_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001415 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001416 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001417 .name = "CONFIGURED",
1418 .action = ns2_sns_st_configured,
1419 .onenter = ns2_sns_st_configured_onenter,
1420 },
1421};
1422
1423static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1424{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001425 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001426 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1427 struct gprs_ns2_inst *nsi = nse->nsi;
1428
Alexander Couzens90ee9632020-12-07 06:18:32 +01001429 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001430 switch (fi->T) {
1431 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001432 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
Alexander Couzens652ab4d2021-06-12 23:09:46 +02001433 sns_failed(fi, "Size retries failed. Selecting next IP-SNS endpoint.");
Alexander Couzensa367d082020-12-21 14:06:24 +01001434 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001435 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 +01001436 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001437 break;
1438 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001439 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens652ab4d2021-06-12 23:09:46 +02001440 sns_failed(fi, "BSS Config retries failed. Selecting next IP-SNS endpoint");
Alexander Couzensa367d082020-12-21 14:06:24 +01001441 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001442 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 +01001443 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001444 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001445 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001446 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens652ab4d2021-06-12 23:09:46 +02001447 sns_failed(fi, "SGSN Config retries failed. Selecting next IP-SNS endpoint.");
Alexander Couzens3df58862021-02-05 17:18:08 +01001448 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001449 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 +01001450 }
1451 break;
1452 case 4:
Alexander Couzens652ab4d2021-06-12 23:09:46 +02001453 sns_failed(fi, "Config succeeded but no NS-VC came online. Selecting next IP-SNS endpoint.");
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001454 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001455 }
1456 return 0;
1457}
1458
Harald Welte9e37bf42021-03-02 20:48:31 +01001459/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001460static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1461{
1462 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001463 struct ns2_sns_bind *sbind;
1464 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001465
Alexander Couzense769f522020-12-07 07:37:07 +01001466 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001467 case NS2_SNS_EV_REQ_ADD_BIND:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001468 sbind = data;
1469 switch (fi->state) {
1470 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001471 osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001472 break;
Harald Welte694dad52021-03-23 15:22:16 +01001473 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001474 /* TODO: add the ip4 element to the list */
1475 break;
Harald Welte694dad52021-03-23 15:22:16 +01001476 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1477 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001478 case GPRS_SNS_ST_CONFIGURED:
1479 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1480 break;
1481 }
1482 break;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001483 case NS2_SNS_EV_REQ_DELETE_BIND:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001484 sbind = data;
1485 switch (fi->state) {
1486 case GPRS_SNS_ST_UNCONFIGURED:
1487 break;
Harald Welte694dad52021-03-23 15:22:16 +01001488 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001489 /* TODO: remove the ip4 element from the list */
1490 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1491 if (nsvc->bind == sbind->bind) {
1492 gprs_ns2_free_nsvc(nsvc);
1493 }
1494 }
1495 break;
Harald Welte694dad52021-03-23 15:22:16 +01001496 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1497 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001498 case GPRS_SNS_ST_CONFIGURED:
1499 /* TODO: do an delete SNS-IP procedure */
1500 /* TODO: remove the ip4 element to the list */
1501 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1502 if (nsvc->bind == sbind->bind) {
1503 gprs_ns2_free_nsvc(nsvc);
1504 }
1505 }
1506 break;
1507 }
1508 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1509 talloc_free(sbind);
1510 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001511 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001512}
1513
Alexander Couzens31d52e12021-06-05 20:04:04 +02001514/* validate the bss configuration (sns endpoint and binds)
1515 * - no endpoints -> invalid
1516 * - no binds -> invalid
1517 * - only v4 sns endpoints, only v6 binds -> invalid
1518 * - only v4 sns endpoints, but v4 sig weights == 0 -> invalid ...
1519 */
1520static int ns2_sns_bss_valid_configuration(struct ns2_sns_state *gss)
1521{
1522 struct ns2_sns_bind *sbind;
1523 struct sns_endpoint *endpoint;
1524 const struct osmo_sockaddr *addr;
1525 int v4_sig = 0, v4_data = 0, v6_sig = 0, v6_data = 0;
1526 bool v4_endpoints = false;
1527 bool v6_endpoints = false;
1528
1529 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds))
1530 return 0;
1531
1532 llist_for_each_entry(sbind, &gss->binds, list) {
1533 addr = gprs_ns2_ip_bind_sockaddr(sbind->bind);
1534 if (!addr)
1535 continue;
1536 switch (addr->u.sa.sa_family) {
1537 case AF_INET:
1538 v4_sig += sbind->bind->sns_sig_weight;
1539 v4_data += sbind->bind->sns_data_weight;
1540 break;
1541 case AF_INET6:
1542 v6_sig += sbind->bind->sns_sig_weight;
1543 v6_data += sbind->bind->sns_data_weight;
1544 break;
1545 }
1546 }
1547
1548 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
1549 switch (endpoint->saddr.u.sa.sa_family) {
1550 case AF_INET:
1551 v4_endpoints = true;
1552 break;
1553 case AF_INET6:
1554 v6_endpoints = true;
1555 break;
1556 }
1557 }
1558
1559 return (v4_endpoints && v4_sig && v4_data) || (v6_endpoints && v6_sig && v6_data);
1560}
1561
Harald Welte9e37bf42021-03-02 20:48:31 +01001562/* allstate-action for BSS role */
1563static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1564{
1565 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1566 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1567
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001568 /* reset when receiving NS2_SNS_EV_REQ_NO_NSVC */
Harald Welte9e37bf42021-03-02 20:48:31 +01001569 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001570 case NS2_SNS_EV_REQ_NO_NSVC:
Harald Welte9e37bf42021-03-02 20:48:31 +01001571 /* ignore reselection running */
1572 if (gss->reselection_running)
1573 break;
1574
Alexander Couzens652ab4d2021-06-12 23:09:46 +02001575 sns_failed(fi, "no remaining NSVC, resetting SNS FSM");
Harald Welte9e37bf42021-03-02 20:48:31 +01001576 break;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001577 case NS2_SNS_EV_REQ_SELECT_ENDPOINT:
Harald Welte9e37bf42021-03-02 20:48:31 +01001578 /* tear down previous state
1579 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1580 gss->reselection_running = true;
1581 gprs_ns2_free_nsvcs(nse);
Alexander Couzensd2c6c492021-06-06 01:57:52 +02001582 ns2_clear_elems(&gss->local);
1583 ns2_clear_elems(&gss->remote);
Harald Welte9e37bf42021-03-02 20:48:31 +01001584
1585 /* Choose the next sns endpoint. */
Alexander Couzens31d52e12021-06-05 20:04:04 +02001586 if (!ns2_sns_bss_valid_configuration(gss)) {
Harald Welte9e37bf42021-03-02 20:48:31 +01001587 gss->initial = NULL;
1588 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1589 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1590 return;
1591 } else if (!gss->initial) {
1592 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1593 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1594 /* last entry, continue with first */
1595 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1596 } else {
1597 /* next element is an entry */
1598 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1599 }
1600
Alexander Couzens68ab9c42021-06-06 03:03:40 +02001601 gss->family = gss->initial->saddr.u.sa.sa_family;
Harald Welte9e37bf42021-03-02 20:48:31 +01001602 gss->reselection_running = false;
Harald Welte694dad52021-03-23 15:22:16 +01001603 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 +01001604 break;
1605 default:
1606 ns2_sns_st_all_action(fi, event, data);
1607 break;
1608 }
1609}
1610
Alexander Couzens6a161492020-07-12 13:45:50 +02001611static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1612 .name = "GPRS-NS2-SNS-BSS",
1613 .states = ns2_sns_bss_states,
1614 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001615 .allstate_event_mask = S(NS2_SNS_EV_REQ_NO_NSVC) |
1616 S(NS2_SNS_EV_REQ_SELECT_ENDPOINT) |
1617 S(NS2_SNS_EV_REQ_ADD_BIND) |
1618 S(NS2_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001619 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001620 .cleanup = NULL,
1621 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001622 .event_names = gprs_sns_event_names,
1623 .pre_term = NULL,
1624 .log_subsys = DLNS,
1625};
1626
Harald Welte5bef2cc2020-09-18 22:33:24 +02001627/*! Allocate an IP-SNS FSM for the BSS side.
1628 * \param[in] nse NS Entity in which the FSM runs
1629 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001630 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001631struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1632 const char *id)
1633{
1634 struct osmo_fsm_inst *fi;
1635 struct ns2_sns_state *gss;
1636
1637 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1638 if (!fi)
1639 return fi;
1640
1641 gss = talloc_zero(fi, struct ns2_sns_state);
1642 if (!gss)
1643 goto err;
1644
1645 fi->priv = gss;
1646 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001647 gss->role = GPRS_SNS_ROLE_BSS;
Harald Welte24f4df52021-03-04 18:02:54 +01001648 /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */
1649 gss->num_max_ip4_remote = 8192;
1650 gss->num_max_ip6_remote = 8192;
Alexander Couzense769f522020-12-07 07:37:07 +01001651 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001652 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001653
1654 return fi;
1655err:
1656 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1657 return NULL;
1658}
1659
Harald Welte5bef2cc2020-09-18 22:33:24 +02001660/*! main entry point for receiving SNS messages from the network.
1661 * \param[in] nsvc NS-VC on which the message was received
1662 * \param[in] msg message buffer of the IP-SNS message
1663 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001664 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001665int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001666{
1667 struct gprs_ns2_nse *nse = nsvc->nse;
1668 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1669 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001670 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001671 struct osmo_fsm_inst *fi;
Alexander Couzens7619ed42021-03-24 17:44:03 +01001672 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001673
1674 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001675 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1676 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001677 rc = -EINVAL;
1678 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +02001679 }
1680
Alexander Couzens6a161492020-07-12 13:45:50 +02001681 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1682 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001683 gss = (struct ns2_sns_state *) fi->priv;
1684 if (!gss->sns_nsvc)
1685 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001686
Harald Weltef2949742021-01-20 14:54:14 +01001687 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1688 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1689
Alexander Couzens6a161492020-07-12 13:45:50 +02001690 switch (nsh->pdu_type) {
1691 case SNS_PDUT_SIZE:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001692 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001693 break;
1694 case SNS_PDUT_SIZE_ACK:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001695 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001696 break;
1697 case SNS_PDUT_CONFIG:
1698 if (nsh->data[0] & 0x01)
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001699 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001700 else
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001701 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001702 break;
1703 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001704 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001705 break;
1706 case SNS_PDUT_ADD:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001707 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001708 break;
1709 case SNS_PDUT_DELETE:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001710 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001711 break;
1712 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001713 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001714 break;
1715 case SNS_PDUT_ACK:
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001716 osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001717 break;
1718 default:
Harald Weltef2949742021-01-20 14:54:14 +01001719 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1720 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001721 rc = -EINVAL;
Alexander Couzens6a161492020-07-12 13:45:50 +02001722 }
1723
Alexander Couzens7619ed42021-03-24 17:44:03 +01001724out:
1725 msgb_free(msg);
1726
1727 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001728}
1729
1730#include <osmocom/vty/vty.h>
1731#include <osmocom/vty/misc.h>
1732
Harald Welte1262c4f2021-01-19 20:58:33 +01001733static 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 +02001734{
1735 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001736 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001737 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1738}
1739
Harald Welte1262c4f2021-01-19 20:58:33 +01001740static 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 +02001741{
1742 char ip_addr[INET6_ADDRSTRLEN] = {};
1743 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1744 strcpy(ip_addr, "Invalid IPv6");
1745
Harald Welte1262c4f2021-01-19 20:58:33 +01001746 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001747 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1748}
1749
Harald Welte5bef2cc2020-09-18 22:33:24 +02001750/*! Dump the IP-SNS state to a vty.
1751 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001752 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001753 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1754 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001755void 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 +02001756{
1757 struct ns2_sns_state *gss;
1758 unsigned int i;
1759
1760 if (!nse->bss_sns_fi)
1761 return;
1762
Harald Welte1262c4f2021-01-19 20:58:33 +01001763 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001764 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1765
Harald Welte1262c4f2021-01-19 20:58:33 +01001766 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1767 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001768
Alexander Couzens71128672021-06-05 18:44:01 +02001769 if (gss->local.num_ip4 && gss->remote.num_ip4) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001770 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001771 for (i = 0; i < gss->local.num_ip4; i++)
1772 vty_dump_sns_ip4(vty, prefix, &gss->local.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001773
Harald Welte1262c4f2021-01-19 20:58:33 +01001774 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001775 for (i = 0; i < gss->remote.num_ip4; i++)
1776 vty_dump_sns_ip4(vty, prefix, &gss->remote.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001777 }
1778
Alexander Couzens71128672021-06-05 18:44:01 +02001779 if (gss->local.num_ip6 && gss->remote.num_ip6) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001780 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001781 for (i = 0; i < gss->local.num_ip6; i++)
1782 vty_dump_sns_ip6(vty, prefix, &gss->local.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001783
Harald Welte1262c4f2021-01-19 20:58:33 +01001784 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001785 for (i = 0; i < gss->remote.num_ip6; i++)
1786 vty_dump_sns_ip6(vty, prefix, &gss->remote.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001787 }
1788}
1789
Alexander Couzens412bc342020-11-19 05:24:37 +01001790/*! write IP-SNS to a vty
1791 * \param[in] vty VTY to which the state shall be printed
1792 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001793void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001794{
1795 struct ns2_sns_state *gss;
1796 struct osmo_sockaddr_str addr_str;
1797 struct sns_endpoint *endpoint;
1798
1799 if (!nse->bss_sns_fi)
1800 return;
1801
1802 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1803 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001804 /* It's unlikely that an error happens, but let's better be safe. */
1805 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1806 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001807 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001808 }
1809}
1810
Alexander Couzense769f522020-12-07 07:37:07 +01001811static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1812 const struct osmo_sockaddr *saddr)
1813{
1814 struct sns_endpoint *endpoint;
1815
1816 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1817 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1818 return endpoint;
1819 }
1820
1821 return NULL;
1822}
1823
1824/*! gprs_ns2_sns_add_endpoint
1825 * \param[in] nse
1826 * \param[in] sockaddr
1827 * \return
1828 */
1829int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1830 const struct osmo_sockaddr *saddr)
1831{
1832 struct ns2_sns_state *gss;
1833 struct sns_endpoint *endpoint;
1834 bool do_selection = false;
1835
1836 if (nse->ll != GPRS_NS2_LL_UDP) {
1837 return -EINVAL;
1838 }
1839
Alexander Couzens138b96f2021-01-25 16:23:29 +01001840 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001841 return -EINVAL;
1842 }
1843
1844 gss = nse->bss_sns_fi->priv;
1845
1846 if (ns2_get_sns_endpoint(gss, saddr))
1847 return -EADDRINUSE;
1848
1849 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1850 if (!endpoint)
1851 return -ENOMEM;
1852
1853 endpoint->saddr = *saddr;
1854 if (llist_empty(&gss->sns_endpoints))
1855 do_selection = true;
1856
1857 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1858 if (do_selection)
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001859 osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001860
1861 return 0;
1862}
1863
1864/*! gprs_ns2_sns_del_endpoint
1865 * \param[in] nse
1866 * \param[in] sockaddr
1867 * \return 0 on success, otherwise < 0
1868 */
1869int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1870 const struct osmo_sockaddr *saddr)
1871{
1872 struct ns2_sns_state *gss;
1873 struct sns_endpoint *endpoint;
1874
1875 if (nse->ll != GPRS_NS2_LL_UDP) {
1876 return -EINVAL;
1877 }
1878
Alexander Couzens138b96f2021-01-25 16:23:29 +01001879 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001880 return -EINVAL;
1881 }
1882
1883 gss = nse->bss_sns_fi->priv;
1884 endpoint = ns2_get_sns_endpoint(gss, saddr);
1885 if (!endpoint)
1886 return -ENOENT;
1887
1888 /* if this is an unused SNS endpoint it's done */
1889 if (gss->initial != endpoint) {
1890 llist_del(&endpoint->list);
1891 talloc_free(endpoint);
1892 return 0;
1893 }
1894
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001895 /* gprs_ns2_free_nsvcs() will trigger NS2_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001896 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001897 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001898 "Closing all NS-VC and restart SNS-SIZE procedure"
1899 "with a remaining SNS endpoint.\n");
1900
1901 /* Continue with the next endpoint in the list.
1902 * Special case if the endpoint is at the start or end of the list */
1903 if (endpoint->list.prev == &gss->sns_endpoints ||
1904 endpoint->list.next == &gss->sns_endpoints)
1905 gss->initial = NULL;
1906 else
1907 gss->initial = llist_entry(endpoint->list.next->prev,
1908 struct sns_endpoint,
1909 list);
1910
1911 llist_del(&endpoint->list);
1912 gprs_ns2_free_nsvcs(nse);
1913 talloc_free(endpoint);
1914
1915 return 0;
1916}
1917
1918/*! gprs_ns2_sns_count
1919 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1920 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1921 */
1922int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1923{
1924 struct ns2_sns_state *gss;
1925 struct sns_endpoint *endpoint;
1926 int count = 0;
1927
1928 if (nse->ll != GPRS_NS2_LL_UDP) {
1929 return -EINVAL;
1930 }
1931
Alexander Couzens138b96f2021-01-25 16:23:29 +01001932 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001933 return -EINVAL;
1934 }
1935
1936 gss = nse->bss_sns_fi->priv;
1937 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1938 count++;
1939
1940 return count;
1941}
1942
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001943void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1944{
1945 struct ns2_sns_state *gss;
1946 struct gprs_ns2_vc *tmp;
1947
1948 if (!nse->bss_sns_fi)
1949 return;
1950
1951 gss = nse->bss_sns_fi->priv;
1952 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1953 return;
1954
1955 if (alive == gss->alive)
1956 return;
1957
1958 /* check if this is the current SNS NS-VC */
1959 if (nsvc == gss->sns_nsvc) {
1960 /* only replace the SNS NS-VC if there are other alive NS-VC.
1961 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1962 * and couldn't confirm yet if the NS-VC comes up */
1963 if (gss->alive && !alive)
1964 ns2_sns_replace_nsvc(nsvc);
1965 }
1966
1967 if (alive) {
1968 gss->alive = true;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001969 osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001970 } else {
1971 /* is there at least another alive nsvc? */
1972 llist_for_each_entry(tmp, &nse->nsvc, list) {
1973 if (ns2_vc_is_unblocked(tmp))
1974 return;
1975 }
1976
1977 /* all NS-VC have failed */
1978 gss->alive = false;
Alexander Couzens175eb7b2021-07-20 18:41:14 +02001979 osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001980 }
1981}
1982
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001983int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1984 struct gprs_ns2_vc_bind *bind)
1985{
1986 struct ns2_sns_state *gss;
1987 struct ns2_sns_bind *tmp;
1988
1989 OSMO_ASSERT(nse->bss_sns_fi);
1990 gss = nse->bss_sns_fi->priv;
1991
1992 if (!gprs_ns2_is_ip_bind(bind)) {
1993 return -EINVAL;
1994 }
1995
1996 if (!llist_empty(&gss->binds)) {
1997 llist_for_each_entry(tmp, &gss->binds, list) {
1998 if (tmp->bind == bind)
1999 return -EALREADY;
2000 }
2001 }
2002
2003 tmp = talloc_zero(gss, struct ns2_sns_bind);
2004 if (!tmp)
2005 return -ENOMEM;
2006 tmp->bind = bind;
2007 llist_add_tail(&tmp->list, &gss->binds);
2008
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002009 osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_ADD_BIND, tmp);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002010 return 0;
2011}
2012
2013/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
2014int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
2015 struct gprs_ns2_vc_bind *bind)
2016{
2017 struct ns2_sns_state *gss;
2018 struct ns2_sns_bind *tmp, *tmp2;
2019 bool found = false;
2020
2021 if (!nse->bss_sns_fi)
2022 return -EINVAL;
2023
2024 gss = nse->bss_sns_fi->priv;
2025 if (gss->initial_bind && gss->initial_bind->bind == bind) {
2026 if (gss->initial_bind->list.prev == &gss->binds)
2027 gss->initial_bind = NULL;
2028 else
2029 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
2030 }
2031
2032 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
2033 if (tmp->bind == bind) {
2034 llist_del(&tmp->list);
2035 found = true;
Alexander Couzensa35c2962021-04-19 03:30:15 +02002036 break;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002037 }
2038 }
2039
2040 if (!found)
2041 return -ENOENT;
2042
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002043 osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_DELETE_BIND, tmp);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002044 return 0;
2045}
2046
Alexander Couzens71128672021-06-05 18:44:01 +02002047/* Update SNS weights for a bind (local endpoint).
2048 * \param[in] bind the bind which has been updated
Alexander Couzensc4704762021-02-08 23:13:12 +01002049 */
2050void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
2051{
2052 /* TODO: implement weights after binds per sns implemented */
2053}
2054
Harald Welte4f127462021-03-02 20:49:10 +01002055
2056
2057
2058/***********************************************************************
2059 * SGSN role
2060 ***********************************************************************/
2061
2062static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2063{
2064 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2065 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2066 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
2067}
2068
2069/* We're waiting for inbound SNS-CONFIG from the BSS */
2070static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2071{
2072 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2073 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2074 struct gprs_ns2_inst *nsi = nse->nsi;
2075 uint8_t cause;
2076 int rc;
2077
2078 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2079
2080 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002081 case NS2_SNS_EV_RX_CONFIG:
2082 case NS2_SNS_EV_RX_CONFIG_END:
Harald Welte4f127462021-03-02 20:49:10 +01002083 rc = ns_sns_append_remote_eps(fi, data);
2084 if (rc < 0) {
2085 cause = -rc;
2086 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2087 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2088 return;
2089 }
2090 /* only change state if last CONFIG was received */
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002091 if (event == NS2_SNS_EV_RX_CONFIG_END) {
Harald Welte4f127462021-03-02 20:49:10 +01002092 /* ensure sum of data weight / sig weights is > 0 */
Alexander Couzens019da4b2021-06-06 02:48:18 +02002093 if (ip46_weight_sum_data(&gss->remote) == 0 || ip46_weight_sum_sig(&gss->remote) == 0) {
Harald Welte4f127462021-03-02 20:49:10 +01002094 cause = NS_CAUSE_INVAL_WEIGH;
2095 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2096 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2097 break;
2098 }
2099 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2100 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2101 } else {
2102 /* just send CONFIG-ACK */
2103 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2104 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2105 }
2106 break;
2107 }
2108}
2109
2110static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2111{
2112 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2113 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2114
Harald Welte4f127462021-03-02 20:49:10 +01002115 /* transmit SGSN-oriented SNS-CONFIG */
Alexander Couzens71128672021-06-05 18:44:01 +02002116 ns2_tx_sns_config(gss->sns_nsvc, true, gss->local.ip4, gss->local.num_ip4,
2117 gss->local.ip6, gss->local.num_ip6);
Harald Welte4f127462021-03-02 20:49:10 +01002118}
2119
2120/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2121static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2122{
2123 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2124 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2125 struct tlv_parsed *tp = NULL;
2126
2127 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2128
2129 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002130 case NS2_SNS_EV_RX_CONFIG_ACK:
Harald Welte4f127462021-03-02 20:49:10 +01002131 tp = data;
2132 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2133 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2134 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2135 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2136 break;
2137 }
2138 /* we currently only send one SNS-CONFIG with END FLAG */
2139 if (true) {
2140 create_missing_nsvcs(fi);
2141 /* start the test procedure on ALL NSVCs! */
2142 gprs_ns2_start_alive_all_nsvcs(nse);
2143 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2144 }
2145 break;
2146 }
2147}
2148
2149/* SGSN-side SNS state machine */
2150static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2151 [GPRS_SNS_ST_UNCONFIGURED] = {
2152 .in_event_mask = 0, /* handled by all_state_action */
2153 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2154 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2155 .name = "UNCONFIGURED",
2156 .action = ns2_sns_st_sgsn_unconfigured,
2157 },
2158 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002159 .in_event_mask = S(NS2_SNS_EV_RX_CONFIG) |
2160 S(NS2_SNS_EV_RX_CONFIG_END),
Harald Welte4f127462021-03-02 20:49:10 +01002161 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2162 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2163 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2164 .name = "SGSN_WAIT_CONFIG",
2165 .action = ns2_sns_st_sgsn_wait_config,
2166 },
2167 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002168 .in_event_mask = S(NS2_SNS_EV_RX_CONFIG_ACK),
Harald Welte4f127462021-03-02 20:49:10 +01002169 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2170 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2171 S(GPRS_SNS_ST_CONFIGURED),
2172 .name = "SGSN_WAIT_CONFIG_ACK",
2173 .action = ns2_sns_st_sgsn_wait_config_ack,
2174 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2175 },
2176 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002177 .in_event_mask = S(NS2_SNS_EV_RX_ADD) |
2178 S(NS2_SNS_EV_RX_DELETE) |
2179 S(NS2_SNS_EV_RX_CHANGE_WEIGHT) |
2180 S(NS2_SNS_EV_REQ_NSVC_ALIVE),
Harald Welte4f127462021-03-02 20:49:10 +01002181 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2182 .name = "CONFIGURED",
2183 /* shared with BSS side; once configured there's no difference */
2184 .action = ns2_sns_st_configured,
2185 .onenter = ns2_sns_st_configured_onenter,
2186 },
2187};
2188
2189static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2190{
2191 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2192 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2193 struct gprs_ns2_inst *nsi = nse->nsi;
2194
2195 gss->N++;
2196 switch (fi->T) {
2197 case 3:
2198 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2199 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2200 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2201 } else {
2202 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2203 }
2204 break;
2205 case 4:
2206 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2207 break;
2208 }
2209 return 0;
2210}
2211
2212
2213/* allstate-action for SGSN role */
2214static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2215{
2216 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2217 struct tlv_parsed *tp = NULL;
Harald Welte01fa6a32021-03-04 19:49:38 +01002218 size_t num_local_eps, num_remote_eps;
Harald Welte4f127462021-03-02 20:49:10 +01002219 uint8_t flag;
Harald Weltea2c5af52021-03-04 17:59:35 +01002220 uint8_t cause;
Harald Welte4f127462021-03-02 20:49:10 +01002221
2222 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2223
2224 switch (event) {
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002225 case NS2_SNS_EV_RX_SIZE:
Harald Welte4f127462021-03-02 20:49:10 +01002226 tp = (struct tlv_parsed *) data;
Harald Weltea2c5af52021-03-04 17:59:35 +01002227 /* check for mandatory / conditional IEs */
2228 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) ||
2229 !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) {
2230 cause = NS_CAUSE_MISSING_ESSENT_IE;
2231 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
Alexander Couzens1c405252021-06-13 00:14:48 +02002232 if (fi->state == GPRS_SNS_ST_UNCONFIGURED)
2233 sns_failed(fi, "Rx Size: Missing essential IE");
Harald Weltea2c5af52021-03-04 17:59:35 +01002234 break;
2235 }
2236 if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) &&
2237 !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) {
2238 cause = NS_CAUSE_MISSING_ESSENT_IE;
Harald Welte4f127462021-03-02 20:49:10 +01002239 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
Alexander Couzens1c405252021-06-13 00:14:48 +02002240 if (fi->state == GPRS_SNS_ST_UNCONFIGURED)
2241 sns_failed(fi, "Rx Size: Missing essential IE");
Harald Welte4f127462021-03-02 20:49:10 +01002242 break;
2243 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002244 if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2))
2245 gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR);
2246 if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2))
2247 gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR);
2248 /* decide if we go for IPv4 or IPv6 */
Alexander Couzens077ce5a2021-06-06 01:32:45 +02002249 if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, AF_INET6)) {
2250 gss->family = AF_INET6;
Harald Welte2d807b62021-03-24 01:57:30 +01002251 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002252 num_local_eps = gss->local.num_ip6;
Harald Welte01fa6a32021-03-04 19:49:38 +01002253 num_remote_eps = gss->num_max_ip6_remote;
Alexander Couzens077ce5a2021-06-06 01:32:45 +02002254 } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, AF_INET)) {
2255 gss->family = AF_INET;
Harald Welte2d807b62021-03-24 01:57:30 +01002256 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002257 num_local_eps = gss->local.num_ip4;
Harald Welte01fa6a32021-03-04 19:49:38 +01002258 num_remote_eps = gss->num_max_ip4_remote;
2259 } else {
Alexander Couzens71128672021-06-05 18:44:01 +02002260 if (gss->local.num_ip4 && !gss->num_max_ip4_remote)
Harald Welte01fa6a32021-03-04 19:49:38 +01002261 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
2262 else
2263 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
2264 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
Alexander Couzens1c405252021-06-13 00:14:48 +02002265 if (fi->state == GPRS_SNS_ST_UNCONFIGURED)
2266 sns_failed(fi, "Rx Size: Invalid Nr of IPv4/IPv6 EPs");
Harald Welte01fa6a32021-03-04 19:49:38 +01002267 break;
2268 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002269 /* ensure number of NS-VCs is sufficient for full mesh */
2270 gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC);
2271 if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) {
2272 LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, "
2273 "but BSS supports only %zu maximum NS-VCs\n", num_local_eps,
2274 num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs);
2275 cause = NS_CAUSE_INVAL_NR_NS_VC;
2276 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
Alexander Couzens1c405252021-06-13 00:14:48 +02002277 if (fi->state == GPRS_SNS_ST_UNCONFIGURED)
2278 sns_failed(fi, NULL);
Harald Welte01fa6a32021-03-04 19:49:38 +01002279 break;
2280 }
2281 /* perform state reset, if requested */
Harald Welte4f127462021-03-02 20:49:10 +01002282 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2283 if (flag & 1) {
2284 struct gprs_ns2_vc *nsvc, *nsvc2;
2285 /* clear all state */
Harald Welte46eb7642021-03-04 17:49:59 +01002286 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
Harald Welte4f127462021-03-02 20:49:10 +01002287 gss->N = 0;
Alexander Couzensd2c6c492021-06-06 01:57:52 +02002288 ns2_clear_elems(&gss->local);
2289 ns2_clear_elems(&gss->remote);
Harald Welte4f127462021-03-02 20:49:10 +01002290 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2291 if (nsvc == gss->sns_nsvc) {
2292 /* keep the NSVC we need for SNS, but unconfigure it */
2293 nsvc->sig_weight = 0;
2294 nsvc->data_weight = 0;
2295 ns2_vc_force_unconfigured(nsvc);
2296 } else {
2297 /* free all other NS-VCs */
2298 gprs_ns2_free_nsvc(nsvc);
2299 }
2300 }
Harald Welte2d807b62021-03-24 01:57:30 +01002301 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte4f127462021-03-02 20:49:10 +01002302 }
Alexander Couzens1c405252021-06-13 00:14:48 +02002303
2304 if (fi->state == GPRS_SNS_ST_UNCONFIGURED && !(flag & 1)) {
2305 sns_failed(fi, "Rx Size without Reset flag, but NSE is unknown");
2306 break;
2307 }
2308
Harald Welte4f127462021-03-02 20:49:10 +01002309 /* send SIZE_ACK */
2310 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2311 /* only wait for SNS-CONFIG in case of Reset flag */
2312 if (flag & 1)
2313 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2314 break;
2315 default:
2316 ns2_sns_st_all_action(fi, event, data);
2317 break;
2318 }
2319}
2320
2321static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2322 .name = "GPRS-NS2-SNS-SGSN",
2323 .states = ns2_sns_sgsn_states,
2324 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
Alexander Couzens175eb7b2021-07-20 18:41:14 +02002325 .allstate_event_mask = S(NS2_SNS_EV_RX_SIZE) |
2326 S(NS2_SNS_EV_REQ_NO_NSVC) |
2327 S(NS2_SNS_EV_REQ_ADD_BIND) |
2328 S(NS2_SNS_EV_REQ_DELETE_BIND),
Harald Welte4f127462021-03-02 20:49:10 +01002329 .allstate_action = ns2_sns_st_all_action_sgsn,
2330 .cleanup = NULL,
2331 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2332 .event_names = gprs_sns_event_names,
2333 .pre_term = NULL,
2334 .log_subsys = DLNS,
2335};
2336
2337/*! Allocate an IP-SNS FSM for the SGSN side.
2338 * \param[in] nse NS Entity in which the FSM runs
2339 * \param[in] id string identifier
2340 * \returns FSM instance on success; NULL on error */
2341struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2342{
2343 struct osmo_fsm_inst *fi;
2344 struct ns2_sns_state *gss;
2345
2346 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2347 if (!fi)
2348 return fi;
2349
2350 gss = talloc_zero(fi, struct ns2_sns_state);
2351 if (!gss)
2352 goto err;
2353
2354 fi->priv = gss;
2355 gss->nse = nse;
2356 gss->role = GPRS_SNS_ROLE_SGSN;
2357 INIT_LLIST_HEAD(&gss->sns_endpoints);
2358 INIT_LLIST_HEAD(&gss->binds);
2359
2360 return fi;
2361err:
2362 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2363 return NULL;
2364}
2365
2366
2367
2368
Alexander Couzens6a161492020-07-12 13:45:50 +02002369/* initialize osmo_ctx on main tread */
2370static __attribute__((constructor)) void on_dso_load_ctx(void)
2371{
2372 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002373 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002374}