blob: 0c1ca6db6fa957095695cb5f7b661c386580309c [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \file gprs_ns2_sns.c
2 * NS Sub-Network Service Protocol implementation
3 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
4 * as well as its successor 3GPP TS 48.016 */
5
Harald Weltec1c7e4a2021-03-02 20:47:29 +01006/* (C) 2018-2021 by Harald Welte <laforge@gnumonks.org>
Alexander Couzens6a161492020-07-12 13:45:50 +02007 * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
8 * Author: Alexander Couzens <lynxis@fe80.eu>
9 *
10 * All Rights Reserved
11 *
12 * SPDX-License-Identifier: GPL-2.0+
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 *
27 */
28
29/* The BSS NSE only has one SGSN IP address configured, and it will use the SNS procedures
30 * to communicated its local IPs/ports as well as all the SGSN side IPs/ports and
31 * associated weights. The BSS then uses this to establish a full mesh
32 * of NSVCs between all BSS-side IPs/ports and SGSN-side IPs/ports.
33 *
34 * Known limitation/expectation/bugs:
35 * - No concurrent dual stack. It supports either IPv4 or IPv6, but not both at the same time.
36 * - SNS Add/Change/Delete: Doesn't answer on the same NSVC as received SNS ADD/CHANGE/DELETE PDUs.
37 * - SNS Add/Change/Delete: Doesn't communicated the failed IPv4/IPv6 entries on the SNS_ACK.
38 */
39
40#include <errno.h>
41
42#include <netinet/in.h>
43#include <arpa/inet.h>
44#include <stdint.h>
45
46#include <osmocom/core/fsm.h>
47#include <osmocom/core/msgb.h>
48#include <osmocom/core/socket.h>
Alexander Couzens412bc342020-11-19 05:24:37 +010049#include <osmocom/core/sockaddr_str.h>
Alexander Couzens6a161492020-07-12 13:45:50 +020050#include <osmocom/gsm/tlv.h>
51#include <osmocom/gprs/gprs_msgb.h>
52#include <osmocom/gprs/gprs_ns2.h>
53#include <osmocom/gprs/protocol/gsm_08_16.h>
54
55#include "gprs_ns2_internal.h"
56
57#define S(x) (1 << (x))
58
59enum ns2_sns_type {
60 IPv4,
61 IPv6,
62};
63
Harald Welte4f127462021-03-02 20:49:10 +010064enum ns2_sns_role {
65 GPRS_SNS_ROLE_BSS,
66 GPRS_SNS_ROLE_SGSN,
67};
68
Harald Welte694dad52021-03-23 15:22:16 +010069/* BSS-side-only states _ST_BSS_; SGSN-side only states _ST_SGSN_; others shared */
Alexander Couzens6a161492020-07-12 13:45:50 +020070enum gprs_sns_bss_state {
71 GPRS_SNS_ST_UNCONFIGURED,
Harald Welte694dad52021-03-23 15:22:16 +010072 GPRS_SNS_ST_BSS_SIZE, /*!< SNS-SIZE procedure ongoing */
73 GPRS_SNS_ST_BSS_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */
74 GPRS_SNS_ST_BSS_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */
Alexander Couzens6a161492020-07-12 13:45:50 +020075 GPRS_SNS_ST_CONFIGURED,
Harald Welte4f127462021-03-02 20:49:10 +010076 GPRS_SNS_ST_SGSN_WAIT_CONFIG, /* !< SGSN role: Wait for CONFIG from BSS */
77 GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, /* !< SGSN role: Wait for CONFIG-ACK from BSS */
Alexander Couzens6a161492020-07-12 13:45:50 +020078};
79
80enum gprs_sns_event {
Alexander Couzens67725e22021-02-15 02:37:03 +010081 GPRS_SNS_EV_REQ_SELECT_ENDPOINT, /*!< Select a SNS endpoint from the list */
82 GPRS_SNS_EV_RX_SIZE,
83 GPRS_SNS_EV_RX_SIZE_ACK,
84 GPRS_SNS_EV_RX_CONFIG,
85 GPRS_SNS_EV_RX_CONFIG_END, /*!< SNS-CONFIG with end flag received */
86 GPRS_SNS_EV_RX_CONFIG_ACK,
87 GPRS_SNS_EV_RX_ADD,
88 GPRS_SNS_EV_RX_DELETE,
89 GPRS_SNS_EV_RX_CHANGE_WEIGHT,
Harald Welteb9f23872021-03-02 20:48:54 +010090 GPRS_SNS_EV_RX_ACK, /*!< Rx of SNS-ACK (response to ADD/DELETE/CHG_WEIGHT */
Harald Welte04647e12021-03-02 18:50:40 +010091 GPRS_SNS_EV_REQ_NO_NSVC, /*!< no more NS-VC remaining (all dead) */
Alexander Couzens67725e22021-02-15 02:37:03 +010092 GPRS_SNS_EV_REQ_NSVC_ALIVE, /*!< a NS-VC became alive */
Harald Welte04647e12021-03-02 18:50:40 +010093 GPRS_SNS_EV_REQ_ADD_BIND, /*!< add a new local bind to this NSE */
94 GPRS_SNS_EV_REQ_DELETE_BIND, /*!< remove a local bind from this NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +020095};
96
97static const struct value_string gprs_sns_event_names[] = {
Alexander Couzens67725e22021-02-15 02:37:03 +010098 { GPRS_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" },
99 { GPRS_SNS_EV_RX_SIZE, "RX_SIZE" },
100 { GPRS_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" },
101 { GPRS_SNS_EV_RX_CONFIG, "RX_CONFIG" },
102 { GPRS_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" },
103 { GPRS_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" },
104 { GPRS_SNS_EV_RX_ADD, "RX_ADD" },
105 { GPRS_SNS_EV_RX_DELETE, "RX_DELETE" },
Harald Welteb9f23872021-03-02 20:48:54 +0100106 { GPRS_SNS_EV_RX_ACK, "RX_ACK" },
Alexander Couzens67725e22021-02-15 02:37:03 +0100107 { GPRS_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" },
108 { GPRS_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" },
109 { GPRS_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"},
110 { GPRS_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"},
111 { GPRS_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200112 { 0, NULL }
113};
114
Alexander Couzense769f522020-12-07 07:37:07 +0100115struct sns_endpoint {
116 struct llist_head list;
117 struct osmo_sockaddr saddr;
118};
119
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100120struct ns2_sns_bind {
121 struct llist_head list;
122 struct gprs_ns2_vc_bind *bind;
123};
124
Alexander Couzens71128672021-06-05 18:44:01 +0200125struct ns2_sns_elems {
126 struct gprs_ns_ie_ip4_elem *ip4;
127 unsigned int num_ip4;
128 struct gprs_ns_ie_ip6_elem *ip6;
129 unsigned int num_ip6;
130};
131
Alexander Couzens6a161492020-07-12 13:45:50 +0200132struct ns2_sns_state {
133 struct gprs_ns2_nse *nse;
134
135 enum ns2_sns_type ip;
Harald Welte4f127462021-03-02 20:49:10 +0100136 enum ns2_sns_role role; /* local role: BSS or SGSN */
Alexander Couzens6a161492020-07-12 13:45:50 +0200137
Alexander Couzense769f522020-12-07 07:37:07 +0100138 /* holds the list of initial SNS endpoints */
139 struct llist_head sns_endpoints;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100140 /* list of used struct ns2_sns_bind */
141 struct llist_head binds;
142 /* pointer to the bind which was used to initiate the SNS connection */
143 struct ns2_sns_bind *initial_bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100144 /* prevent recursive reselection */
145 bool reselection_running;
146
147 /* The current initial SNS endpoints.
148 * The initial connection will be moved into the NSE
149 * if configured via SNS. Otherwise it will be removed
150 * in configured state. */
151 struct sns_endpoint *initial;
Alexander Couzens6a161492020-07-12 13:45:50 +0200152 /* all SNS PDU will be sent over this nsvc */
153 struct gprs_ns2_vc *sns_nsvc;
Alexander Couzens90ee9632020-12-07 06:18:32 +0100154 /* timer N */
155 int N;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100156 /* true if at least one nsvc is alive */
157 bool alive;
Alexander Couzens6a161492020-07-12 13:45:50 +0200158
159 /* local configuration to send to the remote end */
Alexander Couzens71128672021-06-05 18:44:01 +0200160 struct ns2_sns_elems local;
Alexander Couzens6a161492020-07-12 13:45:50 +0200161
Alexander Couzens71128672021-06-05 18:44:01 +0200162 /* remote configuration as received */
163 struct ns2_sns_elems remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200164
165 /* local configuration about our capabilities in terms of connections to
166 * remote (SGSN) side */
167 size_t num_max_nsvcs;
168 size_t num_max_ip4_remote;
169 size_t num_max_ip6_remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200170};
171
172static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi)
173{
174 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
175 return gss->nse;
176}
177
178/* helper function to compute the sum of all (data or signaling) weights */
179static int ip4_weight_sum(const struct gprs_ns_ie_ip4_elem *ip4, unsigned int num,
180 bool data_weight)
181{
182 unsigned int i;
183 int weight_sum = 0;
184
185 for (i = 0; i < num; i++) {
186 if (data_weight)
187 weight_sum += ip4[i].data_weight;
188 else
189 weight_sum += ip4[i].sig_weight;
190 }
191 return weight_sum;
192}
193#define ip4_weight_sum_data(x,y) ip4_weight_sum(x, y, true)
194#define ip4_weight_sum_sig(x,y) ip4_weight_sum(x, y, false)
195
196/* helper function to compute the sum of all (data or signaling) weights */
197static int ip6_weight_sum(const struct gprs_ns_ie_ip6_elem *ip6, unsigned int num,
198 bool data_weight)
199{
200 unsigned int i;
201 int weight_sum = 0;
202
203 for (i = 0; i < num; i++) {
204 if (data_weight)
205 weight_sum += ip6[i].data_weight;
206 else
207 weight_sum += ip6[i].sig_weight;
208 }
209 return weight_sum;
210}
211#define ip6_weight_sum_data(x,y) ip6_weight_sum(x, y, true)
212#define ip6_weight_sum_sig(x,y) ip6_weight_sum(x, y, false)
213
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100214static int nss_weight_sum(const struct ns2_sns_state *nss, bool data_weight)
215{
Alexander Couzens71128672021-06-05 18:44:01 +0200216 return ip4_weight_sum(nss->remote.ip4, nss->remote.num_ip4, data_weight) +
217 ip6_weight_sum(nss->remote.ip6, nss->remote.num_ip6, data_weight);
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100218}
219#define nss_weight_sum_data(nss) nss_weight_sum(nss, true)
220#define nss_weight_sum_sig(nss) nss_weight_sum(nss, false)
221
Alexander Couzens6a161492020-07-12 13:45:50 +0200222static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
223 const struct gprs_ns_ie_ip4_elem *ip4)
224{
225 struct osmo_sockaddr sa;
226 /* copy over. Both data structures use network byte order */
227 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
228 sa.u.sin.sin_port = ip4->udp_port;
229 sa.u.sin.sin_family = AF_INET;
230
Alexander Couzens38b19e82020-09-23 23:56:37 +0200231 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200232}
233
234static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
235 const struct gprs_ns_ie_ip6_elem *ip6)
236{
237 struct osmo_sockaddr sa;
238 /* copy over. Both data structures use network byte order */
239 sa.u.sin6.sin6_addr = ip6->ip_addr;
240 sa.u.sin6.sin6_port = ip6->udp_port;
241 sa.u.sin6.sin6_family = AF_INET;
242
Alexander Couzens38b19e82020-09-23 23:56:37 +0200243 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200244}
245
Alexander Couzens125298f2020-10-11 21:22:42 +0200246/*! Return the initial SNS remote socket address
247 * \param nse NS Entity
248 * \return address of the initial SNS connection; NULL in case of error
249 */
250const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
251{
252 struct ns2_sns_state *gss;
253
254 if (!nse->bss_sns_fi)
255 return NULL;
256
257 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100258 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200259}
260
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100261/*! called when a nsvc is beeing freed or the nsvc became dead */
262void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200263{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100264 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200265 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100266 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200267 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200268
269 if (!fi)
270 return;
271
272 gss = (struct ns2_sns_state *) fi->priv;
273 if (nsvc != gss->sns_nsvc)
274 return;
275
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100276 gss->sns_nsvc = NULL;
277 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200278 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100279 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200280 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100281 return;
282 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200283 }
284 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100285 /* the SNS is waiting for its first NS-VC to come up
286 * choose any other nsvc */
287 llist_for_each_entry(tmp, &nse->nsvc, list) {
288 if (nsvc != tmp) {
289 gss->sns_nsvc = tmp;
290 return;
291 }
292 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200293 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100294
Alexander Couzens67725e22021-02-15 02:37:03 +0100295 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200296}
297
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200298static void ns2_clear_elems(struct ns2_sns_elems *elems)
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100299{
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200300 TALLOC_FREE(elems->ip4);
301 TALLOC_FREE(elems->ip6);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100302
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200303 elems->num_ip4 = 0;
304 elems->num_ip6 = 0;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100305}
306
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100307static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
308 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200309{
310 struct gprs_ns2_inst *nsi = nse->nsi;
311 struct gprs_ns2_vc *nsvc;
312 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100313
314 /* for every bind, create a connection if bind type == IP */
315 llist_for_each_entry(bind, &nsi->binding, list) {
316 if (bind->ll != GPRS_NS2_LL_UDP)
317 continue;
318 /* ignore failed connection */
319 nsvc = gprs_ns2_ip_connect_inactive(bind,
320 remote,
321 nse, 0);
322 if (!nsvc) {
323 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
324 continue;
325 }
326
327 nsvc->sig_weight = sig_weight;
328 nsvc->data_weight = data_weight;
329 }
330}
331
332static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
333 struct gprs_ns2_nse *nse,
334 const struct gprs_ns_ie_ip4_elem *ip4)
335{
Alexander Couzensc068d862020-10-12 04:11:51 +0200336 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200337 /* copy over. Both data structures use network byte order */
338 remote.u.sin.sin_family = AF_INET;
339 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
340 remote.u.sin.sin_port = ip4->udp_port;
341
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100342 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200343}
344
345static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
346 struct gprs_ns2_nse *nse,
347 const struct gprs_ns_ie_ip6_elem *ip6)
348{
Alexander Couzens6a161492020-07-12 13:45:50 +0200349 struct osmo_sockaddr remote = {};
350 /* copy over. Both data structures use network byte order */
351 remote.u.sin6.sin6_family = AF_INET6;
352 remote.u.sin6.sin6_addr = ip6->ip_addr;
353 remote.u.sin6.sin6_port = ip6->udp_port;
354
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100355 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200356}
357
Harald Weltee8c61062021-03-24 13:16:27 +0100358static struct gprs_ns2_vc *nsvc_for_bind_and_remote(struct gprs_ns2_nse *nse,
359 struct gprs_ns2_vc_bind *bind,
360 const struct osmo_sockaddr *remote)
361{
362 struct gprs_ns2_vc *nsvc;
363
364 llist_for_each_entry(nsvc, &nse->nsvc, list) {
365 if (nsvc->bind != bind)
366 continue;
367
368 if (!osmo_sockaddr_cmp(remote, gprs_ns2_ip_vc_remote(nsvc)))
369 return nsvc;
370 }
371 return NULL;
372}
Alexander Couzens6a161492020-07-12 13:45:50 +0200373
374static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
375{
376 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
377 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
378 struct gprs_ns2_vc *nsvc;
Harald Welte3053bbb2021-03-24 13:22:18 +0100379 struct ns2_sns_bind *sbind;
Alexander Couzens6a161492020-07-12 13:45:50 +0200380 struct osmo_sockaddr remote = { };
381 unsigned int i;
382
Harald Weltee8c61062021-03-24 13:16:27 +0100383 /* iterate over all remote IPv4 endpoints */
Alexander Couzens71128672021-06-05 18:44:01 +0200384 for (i = 0; i < gss->remote.num_ip4; i++) {
385 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->remote.ip4[i];
Alexander Couzens6a161492020-07-12 13:45:50 +0200386
387 remote.u.sin.sin_family = AF_INET;
388 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
389 remote.u.sin.sin_port = ip4->udp_port;
390
Harald Welte3053bbb2021-03-24 13:22:18 +0100391 /* iterate over all local binds within this SNS */
392 llist_for_each_entry(sbind, &gss->binds, list) {
393 struct gprs_ns2_vc_bind *bind = sbind->bind;
394
Harald Weltee8c61062021-03-24 13:16:27 +0100395 /* we only care about UDP binds */
Daniel Willmann967e2c12021-01-14 16:58:17 +0100396 if (bind->ll != GPRS_NS2_LL_UDP)
397 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200398
Harald Weltee8c61062021-03-24 13:16:27 +0100399 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
400 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200401 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
402 if (!nsvc) {
403 /* TODO: add to a list to send back a NS-STATUS */
404 continue;
405 }
406 }
407
408 /* update data / signalling weight */
409 nsvc->data_weight = ip4->data_weight;
410 nsvc->sig_weight = ip4->sig_weight;
411 nsvc->sns_only = false;
412 }
413 }
414
Harald Weltee8c61062021-03-24 13:16:27 +0100415 /* iterate over all remote IPv4 endpoints */
Alexander Couzens71128672021-06-05 18:44:01 +0200416 for (i = 0; i < gss->remote.num_ip6; i++) {
417 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->remote.ip6[i];
Alexander Couzens6a161492020-07-12 13:45:50 +0200418
419 remote.u.sin6.sin6_family = AF_INET6;
420 remote.u.sin6.sin6_addr = ip6->ip_addr;
421 remote.u.sin6.sin6_port = ip6->udp_port;
422
Harald Welte3053bbb2021-03-24 13:22:18 +0100423 /* iterate over all local binds within this SNS */
424 llist_for_each_entry(sbind, &gss->binds, list) {
425 struct gprs_ns2_vc_bind *bind = sbind->bind;
426
Daniel Willmann967e2c12021-01-14 16:58:17 +0100427 if (bind->ll != GPRS_NS2_LL_UDP)
428 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200429
Harald Weltee8c61062021-03-24 13:16:27 +0100430 /* we only care about UDP binds */
431 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
432 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200433 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
434 if (!nsvc) {
435 /* TODO: add to a list to send back a NS-STATUS */
436 continue;
437 }
438 }
439
440 /* update data / signalling weight */
441 nsvc->data_weight = ip6->data_weight;
442 nsvc->sig_weight = ip6->sig_weight;
443 nsvc->sns_only = false;
444 }
445 }
446
447
448 return 0;
449}
450
451/* Add a given remote IPv4 element to gprs_sns_state */
452static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
453{
454 unsigned int i;
455
Alexander Couzens71128672021-06-05 18:44:01 +0200456 if (gss->remote.num_ip4 >= gss->num_max_ip4_remote)
Alexander Couzens6a161492020-07-12 13:45:50 +0200457 return -NS_CAUSE_INVAL_NR_NS_VC;
458
459 /* check for duplicates */
Alexander Couzens71128672021-06-05 18:44:01 +0200460 for (i = 0; i < gss->remote.num_ip4; i++) {
461 if (memcmp(&gss->remote.ip4[i], ip4, sizeof(*ip4)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200462 continue;
463 /* TODO: log message duplicate */
Alexander Couzens6a161492020-07-12 13:45:50 +0200464 return -NS_CAUSE_PROTO_ERR_UNSPEC;
465 }
466
Alexander Couzens71128672021-06-05 18:44:01 +0200467 gss->remote.ip4 = talloc_realloc(gss, gss->remote.ip4, struct gprs_ns_ie_ip4_elem,
468 gss->remote.num_ip4+1);
469 gss->remote.ip4[gss->remote.num_ip4] = *ip4;
470 gss->remote.num_ip4 += 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200471 return 0;
472}
473
474/* Remove a given remote IPv4 element from gprs_sns_state */
475static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
476{
477 unsigned int i;
478
Alexander Couzens71128672021-06-05 18:44:01 +0200479 for (i = 0; i < gss->remote.num_ip4; i++) {
480 if (memcmp(&gss->remote.ip4[i], ip4, sizeof(*ip4)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200481 continue;
482 /* all array elements < i remain as they are; all > i are shifted left by one */
Alexander Couzens71128672021-06-05 18:44:01 +0200483 memmove(&gss->remote.ip4[i], &gss->remote.ip4[i+1], gss->remote.num_ip4-i-1);
484 gss->remote.num_ip4 -= 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200485 return 0;
486 }
487 return -1;
488}
489
490/* update the weights for specified remote IPv4 */
491static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
492{
493 unsigned int i;
494
Alexander Couzens71128672021-06-05 18:44:01 +0200495 for (i = 0; i < gss->remote.num_ip4; i++) {
496 if (gss->remote.ip4[i].ip_addr != ip4->ip_addr ||
497 gss->remote.ip4[i].udp_port != ip4->udp_port)
Alexander Couzens6a161492020-07-12 13:45:50 +0200498 continue;
499
Alexander Couzens71128672021-06-05 18:44:01 +0200500 gss->remote.ip4[i].sig_weight = ip4->sig_weight;
501 gss->remote.ip4[i].data_weight = ip4->data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200502 return 0;
503 }
504 return -1;
505}
506
507/* Add a given remote IPv6 element to gprs_sns_state */
508static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
509{
Alexander Couzens71128672021-06-05 18:44:01 +0200510 if (gss->remote.num_ip6 >= gss->num_max_ip6_remote)
Alexander Couzens6a161492020-07-12 13:45:50 +0200511 return -NS_CAUSE_INVAL_NR_NS_VC;
512
Alexander Couzens71128672021-06-05 18:44:01 +0200513 gss->remote.ip6 = talloc_realloc(gss, gss->remote.ip6, struct gprs_ns_ie_ip6_elem,
514 gss->remote.num_ip6+1);
515 gss->remote.ip6[gss->remote.num_ip6] = *ip6;
516 gss->remote.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 */
521static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
522{
523 unsigned int i;
524
Alexander Couzens71128672021-06-05 18:44:01 +0200525 for (i = 0; i < gss->remote.num_ip6; i++) {
526 if (memcmp(&gss->remote.ip6[i], ip6, sizeof(*ip6)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200527 continue;
528 /* all array elements < i remain as they are; all > i are shifted left by one */
Alexander Couzens71128672021-06-05 18:44:01 +0200529 memmove(&gss->remote.ip6[i], &gss->remote.ip6[i+1], gss->remote.num_ip6-i-1);
530 gss->remote.num_ip6 -= 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200531 return 0;
532 }
533 return -1;
534}
535
536/* update the weights for specified remote IPv6 */
537static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
538{
539 unsigned int i;
540
Alexander Couzens71128672021-06-05 18:44:01 +0200541 for (i = 0; i < gss->remote.num_ip6; i++) {
542 if (memcmp(&gss->remote.ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
543 gss->remote.ip6[i].udp_port != ip6->udp_port)
Alexander Couzens6a161492020-07-12 13:45:50 +0200544 continue;
Alexander Couzens71128672021-06-05 18:44:01 +0200545 gss->remote.ip6[i].sig_weight = ip6->sig_weight;
546 gss->remote.ip6[i].data_weight = ip6->data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200547 return 0;
548 }
549 return -1;
550}
551
552static 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)
553{
554 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
555 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
556 struct gprs_ns2_vc *nsvc;
557 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200558 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200559 uint8_t new_signal;
560 uint8_t new_data;
561
562 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
563 * signalling weights of all the peer IP endpoints configured for this NSE is
564 * equal to zero or if the resulting sum of the data weights of all the peer IP
565 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
566 * SNS-ACK PDU with a cause code of "Invalid weights". */
567
568 if (ip4) {
569 if (update_remote_ip4_elem(gss, ip4))
570 return -NS_CAUSE_UNKN_IP_EP;
571
572 /* copy over. Both data structures use network byte order */
573 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
574 sa.u.sin.sin_port = ip4->udp_port;
575 sa.u.sin.sin_family = AF_INET;
576 new_signal = ip4->sig_weight;
577 new_data = ip4->data_weight;
578 } else if (ip6) {
579 if (update_remote_ip6_elem(gss, ip6))
580 return -NS_CAUSE_UNKN_IP_EP;
581
582 /* copy over. Both data structures use network byte order */
583 sa.u.sin6.sin6_addr = ip6->ip_addr;
584 sa.u.sin6.sin6_port = ip6->udp_port;
585 sa.u.sin6.sin6_family = AF_INET6;
586 new_signal = ip6->sig_weight;
587 new_data = ip6->data_weight;
588 } else {
589 OSMO_ASSERT(false);
590 }
591
592 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200593 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200594 /* all nsvc in NSE should be IP/UDP nsvc */
595 OSMO_ASSERT(remote);
596
597 if (osmo_sockaddr_cmp(&sa, remote))
598 continue;
599
600 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
601 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
602 nsvc->sig_weight, new_signal);
603
604 nsvc->data_weight = new_data;
605 nsvc->sig_weight = new_signal;
606 }
607
608 return 0;
609}
610
611static int do_sns_delete(struct osmo_fsm_inst *fi,
612 const struct gprs_ns_ie_ip4_elem *ip4,
613 const struct gprs_ns_ie_ip6_elem *ip6)
614{
615 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
616 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
617 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200618 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200619 struct osmo_sockaddr sa = {};
620
621 if (ip4) {
622 if (remove_remote_ip4_elem(gss, ip4) < 0)
623 return -NS_CAUSE_UNKN_IP_EP;
624 /* copy over. Both data structures use network byte order */
625 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
626 sa.u.sin.sin_port = ip4->udp_port;
627 sa.u.sin.sin_family = AF_INET;
628 } else if (ip6) {
629 if (remove_remote_ip6_elem(gss, ip6))
630 return -NS_CAUSE_UNKN_IP_EP;
631
632 /* copy over. Both data structures use network byte order */
633 sa.u.sin6.sin6_addr = ip6->ip_addr;
634 sa.u.sin6.sin6_port = ip6->udp_port;
635 sa.u.sin6.sin6_family = AF_INET6;
636 } else {
637 OSMO_ASSERT(false);
638 }
639
640 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200641 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200642 /* all nsvc in NSE should be IP/UDP nsvc */
643 OSMO_ASSERT(remote);
644 if (osmo_sockaddr_cmp(&sa, remote))
645 continue;
646
647 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
648 gprs_ns2_free_nsvc(nsvc);
649 }
650
651 return 0;
652}
653
654static int do_sns_add(struct osmo_fsm_inst *fi,
655 const struct gprs_ns_ie_ip4_elem *ip4,
656 const struct gprs_ns_ie_ip6_elem *ip6)
657{
658 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
659 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
660 struct gprs_ns2_vc *nsvc;
661 int rc = 0;
662
663 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
664 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
665 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
666 switch (gss->ip) {
667 case IPv4:
668 rc = add_remote_ip4_elem(gss, ip4);
669 break;
670 case IPv6:
671 rc = add_remote_ip6_elem(gss, ip6);
672 break;
673 default:
674 /* the gss->ip is initialized with the bss */
675 OSMO_ASSERT(false);
676 }
677
678 if (rc)
679 return rc;
680
681 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
682 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
683 * unspecified" */
684 switch (gss->ip) {
685 case IPv4:
686 nsvc = nsvc_by_ip4_elem(nse, ip4);
687 if (nsvc) {
688 /* the nsvc should be already in sync with the ip4 / ip6 elements */
689 return -NS_CAUSE_PROTO_ERR_UNSPEC;
690 }
691
692 /* TODO: failure case */
693 ns2_nsvc_create_ip4(fi, nse, ip4);
694 break;
695 case IPv6:
696 nsvc = nsvc_by_ip6_elem(nse, ip6);
697 if (nsvc) {
698 /* the nsvc should be already in sync with the ip4 / ip6 elements */
699 return -NS_CAUSE_PROTO_ERR_UNSPEC;
700 }
701
702 /* TODO: failure case */
703 ns2_nsvc_create_ip6(fi, nse, ip6);
704 break;
705 }
706
707 gprs_ns2_start_alive_all_nsvcs(nse);
708
709 return 0;
710}
711
712
Harald Welte694dad52021-03-23 15:22:16 +0100713static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200714{
Harald Weltef61a9152021-03-02 22:20:17 +0100715 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
716 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzense769f522020-12-07 07:37:07 +0100717 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200718}
719
Harald Welte694dad52021-03-23 15:22:16 +0100720static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200721{
Harald Weltef61a9152021-03-02 22:20:17 +0100722 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200723 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
724 struct gprs_ns2_inst *nsi = nse->nsi;
725 struct tlv_parsed *tp = NULL;
726
Harald Weltef61a9152021-03-02 22:20:17 +0100727 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
728
Alexander Couzens6a161492020-07-12 13:45:50 +0200729 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100730 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200731 tp = data;
732 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
733 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
734 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
735 /* TODO: What to do? */
736 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100737 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200738 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
739 }
740 break;
741 default:
742 OSMO_ASSERT(0);
743 }
744}
745
Harald Welte01fa6a32021-03-04 19:49:38 +0100746static int ns2_sns_count_num_local_ep(struct osmo_fsm_inst *fi, enum ns2_sns_type stype)
747{
748 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
749 struct ns2_sns_bind *sbind;
750 int count = 0;
751
752 llist_for_each_entry(sbind, &gss->binds, list) {
753 const struct osmo_sockaddr *sa = gprs_ns2_ip_bind_sockaddr(sbind->bind);
754 if (!sa)
755 continue;
756
757 switch (stype) {
758 case IPv4:
759 if (sa->u.sas.ss_family == AF_INET)
760 count++;
761 break;
762 case IPv6:
763 if (sa->u.sas.ss_family == AF_INET6)
764 count++;
765 break;
766 }
767 }
768 return count;
769}
770
Harald Welte24920e22021-03-04 13:03:27 +0100771static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200772{
773 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100774 struct gprs_ns_ie_ip4_elem *ip4_elems;
775 struct gprs_ns_ie_ip6_elem *ip6_elems;
776 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100777 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100778 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100779 const struct osmo_sockaddr *sa;
780 struct osmo_sockaddr local;
781 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200782
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200783 ns2_clear_elems(&gss->local);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100784
Alexander Couzense769f522020-12-07 07:37:07 +0100785 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100786 if (gss->role == GPRS_SNS_ROLE_BSS) {
787 if (!gss->initial)
788 return;
789 remote = &gss->initial->saddr;
790 } else
791 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100792
793 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100794 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100795 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100796 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100797 return;
798 }
799
Alexander Couzense769f522020-12-07 07:37:07 +0100800 switch (gss->ip) {
801 case IPv4:
Alexander Couzens71128672021-06-05 18:44:01 +0200802 ip4_elems = talloc_realloc(fi, gss->local.ip4, struct gprs_ns_ie_ip4_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100803 if (!ip4_elems)
804 return;
805
Alexander Couzens71128672021-06-05 18:44:01 +0200806 gss->local.ip4 = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100807 llist_for_each_entry(sbind, &gss->binds, list) {
808 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100809 sa = gprs_ns2_ip_bind_sockaddr(bind);
810 if (!sa)
811 continue;
812
813 if (sa->u.sas.ss_family != AF_INET)
814 continue;
815
816 /* check if this is an specific bind */
817 if (sa->u.sin.sin_addr.s_addr == 0) {
818 if (osmo_sockaddr_local_ip(&local, remote))
819 continue;
820
821 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
822 } else {
823 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
824 }
825
826 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100827 ip4_elems->sig_weight = bind->sns_sig_weight;
828 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100829 ip4_elems++;
830 }
831
Alexander Couzens71128672021-06-05 18:44:01 +0200832 gss->local.num_ip4 = count;
833 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->local.num_ip4, 8);
Alexander Couzense769f522020-12-07 07:37:07 +0100834 break;
835 case IPv6:
836 /* IPv6 */
Alexander Couzens71128672021-06-05 18:44:01 +0200837 ip6_elems = talloc_realloc(fi, gss->local.ip6, struct gprs_ns_ie_ip6_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100838 if (!ip6_elems)
839 return;
840
Alexander Couzens71128672021-06-05 18:44:01 +0200841 gss->local.ip6 = ip6_elems;
Alexander Couzense769f522020-12-07 07:37:07 +0100842
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100843 llist_for_each_entry(sbind, &gss->binds, list) {
844 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100845 sa = gprs_ns2_ip_bind_sockaddr(bind);
846 if (!sa)
847 continue;
848
849 if (sa->u.sas.ss_family != AF_INET6)
850 continue;
851
852 /* check if this is an specific bind */
853 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
854 if (osmo_sockaddr_local_ip(&local, remote))
855 continue;
856
857 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
858 } else {
859 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
860 }
861
862 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100863 ip6_elems->sig_weight = bind->sns_sig_weight;
864 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100865
866 ip6_elems++;
867 }
Alexander Couzens71128672021-06-05 18:44:01 +0200868 gss->local.num_ip6 = count;
869 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->local.num_ip6, 8);
Alexander Couzense769f522020-12-07 07:37:07 +0100870 break;
871 }
Harald Welte24920e22021-03-04 13:03:27 +0100872}
873
Alexander Couzens6608ce92021-04-26 20:39:46 +0200874static void ns2_sns_choose_next_bind(struct ns2_sns_state *gss)
875{
876 /* take the first bind or take the next bind */
877 if (!gss->initial_bind || gss->initial_bind->list.next == &gss->binds)
878 gss->initial_bind = llist_first_entry_or_null(&gss->binds, struct ns2_sns_bind, list);
879 else
880 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
881}
882
Harald Welte24920e22021-03-04 13:03:27 +0100883/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Harald Welte694dad52021-03-23 15:22:16 +0100884static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Harald Welte24920e22021-03-04 13:03:27 +0100885{
886 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
887
Harald Weltef61a9152021-03-02 22:20:17 +0100888 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
889
Harald Welte24920e22021-03-04 13:03:27 +0100890 /* on a generic failure, the timer callback will recover */
891 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
892 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Harald Welte694dad52021-03-23 15:22:16 +0100893 if (old_state != GPRS_SNS_ST_BSS_SIZE)
Harald Welte24920e22021-03-04 13:03:27 +0100894 gss->N = 0;
895
896 gss->alive = false;
897
898 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens6608ce92021-04-26 20:39:46 +0200899 ns2_sns_choose_next_bind(gss);
Harald Welte24920e22021-03-04 13:03:27 +0100900
901 /* setup the NSVC */
902 if (!gss->sns_nsvc) {
903 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
904 struct osmo_sockaddr *remote = &gss->initial->saddr;
905 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
906 if (!gss->sns_nsvc)
907 return;
Harald Weltec962a2e2021-03-05 08:09:08 +0100908 /* A pre-configured endpoint shall not be used for NSE data or signalling traffic
909 * (with the exception of Size and Configuration procedures) unless it is configured
910 * by the SGSN using the auto-configuration procedures */
Harald Welte24920e22021-03-04 13:03:27 +0100911 gss->sns_nsvc->sns_only = true;
912 }
913
Alexander Couzens6a161492020-07-12 13:45:50 +0200914 if (gss->num_max_ip4_remote > 0)
Alexander Couzens71128672021-06-05 18:44:01 +0200915 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->local.num_ip4, -1);
Alexander Couzens6a161492020-07-12 13:45:50 +0200916 else
Alexander Couzens71128672021-06-05 18:44:01 +0200917 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->local.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200918}
919
Harald Welte694dad52021-03-23 15:22:16 +0100920static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200921{
Harald Weltef61a9152021-03-02 22:20:17 +0100922 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens3df58862021-02-05 17:18:08 +0100923 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Harald Weltef61a9152021-03-02 22:20:17 +0100924 struct tlv_parsed *tp = NULL;
925
926 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzens6a161492020-07-12 13:45:50 +0200927
928 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100929 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200930 tp = (struct tlv_parsed *) data;
931 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
932 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
933 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
934 /* TODO: What to do? */
935 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100936 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 +0200937 }
938 break;
939 default:
940 OSMO_ASSERT(0);
941 }
942}
943
Harald Welte694dad52021-03-23 15:22:16 +0100944static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200945{
946 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100947
Harald Weltef61a9152021-03-02 22:20:17 +0100948 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
949
Harald Welte694dad52021-03-23 15:22:16 +0100950 if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS)
Alexander Couzens790a9632021-02-05 17:18:39 +0100951 gss->N = 0;
952
Alexander Couzens6a161492020-07-12 13:45:50 +0200953 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200954 switch (gss->ip) {
955 case IPv4:
956 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzens71128672021-06-05 18:44:01 +0200957 gss->local.ip4, gss->local.num_ip4,
Alexander Couzense78207f2020-12-07 06:19:29 +0100958 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200959 break;
960 case IPv6:
961 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100962 NULL, 0,
Alexander Couzens71128672021-06-05 18:44:01 +0200963 gss->local.ip6, gss->local.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200964 break;
965 }
966}
967
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100968/* calculate the timeout of the configured state. the configured
969 * state will fail if not at least one NS-VC is alive within X second.
970 */
971static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
972{
973 int secs;
974 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
975 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
976 secs += nsi->timeout[NS_TOUT_TNS_TEST];
977
978 return secs;
979}
Alexander Couzens6a161492020-07-12 13:45:50 +0200980
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100981/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
982static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200983{
984 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200985
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100986 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
987 const struct gprs_ns_ie_ip4_elem *v4_list;
988 unsigned int num_v4;
989 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
990 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +0200991
Alexander Couzens71128672021-06-05 18:44:01 +0200992 if (num_v4 && gss->remote.ip6)
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100993 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +0200994
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100995 /* realloc to the new size */
Alexander Couzens71128672021-06-05 18:44:01 +0200996 gss->remote.ip4 = talloc_realloc(gss, gss->remote.ip4,
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100997 struct gprs_ns_ie_ip4_elem,
Alexander Couzens71128672021-06-05 18:44:01 +0200998 gss->remote.num_ip4 + num_v4);
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100999 /* append the new entries to the end of the list */
Alexander Couzens71128672021-06-05 18:44:01 +02001000 memcpy(&gss->remote.ip4[gss->remote.num_ip4], v4_list, num_v4*sizeof(*v4_list));
1001 gss->remote.num_ip4 += num_v4;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001002
1003 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
Alexander Couzens71128672021-06-05 18:44:01 +02001004 gss->remote.num_ip4);
Alexander Couzens6a161492020-07-12 13:45:50 +02001005 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001006
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001007 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1008 const struct gprs_ns_ie_ip6_elem *v6_list;
1009 unsigned int num_v6;
1010 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1011 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
1012
Alexander Couzens71128672021-06-05 18:44:01 +02001013 if (num_v6 && gss->remote.ip4)
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001014 return -NS_CAUSE_INVAL_NR_IPv6_EP;
1015
1016 /* realloc to the new size */
Alexander Couzens71128672021-06-05 18:44:01 +02001017 gss->remote.ip6 = talloc_realloc(gss, gss->remote.ip6,
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001018 struct gprs_ns_ie_ip6_elem,
Alexander Couzens71128672021-06-05 18:44:01 +02001019 gss->remote.num_ip6 + num_v6);
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001020 /* append the new entries to the end of the list */
Alexander Couzens71128672021-06-05 18:44:01 +02001021 memcpy(&gss->remote.ip6[gss->remote.num_ip6], v6_list, num_v6*sizeof(*v6_list));
1022 gss->remote.num_ip6 += num_v6;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001023
Alexander Couzens71128672021-06-05 18:44:01 +02001024 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %d entries\n",
1025 gss->remote.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +02001026 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001027
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001028 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001029}
1030
Harald Welte694dad52021-03-23 15:22:16 +01001031static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens790a9632021-02-05 17:18:39 +01001032{
1033 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1034
Harald Weltef61a9152021-03-02 22:20:17 +01001035 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1036
Harald Welte694dad52021-03-23 15:22:16 +01001037 if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN)
Alexander Couzens790a9632021-02-05 17:18:39 +01001038 gss->N = 0;
1039}
1040
Harald Welte694dad52021-03-23 15:22:16 +01001041static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +02001042{
1043 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001044 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1045 uint8_t cause;
1046 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001047
Harald Weltef61a9152021-03-02 22:20:17 +01001048 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1049
Alexander Couzens6a161492020-07-12 13:45:50 +02001050 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001051 case GPRS_SNS_EV_RX_CONFIG_END:
1052 case GPRS_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001053 rc = ns_sns_append_remote_eps(fi, data);
1054 if (rc < 0) {
1055 cause = -rc;
1056 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1057 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1058 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001059 }
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001060 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1061 /* check if sum of data / sig weights == 0 */
1062 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1063 cause = NS_CAUSE_INVAL_WEIGH;
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;
1067 }
1068 create_missing_nsvcs(fi);
1069 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1070 /* start the test procedure on ALL NSVCs! */
1071 gprs_ns2_start_alive_all_nsvcs(nse);
1072 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1073 } else {
1074 /* just send CONFIG-ACK */
1075 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1076 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001077 }
1078 break;
1079 default:
1080 OSMO_ASSERT(0);
1081 }
1082}
1083
Alexander Couzens67725e22021-02-15 02:37:03 +01001084/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001085static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1086 struct ns2_sns_state *gss,
1087 struct tlv_parsed *tp)
1088{
1089 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1090 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1091 int num_v4 = 0, num_v6 = 0;
1092 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001093 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001094 int rc = 0;
1095
1096 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1097 * check uniqueness within the lists (no doublicate entries)
1098 * check not-known-by-us and sent back a list of unknown/known values
1099 * (abnormal behaviour according to 48.016)
1100 */
1101
1102 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1103 if (gss->ip == IPv4) {
1104 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1105 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1106 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1107 return;
1108 }
1109
1110 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1111 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001112 for (i = 0; i < num_v4; i++) {
1113 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001114 rc = do_sns_add(fi, &v4_list[i], NULL);
1115 if (rc < 0) {
1116 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001117 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001118 do_sns_delete(fi, &v4_list[j], NULL);
1119 cause = -rc;
1120 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1121 break;
1122 }
1123 }
1124 } else { /* IPv6 */
1125 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1126 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1127 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1128 return;
1129 }
1130
1131 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1132 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001133 for (i = 0; i < num_v6; i++) {
1134 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001135 rc = do_sns_add(fi, NULL, &v6_list[i]);
1136 if (rc < 0) {
1137 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001138 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001139 do_sns_delete(fi, NULL, &v6_list[j]);
1140 cause = -rc;
1141 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1142 break;
1143 }
1144 }
1145 }
1146
1147 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1148 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1149}
1150
1151static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1152 struct ns2_sns_state *gss,
1153 struct tlv_parsed *tp)
1154{
1155 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1156 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1157 int num_v4 = 0, num_v6 = 0;
1158 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001159 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001160 int rc = 0;
1161
1162 /* TODO: split up delete into v4 + v6
1163 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1164 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1165 */
1166 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1167 if (gss->ip == IPv4) {
1168 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1169 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1170 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001171 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001172 rc = do_sns_delete(fi, &v4_list[i], NULL);
1173 if (rc < 0) {
1174 cause = -rc;
1175 /* continue to delete others */
1176 }
1177 }
1178 if (cause != 0xff) {
1179 /* TODO: create list of not-deleted and return it */
1180 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1181 return;
1182 }
1183
1184 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1185 /* delete all NS-VCs for given IPv4 address */
1186 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1187 struct gprs_ns_ie_ip4_elem *ip4_remote;
1188 uint32_t ip_addr = *(uint32_t *)(ie+1);
1189 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1190 cause = NS_CAUSE_UNKN_IP_ADDR;
1191 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1192 return;
1193 }
1194 /* make a copy as do_sns_delete() will change the array underneath us */
Alexander Couzens71128672021-06-05 18:44:01 +02001195 ip4_remote = talloc_memdup(fi, gss->remote.ip4,
1196 gss->remote.num_ip4 * sizeof(*v4_list));
1197 for (i = 0; i < gss->remote.num_ip4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001198 if (ip4_remote[i].ip_addr == ip_addr) {
1199 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1200 if (rc < 0) {
1201 cause = -rc;
1202 /* continue to delete others */
1203 }
1204 }
1205 }
1206 talloc_free(ip4_remote);
1207 if (cause != 0xff) {
1208 /* TODO: create list of not-deleted and return it */
1209 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1210 return;
1211 }
1212 } else {
1213 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1214 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1215 return;
1216 }
1217 } else { /* IPv6 */
1218 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1219 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1220 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001221 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001222 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1223 if (rc < 0) {
1224 cause = -rc;
1225 /* continue to delete others */
1226 }
1227 }
1228 if (cause != 0xff) {
1229 /* TODO: create list of not-deleted and return it */
1230 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1231 return;
1232 }
1233 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1234 /* delete all NS-VCs for given IPv4 address */
1235 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1236 struct gprs_ns_ie_ip6_elem *ip6_remote;
1237 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001238 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001239 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1240 cause = NS_CAUSE_UNKN_IP_ADDR;
1241 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1242 return;
1243 }
1244 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1245 /* make a copy as do_sns_delete() will change the array underneath us */
Alexander Couzens71128672021-06-05 18:44:01 +02001246 ip6_remote = talloc_memdup(fi, gss->remote.ip6,
1247 gss->remote.num_ip6 * sizeof(*v4_list));
1248 for (i = 0; i < gss->remote.num_ip6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001249 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1250 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1251 if (rc < 0) {
1252 cause = -rc;
1253 /* continue to delete others */
1254 }
1255 }
1256 }
1257
1258 talloc_free(ip6_remote);
1259 if (cause != 0xff) {
1260 /* TODO: create list of not-deleted and return it */
1261 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1262 return;
1263 }
1264 } else {
1265 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1266 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1267 return;
1268 }
1269 }
1270 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1271}
1272
1273static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1274 struct ns2_sns_state *gss,
1275 struct tlv_parsed *tp)
1276{
1277 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1278 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1279 int num_v4 = 0, num_v6 = 0;
1280 uint8_t trans_id, cause = 0xff;
1281 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001282 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001283
1284 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1285 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1286 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1287 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001288 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001289 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1290 if (rc < 0) {
1291 cause = -rc;
1292 /* continue to others */
1293 }
1294 }
1295 if (cause != 0xff) {
1296 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1297 return;
1298 }
1299 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1300 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1301 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001302 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001303 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1304 if (rc < 0) {
1305 cause = -rc;
1306 /* continue to others */
1307 }
1308 }
1309 if (cause != 0xff) {
1310 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1311 return;
1312 }
1313 } else {
1314 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1315 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1316 return;
1317 }
1318 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1319}
1320
1321static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1322{
1323 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1324 struct tlv_parsed *tp = data;
1325
1326 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001327 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001328 ns2_sns_st_configured_add(fi, gss, tp);
1329 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001330 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001331 ns2_sns_st_configured_delete(fi, gss, tp);
1332 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001333 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001334 ns2_sns_st_configured_change(fi, gss, tp);
1335 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001336 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001337 osmo_timer_del(&fi->timer);
1338 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001339 }
1340}
1341
1342static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1343{
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001344 struct gprs_ns2_vc *nsvc;
1345 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001346 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001347 /* NS-VC status updates are only parsed in ST_CONFIGURED.
1348 * Do an initial check if there are any nsvc alive atm */
1349 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1350 if (ns2_vc_is_unblocked(nsvc)) {
1351 gss->alive = true;
1352 osmo_timer_del(&fi->timer);
1353 break;
1354 }
1355 }
1356
Alexander Couzens53e70092021-04-06 15:45:47 +02001357 /* remove the initial NSVC if the NSVC isn't part of the configuration */
1358 if (gss->sns_nsvc->sns_only)
1359 gprs_ns2_free_nsvc(gss->sns_nsvc);
1360
Alexander Couzens138b96f2021-01-25 16:23:29 +01001361 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001362}
1363
1364static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1365 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001366 .in_event_mask = 0, /* handled by all_state_action */
Alexander Couzens0a7c5ee2021-04-10 18:20:21 +02001367 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1368 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001369 .name = "UNCONFIGURED",
Harald Welte694dad52021-03-23 15:22:16 +01001370 .action = ns2_sns_st_bss_unconfigured,
Alexander Couzens6a161492020-07-12 13:45:50 +02001371 },
Harald Welte694dad52021-03-23 15:22:16 +01001372 [GPRS_SNS_ST_BSS_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001373 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001374 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001375 S(GPRS_SNS_ST_BSS_SIZE) |
1376 S(GPRS_SNS_ST_BSS_CONFIG_BSS),
1377 .name = "BSS_SIZE",
1378 .action = ns2_sns_st_bss_size,
1379 .onenter = ns2_sns_st_bss_size_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001380 },
Harald Welte694dad52021-03-23 15:22:16 +01001381 [GPRS_SNS_ST_BSS_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001382 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001383 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001384 S(GPRS_SNS_ST_BSS_CONFIG_BSS) |
1385 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
1386 S(GPRS_SNS_ST_BSS_SIZE),
1387 .name = "BSS_CONFIG_BSS",
1388 .action = ns2_sns_st_bss_config_bss,
1389 .onenter = ns2_sns_st_bss_config_bss_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001390 },
Harald Welte694dad52021-03-23 15:22:16 +01001391 [GPRS_SNS_ST_BSS_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001392 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1393 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001394 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001395 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens6a161492020-07-12 13:45:50 +02001396 S(GPRS_SNS_ST_CONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001397 S(GPRS_SNS_ST_BSS_SIZE),
1398 .name = "BSS_CONFIG_SGSN",
1399 .action = ns2_sns_st_bss_config_sgsn,
1400 .onenter = ns2_sns_st_bss_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001401 },
1402 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001403 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1404 S(GPRS_SNS_EV_RX_DELETE) |
1405 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1406 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001407 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001408 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001409 .name = "CONFIGURED",
1410 .action = ns2_sns_st_configured,
1411 .onenter = ns2_sns_st_configured_onenter,
1412 },
1413};
1414
1415static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1416{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001417 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001418 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1419 struct gprs_ns2_inst *nsi = nse->nsi;
1420
Alexander Couzens90ee9632020-12-07 06:18:32 +01001421 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001422 switch (fi->T) {
1423 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001424 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1425 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001426 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001427 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001428 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 +01001429 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001430 break;
1431 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001432 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001433 LOGPFSML(fi, LOGL_ERROR, "NSE %d: BSS Config retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001434 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001435 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001436 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 +01001437 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001438 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001439 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001440 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1441 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001442 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001443 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001444 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 +01001445 }
1446 break;
1447 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001448 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001449 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001450 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001451 }
1452 return 0;
1453}
1454
Harald Welte9e37bf42021-03-02 20:48:31 +01001455/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001456static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1457{
1458 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001459 struct ns2_sns_bind *sbind;
1460 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001461
Alexander Couzense769f522020-12-07 07:37:07 +01001462 switch (event) {
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001463 case GPRS_SNS_EV_REQ_ADD_BIND:
1464 sbind = data;
1465 switch (fi->state) {
1466 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001467 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001468 break;
Harald Welte694dad52021-03-23 15:22:16 +01001469 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001470 /* TODO: add the ip4 element to the list */
1471 break;
Harald Welte694dad52021-03-23 15:22:16 +01001472 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1473 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001474 case GPRS_SNS_ST_CONFIGURED:
1475 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1476 break;
1477 }
1478 break;
1479 case GPRS_SNS_EV_REQ_DELETE_BIND:
1480 sbind = data;
1481 switch (fi->state) {
1482 case GPRS_SNS_ST_UNCONFIGURED:
1483 break;
Harald Welte694dad52021-03-23 15:22:16 +01001484 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001485 /* TODO: remove the ip4 element from the list */
1486 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1487 if (nsvc->bind == sbind->bind) {
1488 gprs_ns2_free_nsvc(nsvc);
1489 }
1490 }
1491 break;
Harald Welte694dad52021-03-23 15:22:16 +01001492 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1493 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001494 case GPRS_SNS_ST_CONFIGURED:
1495 /* TODO: do an delete SNS-IP procedure */
1496 /* TODO: remove the ip4 element to the list */
1497 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1498 if (nsvc->bind == sbind->bind) {
1499 gprs_ns2_free_nsvc(nsvc);
1500 }
1501 }
1502 break;
1503 }
1504 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1505 talloc_free(sbind);
1506 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001507 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001508}
1509
Alexander Couzens31d52e12021-06-05 20:04:04 +02001510/* validate the bss configuration (sns endpoint and binds)
1511 * - no endpoints -> invalid
1512 * - no binds -> invalid
1513 * - only v4 sns endpoints, only v6 binds -> invalid
1514 * - only v4 sns endpoints, but v4 sig weights == 0 -> invalid ...
1515 */
1516static int ns2_sns_bss_valid_configuration(struct ns2_sns_state *gss)
1517{
1518 struct ns2_sns_bind *sbind;
1519 struct sns_endpoint *endpoint;
1520 const struct osmo_sockaddr *addr;
1521 int v4_sig = 0, v4_data = 0, v6_sig = 0, v6_data = 0;
1522 bool v4_endpoints = false;
1523 bool v6_endpoints = false;
1524
1525 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds))
1526 return 0;
1527
1528 llist_for_each_entry(sbind, &gss->binds, list) {
1529 addr = gprs_ns2_ip_bind_sockaddr(sbind->bind);
1530 if (!addr)
1531 continue;
1532 switch (addr->u.sa.sa_family) {
1533 case AF_INET:
1534 v4_sig += sbind->bind->sns_sig_weight;
1535 v4_data += sbind->bind->sns_data_weight;
1536 break;
1537 case AF_INET6:
1538 v6_sig += sbind->bind->sns_sig_weight;
1539 v6_data += sbind->bind->sns_data_weight;
1540 break;
1541 }
1542 }
1543
1544 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
1545 switch (endpoint->saddr.u.sa.sa_family) {
1546 case AF_INET:
1547 v4_endpoints = true;
1548 break;
1549 case AF_INET6:
1550 v6_endpoints = true;
1551 break;
1552 }
1553 }
1554
1555 return (v4_endpoints && v4_sig && v4_data) || (v6_endpoints && v6_sig && v6_data);
1556}
1557
Harald Welte9e37bf42021-03-02 20:48:31 +01001558/* allstate-action for BSS role */
1559static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1560{
1561 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1562 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1563
1564 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
1565 switch (event) {
1566 case GPRS_SNS_EV_REQ_NO_NSVC:
1567 /* ignore reselection running */
1568 if (gss->reselection_running)
1569 break;
1570
1571 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1572 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1573 break;
1574 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
1575 /* tear down previous state
1576 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1577 gss->reselection_running = true;
1578 gprs_ns2_free_nsvcs(nse);
Alexander Couzensd2c6c492021-06-06 01:57:52 +02001579 ns2_clear_elems(&gss->local);
1580 ns2_clear_elems(&gss->remote);
Harald Welte9e37bf42021-03-02 20:48:31 +01001581
1582 /* Choose the next sns endpoint. */
Alexander Couzens31d52e12021-06-05 20:04:04 +02001583 if (!ns2_sns_bss_valid_configuration(gss)) {
Harald Welte9e37bf42021-03-02 20:48:31 +01001584 gss->initial = NULL;
1585 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1586 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1587 return;
1588 } else if (!gss->initial) {
1589 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1590 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1591 /* last entry, continue with first */
1592 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1593 } else {
1594 /* next element is an entry */
1595 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1596 }
1597
1598 gss->reselection_running = false;
Harald Welte694dad52021-03-23 15:22:16 +01001599 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 +01001600 break;
1601 default:
1602 ns2_sns_st_all_action(fi, event, data);
1603 break;
1604 }
1605}
1606
Alexander Couzens6a161492020-07-12 13:45:50 +02001607static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1608 .name = "GPRS-NS2-SNS-BSS",
1609 .states = ns2_sns_bss_states,
1610 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001611 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1612 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001613 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1614 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001615 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001616 .cleanup = NULL,
1617 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001618 .event_names = gprs_sns_event_names,
1619 .pre_term = NULL,
1620 .log_subsys = DLNS,
1621};
1622
Harald Welte5bef2cc2020-09-18 22:33:24 +02001623/*! Allocate an IP-SNS FSM for the BSS side.
1624 * \param[in] nse NS Entity in which the FSM runs
1625 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001626 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001627struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1628 const char *id)
1629{
1630 struct osmo_fsm_inst *fi;
1631 struct ns2_sns_state *gss;
1632
1633 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1634 if (!fi)
1635 return fi;
1636
1637 gss = talloc_zero(fi, struct ns2_sns_state);
1638 if (!gss)
1639 goto err;
1640
1641 fi->priv = gss;
1642 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001643 gss->role = GPRS_SNS_ROLE_BSS;
Harald Welte24f4df52021-03-04 18:02:54 +01001644 /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */
1645 gss->num_max_ip4_remote = 8192;
1646 gss->num_max_ip6_remote = 8192;
Alexander Couzense769f522020-12-07 07:37:07 +01001647 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001648 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001649
1650 return fi;
1651err:
1652 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1653 return NULL;
1654}
1655
Harald Welte5bef2cc2020-09-18 22:33:24 +02001656/*! main entry point for receiving SNS messages from the network.
1657 * \param[in] nsvc NS-VC on which the message was received
1658 * \param[in] msg message buffer of the IP-SNS message
1659 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001660 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001661int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001662{
1663 struct gprs_ns2_nse *nse = nsvc->nse;
1664 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1665 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001666 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001667 struct osmo_fsm_inst *fi;
Alexander Couzens7619ed42021-03-24 17:44:03 +01001668 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001669
1670 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001671 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1672 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001673 rc = -EINVAL;
1674 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +02001675 }
1676
Alexander Couzens6a161492020-07-12 13:45:50 +02001677 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1678 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001679 gss = (struct ns2_sns_state *) fi->priv;
1680 if (!gss->sns_nsvc)
1681 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001682
Harald Weltef2949742021-01-20 14:54:14 +01001683 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1684 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1685
Alexander Couzens6a161492020-07-12 13:45:50 +02001686 switch (nsh->pdu_type) {
1687 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001688 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001689 break;
1690 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001691 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001692 break;
1693 case SNS_PDUT_CONFIG:
1694 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001695 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001696 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001697 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001698 break;
1699 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001700 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001701 break;
1702 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001703 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001704 break;
1705 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001706 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001707 break;
1708 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001709 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001710 break;
1711 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001712 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001713 break;
1714 default:
Harald Weltef2949742021-01-20 14:54:14 +01001715 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1716 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001717 rc = -EINVAL;
Alexander Couzens6a161492020-07-12 13:45:50 +02001718 }
1719
Alexander Couzens7619ed42021-03-24 17:44:03 +01001720out:
1721 msgb_free(msg);
1722
1723 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001724}
1725
1726#include <osmocom/vty/vty.h>
1727#include <osmocom/vty/misc.h>
1728
Harald Welte1262c4f2021-01-19 20:58:33 +01001729static 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 +02001730{
1731 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001732 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001733 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1734}
1735
Harald Welte1262c4f2021-01-19 20:58:33 +01001736static 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 +02001737{
1738 char ip_addr[INET6_ADDRSTRLEN] = {};
1739 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1740 strcpy(ip_addr, "Invalid IPv6");
1741
Harald Welte1262c4f2021-01-19 20:58:33 +01001742 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001743 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1744}
1745
Harald Welte5bef2cc2020-09-18 22:33:24 +02001746/*! Dump the IP-SNS state to a vty.
1747 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001748 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001749 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1750 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001751void 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 +02001752{
1753 struct ns2_sns_state *gss;
1754 unsigned int i;
1755
1756 if (!nse->bss_sns_fi)
1757 return;
1758
Harald Welte1262c4f2021-01-19 20:58:33 +01001759 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001760 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1761
Harald Welte1262c4f2021-01-19 20:58:33 +01001762 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1763 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001764
Alexander Couzens71128672021-06-05 18:44:01 +02001765 if (gss->local.num_ip4 && gss->remote.num_ip4) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001766 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001767 for (i = 0; i < gss->local.num_ip4; i++)
1768 vty_dump_sns_ip4(vty, prefix, &gss->local.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001769
Harald Welte1262c4f2021-01-19 20:58:33 +01001770 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001771 for (i = 0; i < gss->remote.num_ip4; i++)
1772 vty_dump_sns_ip4(vty, prefix, &gss->remote.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001773 }
1774
Alexander Couzens71128672021-06-05 18:44:01 +02001775 if (gss->local.num_ip6 && gss->remote.num_ip6) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001776 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001777 for (i = 0; i < gss->local.num_ip6; i++)
1778 vty_dump_sns_ip6(vty, prefix, &gss->local.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001779
Harald Welte1262c4f2021-01-19 20:58:33 +01001780 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001781 for (i = 0; i < gss->remote.num_ip6; i++)
1782 vty_dump_sns_ip6(vty, prefix, &gss->remote.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001783 }
1784}
1785
Alexander Couzens412bc342020-11-19 05:24:37 +01001786/*! write IP-SNS to a vty
1787 * \param[in] vty VTY to which the state shall be printed
1788 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001789void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001790{
1791 struct ns2_sns_state *gss;
1792 struct osmo_sockaddr_str addr_str;
1793 struct sns_endpoint *endpoint;
1794
1795 if (!nse->bss_sns_fi)
1796 return;
1797
1798 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1799 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001800 /* It's unlikely that an error happens, but let's better be safe. */
1801 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1802 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001803 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001804 }
1805}
1806
Alexander Couzense769f522020-12-07 07:37:07 +01001807static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1808 const struct osmo_sockaddr *saddr)
1809{
1810 struct sns_endpoint *endpoint;
1811
1812 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1813 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1814 return endpoint;
1815 }
1816
1817 return NULL;
1818}
1819
1820/*! gprs_ns2_sns_add_endpoint
1821 * \param[in] nse
1822 * \param[in] sockaddr
1823 * \return
1824 */
1825int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1826 const struct osmo_sockaddr *saddr)
1827{
1828 struct ns2_sns_state *gss;
1829 struct sns_endpoint *endpoint;
1830 bool do_selection = false;
1831
1832 if (nse->ll != GPRS_NS2_LL_UDP) {
1833 return -EINVAL;
1834 }
1835
Alexander Couzens138b96f2021-01-25 16:23:29 +01001836 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001837 return -EINVAL;
1838 }
1839
1840 gss = nse->bss_sns_fi->priv;
1841
1842 if (ns2_get_sns_endpoint(gss, saddr))
1843 return -EADDRINUSE;
1844
1845 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1846 if (!endpoint)
1847 return -ENOMEM;
1848
1849 endpoint->saddr = *saddr;
1850 if (llist_empty(&gss->sns_endpoints))
1851 do_selection = true;
1852
1853 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1854 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001855 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001856
1857 return 0;
1858}
1859
1860/*! gprs_ns2_sns_del_endpoint
1861 * \param[in] nse
1862 * \param[in] sockaddr
1863 * \return 0 on success, otherwise < 0
1864 */
1865int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1866 const struct osmo_sockaddr *saddr)
1867{
1868 struct ns2_sns_state *gss;
1869 struct sns_endpoint *endpoint;
1870
1871 if (nse->ll != GPRS_NS2_LL_UDP) {
1872 return -EINVAL;
1873 }
1874
Alexander Couzens138b96f2021-01-25 16:23:29 +01001875 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001876 return -EINVAL;
1877 }
1878
1879 gss = nse->bss_sns_fi->priv;
1880 endpoint = ns2_get_sns_endpoint(gss, saddr);
1881 if (!endpoint)
1882 return -ENOENT;
1883
1884 /* if this is an unused SNS endpoint it's done */
1885 if (gss->initial != endpoint) {
1886 llist_del(&endpoint->list);
1887 talloc_free(endpoint);
1888 return 0;
1889 }
1890
Alexander Couzens67725e22021-02-15 02:37:03 +01001891 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001892 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001893 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001894 "Closing all NS-VC and restart SNS-SIZE procedure"
1895 "with a remaining SNS endpoint.\n");
1896
1897 /* Continue with the next endpoint in the list.
1898 * Special case if the endpoint is at the start or end of the list */
1899 if (endpoint->list.prev == &gss->sns_endpoints ||
1900 endpoint->list.next == &gss->sns_endpoints)
1901 gss->initial = NULL;
1902 else
1903 gss->initial = llist_entry(endpoint->list.next->prev,
1904 struct sns_endpoint,
1905 list);
1906
1907 llist_del(&endpoint->list);
1908 gprs_ns2_free_nsvcs(nse);
1909 talloc_free(endpoint);
1910
1911 return 0;
1912}
1913
1914/*! gprs_ns2_sns_count
1915 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1916 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1917 */
1918int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1919{
1920 struct ns2_sns_state *gss;
1921 struct sns_endpoint *endpoint;
1922 int count = 0;
1923
1924 if (nse->ll != GPRS_NS2_LL_UDP) {
1925 return -EINVAL;
1926 }
1927
Alexander Couzens138b96f2021-01-25 16:23:29 +01001928 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001929 return -EINVAL;
1930 }
1931
1932 gss = nse->bss_sns_fi->priv;
1933 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1934 count++;
1935
1936 return count;
1937}
1938
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001939void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1940{
1941 struct ns2_sns_state *gss;
1942 struct gprs_ns2_vc *tmp;
1943
1944 if (!nse->bss_sns_fi)
1945 return;
1946
1947 gss = nse->bss_sns_fi->priv;
1948 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1949 return;
1950
1951 if (alive == gss->alive)
1952 return;
1953
1954 /* check if this is the current SNS NS-VC */
1955 if (nsvc == gss->sns_nsvc) {
1956 /* only replace the SNS NS-VC if there are other alive NS-VC.
1957 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1958 * and couldn't confirm yet if the NS-VC comes up */
1959 if (gss->alive && !alive)
1960 ns2_sns_replace_nsvc(nsvc);
1961 }
1962
1963 if (alive) {
1964 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001965 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001966 } else {
1967 /* is there at least another alive nsvc? */
1968 llist_for_each_entry(tmp, &nse->nsvc, list) {
1969 if (ns2_vc_is_unblocked(tmp))
1970 return;
1971 }
1972
1973 /* all NS-VC have failed */
1974 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001975 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001976 }
1977}
1978
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001979int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1980 struct gprs_ns2_vc_bind *bind)
1981{
1982 struct ns2_sns_state *gss;
1983 struct ns2_sns_bind *tmp;
1984
1985 OSMO_ASSERT(nse->bss_sns_fi);
1986 gss = nse->bss_sns_fi->priv;
1987
1988 if (!gprs_ns2_is_ip_bind(bind)) {
1989 return -EINVAL;
1990 }
1991
1992 if (!llist_empty(&gss->binds)) {
1993 llist_for_each_entry(tmp, &gss->binds, list) {
1994 if (tmp->bind == bind)
1995 return -EALREADY;
1996 }
1997 }
1998
1999 tmp = talloc_zero(gss, struct ns2_sns_bind);
2000 if (!tmp)
2001 return -ENOMEM;
2002 tmp->bind = bind;
2003 llist_add_tail(&tmp->list, &gss->binds);
2004
2005 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
2006 return 0;
2007}
2008
2009/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
2010int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
2011 struct gprs_ns2_vc_bind *bind)
2012{
2013 struct ns2_sns_state *gss;
2014 struct ns2_sns_bind *tmp, *tmp2;
2015 bool found = false;
2016
2017 if (!nse->bss_sns_fi)
2018 return -EINVAL;
2019
2020 gss = nse->bss_sns_fi->priv;
2021 if (gss->initial_bind && gss->initial_bind->bind == bind) {
2022 if (gss->initial_bind->list.prev == &gss->binds)
2023 gss->initial_bind = NULL;
2024 else
2025 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
2026 }
2027
2028 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
2029 if (tmp->bind == bind) {
2030 llist_del(&tmp->list);
2031 found = true;
Alexander Couzensa35c2962021-04-19 03:30:15 +02002032 break;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002033 }
2034 }
2035
2036 if (!found)
2037 return -ENOENT;
2038
2039 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
2040 return 0;
2041}
2042
Alexander Couzens71128672021-06-05 18:44:01 +02002043/* Update SNS weights for a bind (local endpoint).
2044 * \param[in] bind the bind which has been updated
Alexander Couzensc4704762021-02-08 23:13:12 +01002045 */
2046void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
2047{
2048 /* TODO: implement weights after binds per sns implemented */
2049}
2050
Harald Welte4f127462021-03-02 20:49:10 +01002051
2052
2053
2054/***********************************************************************
2055 * SGSN role
2056 ***********************************************************************/
2057
2058static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2059{
2060 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2061 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2062 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
2063}
2064
2065/* We're waiting for inbound SNS-CONFIG from the BSS */
2066static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2067{
2068 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2069 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2070 struct gprs_ns2_inst *nsi = nse->nsi;
2071 uint8_t cause;
2072 int rc;
2073
2074 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2075
2076 switch (event) {
2077 case GPRS_SNS_EV_RX_CONFIG:
2078 case GPRS_SNS_EV_RX_CONFIG_END:
2079 rc = ns_sns_append_remote_eps(fi, data);
2080 if (rc < 0) {
2081 cause = -rc;
2082 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2083 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2084 return;
2085 }
2086 /* only change state if last CONFIG was received */
2087 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
2088 /* ensure sum of data weight / sig weights is > 0 */
2089 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
2090 cause = NS_CAUSE_INVAL_WEIGH;
2091 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2092 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2093 break;
2094 }
2095 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2096 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2097 } else {
2098 /* just send CONFIG-ACK */
2099 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2100 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2101 }
2102 break;
2103 }
2104}
2105
2106static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2107{
2108 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2109 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2110
Harald Welte4f127462021-03-02 20:49:10 +01002111 /* transmit SGSN-oriented SNS-CONFIG */
Alexander Couzens71128672021-06-05 18:44:01 +02002112 ns2_tx_sns_config(gss->sns_nsvc, true, gss->local.ip4, gss->local.num_ip4,
2113 gss->local.ip6, gss->local.num_ip6);
Harald Welte4f127462021-03-02 20:49:10 +01002114}
2115
2116/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2117static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2118{
2119 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2120 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2121 struct tlv_parsed *tp = NULL;
2122
2123 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2124
2125 switch (event) {
2126 case GPRS_SNS_EV_RX_CONFIG_ACK:
2127 tp = data;
2128 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2129 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2130 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2131 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2132 break;
2133 }
2134 /* we currently only send one SNS-CONFIG with END FLAG */
2135 if (true) {
2136 create_missing_nsvcs(fi);
2137 /* start the test procedure on ALL NSVCs! */
2138 gprs_ns2_start_alive_all_nsvcs(nse);
2139 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2140 }
2141 break;
2142 }
2143}
2144
2145/* SGSN-side SNS state machine */
2146static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2147 [GPRS_SNS_ST_UNCONFIGURED] = {
2148 .in_event_mask = 0, /* handled by all_state_action */
2149 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2150 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2151 .name = "UNCONFIGURED",
2152 .action = ns2_sns_st_sgsn_unconfigured,
2153 },
2154 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
2155 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
2156 S(GPRS_SNS_EV_RX_CONFIG_END),
2157 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2158 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2159 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2160 .name = "SGSN_WAIT_CONFIG",
2161 .action = ns2_sns_st_sgsn_wait_config,
2162 },
2163 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
2164 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
2165 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2166 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2167 S(GPRS_SNS_ST_CONFIGURED),
2168 .name = "SGSN_WAIT_CONFIG_ACK",
2169 .action = ns2_sns_st_sgsn_wait_config_ack,
2170 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2171 },
2172 [GPRS_SNS_ST_CONFIGURED] = {
2173 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
2174 S(GPRS_SNS_EV_RX_DELETE) |
2175 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
2176 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
2177 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2178 .name = "CONFIGURED",
2179 /* shared with BSS side; once configured there's no difference */
2180 .action = ns2_sns_st_configured,
2181 .onenter = ns2_sns_st_configured_onenter,
2182 },
2183};
2184
2185static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2186{
2187 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2188 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2189 struct gprs_ns2_inst *nsi = nse->nsi;
2190
2191 gss->N++;
2192 switch (fi->T) {
2193 case 3:
2194 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2195 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2196 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2197 } else {
2198 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2199 }
2200 break;
2201 case 4:
2202 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2203 break;
2204 }
2205 return 0;
2206}
2207
2208
2209/* allstate-action for SGSN role */
2210static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2211{
2212 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2213 struct tlv_parsed *tp = NULL;
Harald Welte01fa6a32021-03-04 19:49:38 +01002214 size_t num_local_eps, num_remote_eps;
Harald Welte4f127462021-03-02 20:49:10 +01002215 uint8_t flag;
Harald Weltea2c5af52021-03-04 17:59:35 +01002216 uint8_t cause;
Harald Welte4f127462021-03-02 20:49:10 +01002217
2218 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2219
2220 switch (event) {
2221 case GPRS_SNS_EV_RX_SIZE:
2222 tp = (struct tlv_parsed *) data;
Harald Weltea2c5af52021-03-04 17:59:35 +01002223 /* check for mandatory / conditional IEs */
2224 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) ||
2225 !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) {
2226 cause = NS_CAUSE_MISSING_ESSENT_IE;
2227 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2228 break;
2229 }
2230 if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) &&
2231 !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) {
2232 cause = NS_CAUSE_MISSING_ESSENT_IE;
Harald Welte4f127462021-03-02 20:49:10 +01002233 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2234 break;
2235 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002236 if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2))
2237 gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR);
2238 if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2))
2239 gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR);
2240 /* decide if we go for IPv4 or IPv6 */
2241 if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, IPv6)) {
2242 gss->ip = IPv6;
Harald Welte2d807b62021-03-24 01:57:30 +01002243 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002244 num_local_eps = gss->local.num_ip6;
Harald Welte01fa6a32021-03-04 19:49:38 +01002245 num_remote_eps = gss->num_max_ip6_remote;
2246 } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, IPv4)) {
2247 gss->ip = IPv4;
Harald Welte2d807b62021-03-24 01:57:30 +01002248 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002249 num_local_eps = gss->local.num_ip4;
Harald Welte01fa6a32021-03-04 19:49:38 +01002250 num_remote_eps = gss->num_max_ip4_remote;
2251 } else {
Alexander Couzens71128672021-06-05 18:44:01 +02002252 if (gss->local.num_ip4 && !gss->num_max_ip4_remote)
Harald Welte01fa6a32021-03-04 19:49:38 +01002253 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
2254 else
2255 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
2256 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2257 break;
2258 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002259 /* ensure number of NS-VCs is sufficient for full mesh */
2260 gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC);
2261 if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) {
2262 LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, "
2263 "but BSS supports only %zu maximum NS-VCs\n", num_local_eps,
2264 num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs);
2265 cause = NS_CAUSE_INVAL_NR_NS_VC;
2266 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2267 break;
2268 }
2269 /* perform state reset, if requested */
Harald Welte4f127462021-03-02 20:49:10 +01002270 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2271 if (flag & 1) {
2272 struct gprs_ns2_vc *nsvc, *nsvc2;
2273 /* clear all state */
Harald Welte46eb7642021-03-04 17:49:59 +01002274 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
Harald Welte4f127462021-03-02 20:49:10 +01002275 gss->N = 0;
Alexander Couzensd2c6c492021-06-06 01:57:52 +02002276 ns2_clear_elems(&gss->local);
2277 ns2_clear_elems(&gss->remote);
Harald Welte4f127462021-03-02 20:49:10 +01002278 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2279 if (nsvc == gss->sns_nsvc) {
2280 /* keep the NSVC we need for SNS, but unconfigure it */
2281 nsvc->sig_weight = 0;
2282 nsvc->data_weight = 0;
2283 ns2_vc_force_unconfigured(nsvc);
2284 } else {
2285 /* free all other NS-VCs */
2286 gprs_ns2_free_nsvc(nsvc);
2287 }
2288 }
Harald Welte2d807b62021-03-24 01:57:30 +01002289 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte4f127462021-03-02 20:49:10 +01002290 }
2291 /* send SIZE_ACK */
2292 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2293 /* only wait for SNS-CONFIG in case of Reset flag */
2294 if (flag & 1)
2295 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2296 break;
2297 default:
2298 ns2_sns_st_all_action(fi, event, data);
2299 break;
2300 }
2301}
2302
2303static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2304 .name = "GPRS-NS2-SNS-SGSN",
2305 .states = ns2_sns_sgsn_states,
2306 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
2307 .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) |
2308 S(GPRS_SNS_EV_REQ_NO_NSVC) |
2309 S(GPRS_SNS_EV_REQ_ADD_BIND) |
2310 S(GPRS_SNS_EV_REQ_DELETE_BIND),
2311 .allstate_action = ns2_sns_st_all_action_sgsn,
2312 .cleanup = NULL,
2313 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2314 .event_names = gprs_sns_event_names,
2315 .pre_term = NULL,
2316 .log_subsys = DLNS,
2317};
2318
2319/*! Allocate an IP-SNS FSM for the SGSN side.
2320 * \param[in] nse NS Entity in which the FSM runs
2321 * \param[in] id string identifier
2322 * \returns FSM instance on success; NULL on error */
2323struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2324{
2325 struct osmo_fsm_inst *fi;
2326 struct ns2_sns_state *gss;
2327
2328 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2329 if (!fi)
2330 return fi;
2331
2332 gss = talloc_zero(fi, struct ns2_sns_state);
2333 if (!gss)
2334 goto err;
2335
2336 fi->priv = gss;
2337 gss->nse = nse;
2338 gss->role = GPRS_SNS_ROLE_SGSN;
2339 INIT_LLIST_HEAD(&gss->sns_endpoints);
2340 INIT_LLIST_HEAD(&gss->binds);
2341
2342 return fi;
2343err:
2344 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2345 return NULL;
2346}
2347
2348
2349
2350
Alexander Couzens6a161492020-07-12 13:45:50 +02002351/* initialize osmo_ctx on main tread */
2352static __attribute__((constructor)) void on_dso_load_ctx(void)
2353{
2354 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002355 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002356}