blob: c50b2b943f177cbee09adc7c910cf6129d5fbcff [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 */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200452static int add_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
453 const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +0200454{
455 unsigned int i;
456
Alexander Couzens6a161492020-07-12 13:45:50 +0200457 /* check for duplicates */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200458 for (i = 0; i < elems->num_ip4; i++) {
459 if (memcmp(&elems->ip4[i], ip4, sizeof(*ip4)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200460 continue;
Alexander Couzensd3507e82021-06-06 03:32:32 +0200461 return -1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200462 }
463
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200464 elems->ip4 = talloc_realloc(gss, elems->ip4, struct gprs_ns_ie_ip4_elem,
465 elems->num_ip4+1);
466 elems->ip4[elems->num_ip4] = *ip4;
467 elems->num_ip4 += 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200468 return 0;
469}
470
471/* Remove a given remote IPv4 element from gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200472static int remove_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
473 const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +0200474{
475 unsigned int i;
476
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200477 for (i = 0; i < elems->num_ip4; i++) {
478 if (memcmp(&elems->ip4[i], ip4, sizeof(*ip4)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200479 continue;
480 /* all array elements < i remain as they are; all > i are shifted left by one */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200481 memmove(&elems->ip4[i], &elems->ip4[i+1], elems->num_ip4-i-1);
482 elems->num_ip4 -= 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200483 return 0;
484 }
485 return -1;
486}
487
488/* update the weights for specified remote IPv4 */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200489static int update_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
490 const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +0200491{
492 unsigned int i;
493
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200494 for (i = 0; i < elems->num_ip4; i++) {
495 if (elems->ip4[i].ip_addr != ip4->ip_addr ||
496 elems->ip4[i].udp_port != ip4->udp_port)
Alexander Couzens6a161492020-07-12 13:45:50 +0200497 continue;
498
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200499 elems->ip4[i].sig_weight = ip4->sig_weight;
500 elems->ip4[i].data_weight = ip4->data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200501 return 0;
502 }
503 return -1;
504}
505
506/* Add a given remote IPv6 element to gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200507static int add_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
508 const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200509{
Alexander Couzens6a161492020-07-12 13:45:50 +0200510
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200511 elems->ip6 = talloc_realloc(gss, elems->ip6, struct gprs_ns_ie_ip6_elem,
512 elems->num_ip6+1);
513 elems->ip6[elems->num_ip6] = *ip6;
514 elems->num_ip6 += 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200515 return 0;
516}
517
518/* Remove a given remote IPv6 element from gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200519static int remove_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
520 const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200521{
522 unsigned int i;
523
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200524 for (i = 0; i < elems->num_ip6; i++) {
525 if (memcmp(&elems->ip6[i], ip6, sizeof(*ip6)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200526 continue;
527 /* all array elements < i remain as they are; all > i are shifted left by one */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200528 memmove(&elems->ip6[i], &elems->ip6[i+1], elems->num_ip6-i-1);
529 elems->num_ip6 -= 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200530 return 0;
531 }
532 return -1;
533}
534
535/* update the weights for specified remote IPv6 */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200536static int update_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
537 const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200538{
539 unsigned int i;
540
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200541 for (i = 0; i < elems->num_ip6; i++) {
542 if (memcmp(&elems->ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
543 elems->ip6[i].udp_port != ip6->udp_port)
Alexander Couzens6a161492020-07-12 13:45:50 +0200544 continue;
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200545 elems->ip6[i].sig_weight = ip6->sig_weight;
546 elems->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) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200569 if (update_ip4_elem(gss, &gss->remote, ip4))
Alexander Couzens6a161492020-07-12 13:45:50 +0200570 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) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200579 if (update_ip6_elem(gss, &gss->remote, ip6))
Alexander Couzens6a161492020-07-12 13:45:50 +0200580 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) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200622 if (remove_ip4_elem(gss, &gss->remote, ip4) < 0)
Alexander Couzens6a161492020-07-12 13:45:50 +0200623 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) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200629 if (remove_ip6_elem(gss, &gss->remote, ip6))
Alexander Couzens6a161492020-07-12 13:45:50 +0200630 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:
Alexander Couzensd3507e82021-06-06 03:32:32 +0200668 if (gss->remote.num_ip4 >= gss->num_max_ip4_remote)
669 return -NS_CAUSE_INVAL_NR_NS_VC;
670 /* TODO: log message duplicate */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200671 rc = add_ip4_elem(gss, &gss->remote, ip4);
Alexander Couzens6a161492020-07-12 13:45:50 +0200672 break;
673 case IPv6:
Alexander Couzensd3507e82021-06-06 03:32:32 +0200674 if (gss->remote.num_ip6 >= gss->num_max_ip6_remote)
675 return -NS_CAUSE_INVAL_NR_NS_VC;
676 /* TODO: log message duplicate */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200677 rc = add_ip6_elem(gss, &gss->remote, ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200678 break;
679 default:
680 /* the gss->ip is initialized with the bss */
681 OSMO_ASSERT(false);
682 }
683
684 if (rc)
Alexander Couzensd3507e82021-06-06 03:32:32 +0200685 return -NS_CAUSE_PROTO_ERR_UNSPEC;
Alexander Couzens6a161492020-07-12 13:45:50 +0200686
687 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
688 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
689 * unspecified" */
690 switch (gss->ip) {
691 case IPv4:
692 nsvc = nsvc_by_ip4_elem(nse, ip4);
693 if (nsvc) {
694 /* the nsvc should be already in sync with the ip4 / ip6 elements */
695 return -NS_CAUSE_PROTO_ERR_UNSPEC;
696 }
697
698 /* TODO: failure case */
699 ns2_nsvc_create_ip4(fi, nse, ip4);
700 break;
701 case IPv6:
702 nsvc = nsvc_by_ip6_elem(nse, ip6);
703 if (nsvc) {
704 /* the nsvc should be already in sync with the ip4 / ip6 elements */
705 return -NS_CAUSE_PROTO_ERR_UNSPEC;
706 }
707
708 /* TODO: failure case */
709 ns2_nsvc_create_ip6(fi, nse, ip6);
710 break;
711 }
712
713 gprs_ns2_start_alive_all_nsvcs(nse);
714
715 return 0;
716}
717
718
Harald Welte694dad52021-03-23 15:22:16 +0100719static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200720{
Harald Weltef61a9152021-03-02 22:20:17 +0100721 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
722 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzense769f522020-12-07 07:37:07 +0100723 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200724}
725
Harald Welte694dad52021-03-23 15:22:16 +0100726static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200727{
Harald Weltef61a9152021-03-02 22:20:17 +0100728 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200729 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
730 struct gprs_ns2_inst *nsi = nse->nsi;
731 struct tlv_parsed *tp = NULL;
732
Harald Weltef61a9152021-03-02 22:20:17 +0100733 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
734
Alexander Couzens6a161492020-07-12 13:45:50 +0200735 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100736 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200737 tp = data;
738 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
739 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
740 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
741 /* TODO: What to do? */
742 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100743 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200744 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
745 }
746 break;
747 default:
748 OSMO_ASSERT(0);
749 }
750}
751
Harald Welte01fa6a32021-03-04 19:49:38 +0100752static int ns2_sns_count_num_local_ep(struct osmo_fsm_inst *fi, enum ns2_sns_type stype)
753{
754 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
755 struct ns2_sns_bind *sbind;
756 int count = 0;
757
758 llist_for_each_entry(sbind, &gss->binds, list) {
759 const struct osmo_sockaddr *sa = gprs_ns2_ip_bind_sockaddr(sbind->bind);
760 if (!sa)
761 continue;
762
763 switch (stype) {
764 case IPv4:
765 if (sa->u.sas.ss_family == AF_INET)
766 count++;
767 break;
768 case IPv6:
769 if (sa->u.sas.ss_family == AF_INET6)
770 count++;
771 break;
772 }
773 }
774 return count;
775}
776
Harald Welte24920e22021-03-04 13:03:27 +0100777static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200778{
779 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100780 struct gprs_ns_ie_ip4_elem *ip4_elems;
781 struct gprs_ns_ie_ip6_elem *ip6_elems;
782 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100783 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100784 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100785 const struct osmo_sockaddr *sa;
786 struct osmo_sockaddr local;
787 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200788
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200789 ns2_clear_elems(&gss->local);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100790
Alexander Couzense769f522020-12-07 07:37:07 +0100791 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100792 if (gss->role == GPRS_SNS_ROLE_BSS) {
793 if (!gss->initial)
794 return;
795 remote = &gss->initial->saddr;
796 } else
797 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100798
799 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100800 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100801 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100802 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100803 return;
804 }
805
Alexander Couzense769f522020-12-07 07:37:07 +0100806 switch (gss->ip) {
807 case IPv4:
Alexander Couzens71128672021-06-05 18:44:01 +0200808 ip4_elems = talloc_realloc(fi, gss->local.ip4, struct gprs_ns_ie_ip4_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100809 if (!ip4_elems)
810 return;
811
Alexander Couzens71128672021-06-05 18:44:01 +0200812 gss->local.ip4 = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100813 llist_for_each_entry(sbind, &gss->binds, list) {
814 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100815 sa = gprs_ns2_ip_bind_sockaddr(bind);
816 if (!sa)
817 continue;
818
819 if (sa->u.sas.ss_family != AF_INET)
820 continue;
821
822 /* check if this is an specific bind */
823 if (sa->u.sin.sin_addr.s_addr == 0) {
824 if (osmo_sockaddr_local_ip(&local, remote))
825 continue;
826
827 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
828 } else {
829 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
830 }
831
832 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100833 ip4_elems->sig_weight = bind->sns_sig_weight;
834 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100835 ip4_elems++;
836 }
837
Alexander Couzens71128672021-06-05 18:44:01 +0200838 gss->local.num_ip4 = count;
839 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->local.num_ip4, 8);
Alexander Couzense769f522020-12-07 07:37:07 +0100840 break;
841 case IPv6:
842 /* IPv6 */
Alexander Couzens71128672021-06-05 18:44:01 +0200843 ip6_elems = talloc_realloc(fi, gss->local.ip6, struct gprs_ns_ie_ip6_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100844 if (!ip6_elems)
845 return;
846
Alexander Couzens71128672021-06-05 18:44:01 +0200847 gss->local.ip6 = ip6_elems;
Alexander Couzense769f522020-12-07 07:37:07 +0100848
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100849 llist_for_each_entry(sbind, &gss->binds, list) {
850 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100851 sa = gprs_ns2_ip_bind_sockaddr(bind);
852 if (!sa)
853 continue;
854
855 if (sa->u.sas.ss_family != AF_INET6)
856 continue;
857
858 /* check if this is an specific bind */
859 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
860 if (osmo_sockaddr_local_ip(&local, remote))
861 continue;
862
863 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
864 } else {
865 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
866 }
867
868 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100869 ip6_elems->sig_weight = bind->sns_sig_weight;
870 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100871
872 ip6_elems++;
873 }
Alexander Couzens71128672021-06-05 18:44:01 +0200874 gss->local.num_ip6 = count;
875 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->local.num_ip6, 8);
Alexander Couzense769f522020-12-07 07:37:07 +0100876 break;
877 }
Harald Welte24920e22021-03-04 13:03:27 +0100878}
879
Alexander Couzens6608ce92021-04-26 20:39:46 +0200880static void ns2_sns_choose_next_bind(struct ns2_sns_state *gss)
881{
882 /* take the first bind or take the next bind */
883 if (!gss->initial_bind || gss->initial_bind->list.next == &gss->binds)
884 gss->initial_bind = llist_first_entry_or_null(&gss->binds, struct ns2_sns_bind, list);
885 else
886 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
887}
888
Harald Welte24920e22021-03-04 13:03:27 +0100889/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Harald Welte694dad52021-03-23 15:22:16 +0100890static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Harald Welte24920e22021-03-04 13:03:27 +0100891{
892 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
893
Harald Weltef61a9152021-03-02 22:20:17 +0100894 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
895
Harald Welte24920e22021-03-04 13:03:27 +0100896 /* on a generic failure, the timer callback will recover */
897 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
898 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Harald Welte694dad52021-03-23 15:22:16 +0100899 if (old_state != GPRS_SNS_ST_BSS_SIZE)
Harald Welte24920e22021-03-04 13:03:27 +0100900 gss->N = 0;
901
902 gss->alive = false;
903
904 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens6608ce92021-04-26 20:39:46 +0200905 ns2_sns_choose_next_bind(gss);
Harald Welte24920e22021-03-04 13:03:27 +0100906
907 /* setup the NSVC */
908 if (!gss->sns_nsvc) {
909 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
910 struct osmo_sockaddr *remote = &gss->initial->saddr;
911 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
912 if (!gss->sns_nsvc)
913 return;
Harald Weltec962a2e2021-03-05 08:09:08 +0100914 /* A pre-configured endpoint shall not be used for NSE data or signalling traffic
915 * (with the exception of Size and Configuration procedures) unless it is configured
916 * by the SGSN using the auto-configuration procedures */
Harald Welte24920e22021-03-04 13:03:27 +0100917 gss->sns_nsvc->sns_only = true;
918 }
919
Alexander Couzens6a161492020-07-12 13:45:50 +0200920 if (gss->num_max_ip4_remote > 0)
Alexander Couzens71128672021-06-05 18:44:01 +0200921 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->local.num_ip4, -1);
Alexander Couzens6a161492020-07-12 13:45:50 +0200922 else
Alexander Couzens71128672021-06-05 18:44:01 +0200923 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->local.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200924}
925
Harald Welte694dad52021-03-23 15:22:16 +0100926static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200927{
Harald Weltef61a9152021-03-02 22:20:17 +0100928 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens3df58862021-02-05 17:18:08 +0100929 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Harald Weltef61a9152021-03-02 22:20:17 +0100930 struct tlv_parsed *tp = NULL;
931
932 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzens6a161492020-07-12 13:45:50 +0200933
934 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100935 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200936 tp = (struct tlv_parsed *) data;
937 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
938 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
939 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
940 /* TODO: What to do? */
941 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100942 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 +0200943 }
944 break;
945 default:
946 OSMO_ASSERT(0);
947 }
948}
949
Harald Welte694dad52021-03-23 15:22:16 +0100950static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200951{
952 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100953
Harald Weltef61a9152021-03-02 22:20:17 +0100954 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
955
Harald Welte694dad52021-03-23 15:22:16 +0100956 if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS)
Alexander Couzens790a9632021-02-05 17:18:39 +0100957 gss->N = 0;
958
Alexander Couzens6a161492020-07-12 13:45:50 +0200959 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200960 switch (gss->ip) {
961 case IPv4:
962 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzens71128672021-06-05 18:44:01 +0200963 gss->local.ip4, gss->local.num_ip4,
Alexander Couzense78207f2020-12-07 06:19:29 +0100964 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200965 break;
966 case IPv6:
967 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100968 NULL, 0,
Alexander Couzens71128672021-06-05 18:44:01 +0200969 gss->local.ip6, gss->local.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200970 break;
971 }
972}
973
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100974/* calculate the timeout of the configured state. the configured
975 * state will fail if not at least one NS-VC is alive within X second.
976 */
977static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
978{
979 int secs;
980 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
981 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
982 secs += nsi->timeout[NS_TOUT_TNS_TEST];
983
984 return secs;
985}
Alexander Couzens6a161492020-07-12 13:45:50 +0200986
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100987/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
988static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200989{
990 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200991
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100992 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
993 const struct gprs_ns_ie_ip4_elem *v4_list;
994 unsigned int num_v4;
995 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
996 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +0200997
Alexander Couzens71128672021-06-05 18:44:01 +0200998 if (num_v4 && gss->remote.ip6)
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100999 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +02001000
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001001 /* realloc to the new size */
Alexander Couzens71128672021-06-05 18:44:01 +02001002 gss->remote.ip4 = talloc_realloc(gss, gss->remote.ip4,
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001003 struct gprs_ns_ie_ip4_elem,
Alexander Couzens71128672021-06-05 18:44:01 +02001004 gss->remote.num_ip4 + num_v4);
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001005 /* append the new entries to the end of the list */
Alexander Couzens71128672021-06-05 18:44:01 +02001006 memcpy(&gss->remote.ip4[gss->remote.num_ip4], v4_list, num_v4*sizeof(*v4_list));
1007 gss->remote.num_ip4 += num_v4;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001008
1009 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
Alexander Couzens71128672021-06-05 18:44:01 +02001010 gss->remote.num_ip4);
Alexander Couzens6a161492020-07-12 13:45:50 +02001011 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001012
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001013 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1014 const struct gprs_ns_ie_ip6_elem *v6_list;
1015 unsigned int num_v6;
1016 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1017 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
1018
Alexander Couzens71128672021-06-05 18:44:01 +02001019 if (num_v6 && gss->remote.ip4)
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001020 return -NS_CAUSE_INVAL_NR_IPv6_EP;
1021
1022 /* realloc to the new size */
Alexander Couzens71128672021-06-05 18:44:01 +02001023 gss->remote.ip6 = talloc_realloc(gss, gss->remote.ip6,
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001024 struct gprs_ns_ie_ip6_elem,
Alexander Couzens71128672021-06-05 18:44:01 +02001025 gss->remote.num_ip6 + num_v6);
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001026 /* append the new entries to the end of the list */
Alexander Couzens71128672021-06-05 18:44:01 +02001027 memcpy(&gss->remote.ip6[gss->remote.num_ip6], v6_list, num_v6*sizeof(*v6_list));
1028 gss->remote.num_ip6 += num_v6;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001029
Alexander Couzens71128672021-06-05 18:44:01 +02001030 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %d entries\n",
1031 gss->remote.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +02001032 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001033
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001034 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001035}
1036
Harald Welte694dad52021-03-23 15:22:16 +01001037static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens790a9632021-02-05 17:18:39 +01001038{
1039 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1040
Harald Weltef61a9152021-03-02 22:20:17 +01001041 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1042
Harald Welte694dad52021-03-23 15:22:16 +01001043 if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN)
Alexander Couzens790a9632021-02-05 17:18:39 +01001044 gss->N = 0;
1045}
1046
Harald Welte694dad52021-03-23 15:22:16 +01001047static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +02001048{
1049 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001050 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1051 uint8_t cause;
1052 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001053
Harald Weltef61a9152021-03-02 22:20:17 +01001054 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1055
Alexander Couzens6a161492020-07-12 13:45:50 +02001056 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001057 case GPRS_SNS_EV_RX_CONFIG_END:
1058 case GPRS_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001059 rc = ns_sns_append_remote_eps(fi, data);
1060 if (rc < 0) {
1061 cause = -rc;
1062 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1063 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1064 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001065 }
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001066 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1067 /* check if sum of data / sig weights == 0 */
1068 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1069 cause = NS_CAUSE_INVAL_WEIGH;
1070 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1071 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1072 return;
1073 }
1074 create_missing_nsvcs(fi);
1075 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1076 /* start the test procedure on ALL NSVCs! */
1077 gprs_ns2_start_alive_all_nsvcs(nse);
1078 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1079 } else {
1080 /* just send CONFIG-ACK */
1081 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1082 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001083 }
1084 break;
1085 default:
1086 OSMO_ASSERT(0);
1087 }
1088}
1089
Alexander Couzens67725e22021-02-15 02:37:03 +01001090/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001091static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1092 struct ns2_sns_state *gss,
1093 struct tlv_parsed *tp)
1094{
1095 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1096 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1097 int num_v4 = 0, num_v6 = 0;
1098 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001099 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001100 int rc = 0;
1101
1102 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1103 * check uniqueness within the lists (no doublicate entries)
1104 * check not-known-by-us and sent back a list of unknown/known values
1105 * (abnormal behaviour according to 48.016)
1106 */
1107
1108 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1109 if (gss->ip == IPv4) {
1110 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1111 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1112 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1113 return;
1114 }
1115
1116 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1117 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001118 for (i = 0; i < num_v4; i++) {
1119 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001120 rc = do_sns_add(fi, &v4_list[i], NULL);
1121 if (rc < 0) {
1122 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001123 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001124 do_sns_delete(fi, &v4_list[j], NULL);
1125 cause = -rc;
1126 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1127 break;
1128 }
1129 }
1130 } else { /* IPv6 */
1131 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1132 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1133 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1134 return;
1135 }
1136
1137 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1138 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001139 for (i = 0; i < num_v6; i++) {
1140 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001141 rc = do_sns_add(fi, NULL, &v6_list[i]);
1142 if (rc < 0) {
1143 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001144 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001145 do_sns_delete(fi, NULL, &v6_list[j]);
1146 cause = -rc;
1147 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1148 break;
1149 }
1150 }
1151 }
1152
1153 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1154 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1155}
1156
1157static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1158 struct ns2_sns_state *gss,
1159 struct tlv_parsed *tp)
1160{
1161 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1162 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1163 int num_v4 = 0, num_v6 = 0;
1164 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001165 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001166 int rc = 0;
1167
1168 /* TODO: split up delete into v4 + v6
1169 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1170 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1171 */
1172 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1173 if (gss->ip == IPv4) {
1174 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1175 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1176 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001177 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001178 rc = do_sns_delete(fi, &v4_list[i], NULL);
1179 if (rc < 0) {
1180 cause = -rc;
1181 /* continue to delete others */
1182 }
1183 }
1184 if (cause != 0xff) {
1185 /* TODO: create list of not-deleted and return it */
1186 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1187 return;
1188 }
1189
1190 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1191 /* delete all NS-VCs for given IPv4 address */
1192 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1193 struct gprs_ns_ie_ip4_elem *ip4_remote;
1194 uint32_t ip_addr = *(uint32_t *)(ie+1);
1195 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1196 cause = NS_CAUSE_UNKN_IP_ADDR;
1197 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1198 return;
1199 }
1200 /* make a copy as do_sns_delete() will change the array underneath us */
Alexander Couzens71128672021-06-05 18:44:01 +02001201 ip4_remote = talloc_memdup(fi, gss->remote.ip4,
1202 gss->remote.num_ip4 * sizeof(*v4_list));
1203 for (i = 0; i < gss->remote.num_ip4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001204 if (ip4_remote[i].ip_addr == ip_addr) {
1205 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1206 if (rc < 0) {
1207 cause = -rc;
1208 /* continue to delete others */
1209 }
1210 }
1211 }
1212 talloc_free(ip4_remote);
1213 if (cause != 0xff) {
1214 /* TODO: create list of not-deleted and return it */
1215 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1216 return;
1217 }
1218 } else {
1219 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1220 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1221 return;
1222 }
1223 } else { /* IPv6 */
1224 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1225 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1226 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001227 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001228 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1229 if (rc < 0) {
1230 cause = -rc;
1231 /* continue to delete others */
1232 }
1233 }
1234 if (cause != 0xff) {
1235 /* TODO: create list of not-deleted and return it */
1236 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1237 return;
1238 }
1239 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1240 /* delete all NS-VCs for given IPv4 address */
1241 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1242 struct gprs_ns_ie_ip6_elem *ip6_remote;
1243 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001244 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001245 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1246 cause = NS_CAUSE_UNKN_IP_ADDR;
1247 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1248 return;
1249 }
1250 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1251 /* make a copy as do_sns_delete() will change the array underneath us */
Alexander Couzens71128672021-06-05 18:44:01 +02001252 ip6_remote = talloc_memdup(fi, gss->remote.ip6,
1253 gss->remote.num_ip6 * sizeof(*v4_list));
1254 for (i = 0; i < gss->remote.num_ip6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001255 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1256 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1257 if (rc < 0) {
1258 cause = -rc;
1259 /* continue to delete others */
1260 }
1261 }
1262 }
1263
1264 talloc_free(ip6_remote);
1265 if (cause != 0xff) {
1266 /* TODO: create list of not-deleted and return it */
1267 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1268 return;
1269 }
1270 } else {
1271 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1272 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1273 return;
1274 }
1275 }
1276 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1277}
1278
1279static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1280 struct ns2_sns_state *gss,
1281 struct tlv_parsed *tp)
1282{
1283 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1284 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1285 int num_v4 = 0, num_v6 = 0;
1286 uint8_t trans_id, cause = 0xff;
1287 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001288 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001289
1290 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1291 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1292 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1293 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001294 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001295 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1296 if (rc < 0) {
1297 cause = -rc;
1298 /* continue to others */
1299 }
1300 }
1301 if (cause != 0xff) {
1302 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1303 return;
1304 }
1305 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1306 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1307 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001308 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001309 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1310 if (rc < 0) {
1311 cause = -rc;
1312 /* continue to others */
1313 }
1314 }
1315 if (cause != 0xff) {
1316 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1317 return;
1318 }
1319 } else {
1320 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1321 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1322 return;
1323 }
1324 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1325}
1326
1327static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1328{
1329 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1330 struct tlv_parsed *tp = data;
1331
1332 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001333 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001334 ns2_sns_st_configured_add(fi, gss, tp);
1335 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001336 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001337 ns2_sns_st_configured_delete(fi, gss, tp);
1338 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001339 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001340 ns2_sns_st_configured_change(fi, gss, tp);
1341 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001342 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001343 osmo_timer_del(&fi->timer);
1344 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001345 }
1346}
1347
1348static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1349{
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001350 struct gprs_ns2_vc *nsvc;
1351 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001352 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001353 /* NS-VC status updates are only parsed in ST_CONFIGURED.
1354 * Do an initial check if there are any nsvc alive atm */
1355 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1356 if (ns2_vc_is_unblocked(nsvc)) {
1357 gss->alive = true;
1358 osmo_timer_del(&fi->timer);
1359 break;
1360 }
1361 }
1362
Alexander Couzens53e70092021-04-06 15:45:47 +02001363 /* remove the initial NSVC if the NSVC isn't part of the configuration */
1364 if (gss->sns_nsvc->sns_only)
1365 gprs_ns2_free_nsvc(gss->sns_nsvc);
1366
Alexander Couzens138b96f2021-01-25 16:23:29 +01001367 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001368}
1369
1370static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1371 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001372 .in_event_mask = 0, /* handled by all_state_action */
Alexander Couzens0a7c5ee2021-04-10 18:20:21 +02001373 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1374 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001375 .name = "UNCONFIGURED",
Harald Welte694dad52021-03-23 15:22:16 +01001376 .action = ns2_sns_st_bss_unconfigured,
Alexander Couzens6a161492020-07-12 13:45:50 +02001377 },
Harald Welte694dad52021-03-23 15:22:16 +01001378 [GPRS_SNS_ST_BSS_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001379 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001380 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001381 S(GPRS_SNS_ST_BSS_SIZE) |
1382 S(GPRS_SNS_ST_BSS_CONFIG_BSS),
1383 .name = "BSS_SIZE",
1384 .action = ns2_sns_st_bss_size,
1385 .onenter = ns2_sns_st_bss_size_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001386 },
Harald Welte694dad52021-03-23 15:22:16 +01001387 [GPRS_SNS_ST_BSS_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001388 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001389 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001390 S(GPRS_SNS_ST_BSS_CONFIG_BSS) |
1391 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
1392 S(GPRS_SNS_ST_BSS_SIZE),
1393 .name = "BSS_CONFIG_BSS",
1394 .action = ns2_sns_st_bss_config_bss,
1395 .onenter = ns2_sns_st_bss_config_bss_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001396 },
Harald Welte694dad52021-03-23 15:22:16 +01001397 [GPRS_SNS_ST_BSS_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001398 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1399 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001400 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001401 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens6a161492020-07-12 13:45:50 +02001402 S(GPRS_SNS_ST_CONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001403 S(GPRS_SNS_ST_BSS_SIZE),
1404 .name = "BSS_CONFIG_SGSN",
1405 .action = ns2_sns_st_bss_config_sgsn,
1406 .onenter = ns2_sns_st_bss_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001407 },
1408 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001409 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1410 S(GPRS_SNS_EV_RX_DELETE) |
1411 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1412 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001413 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001414 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001415 .name = "CONFIGURED",
1416 .action = ns2_sns_st_configured,
1417 .onenter = ns2_sns_st_configured_onenter,
1418 },
1419};
1420
1421static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1422{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001423 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001424 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1425 struct gprs_ns2_inst *nsi = nse->nsi;
1426
Alexander Couzens90ee9632020-12-07 06:18:32 +01001427 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001428 switch (fi->T) {
1429 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001430 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1431 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001432 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001433 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001434 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 +01001435 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001436 break;
1437 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001438 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001439 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 +01001440 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001441 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001442 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS, nsi->timeout[NS_TOUT_TSNS_PROV], 2);
Alexander Couzensa367d082020-12-21 14:06:24 +01001443 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001444 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001445 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001446 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1447 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 +01001448 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001449 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001450 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 +01001451 }
1452 break;
1453 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001454 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 +01001455 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001456 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001457 }
1458 return 0;
1459}
1460
Harald Welte9e37bf42021-03-02 20:48:31 +01001461/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001462static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1463{
1464 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001465 struct ns2_sns_bind *sbind;
1466 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001467
Alexander Couzense769f522020-12-07 07:37:07 +01001468 switch (event) {
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001469 case GPRS_SNS_EV_REQ_ADD_BIND:
1470 sbind = data;
1471 switch (fi->state) {
1472 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001473 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001474 break;
Harald Welte694dad52021-03-23 15:22:16 +01001475 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001476 /* TODO: add the ip4 element to the list */
1477 break;
Harald Welte694dad52021-03-23 15:22:16 +01001478 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1479 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001480 case GPRS_SNS_ST_CONFIGURED:
1481 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1482 break;
1483 }
1484 break;
1485 case GPRS_SNS_EV_REQ_DELETE_BIND:
1486 sbind = data;
1487 switch (fi->state) {
1488 case GPRS_SNS_ST_UNCONFIGURED:
1489 break;
Harald Welte694dad52021-03-23 15:22:16 +01001490 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001491 /* TODO: remove the ip4 element from the list */
1492 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1493 if (nsvc->bind == sbind->bind) {
1494 gprs_ns2_free_nsvc(nsvc);
1495 }
1496 }
1497 break;
Harald Welte694dad52021-03-23 15:22:16 +01001498 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1499 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001500 case GPRS_SNS_ST_CONFIGURED:
1501 /* TODO: do an delete SNS-IP procedure */
1502 /* TODO: remove the ip4 element to the list */
1503 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1504 if (nsvc->bind == sbind->bind) {
1505 gprs_ns2_free_nsvc(nsvc);
1506 }
1507 }
1508 break;
1509 }
1510 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1511 talloc_free(sbind);
1512 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001513 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001514}
1515
Alexander Couzens31d52e12021-06-05 20:04:04 +02001516/* validate the bss configuration (sns endpoint and binds)
1517 * - no endpoints -> invalid
1518 * - no binds -> invalid
1519 * - only v4 sns endpoints, only v6 binds -> invalid
1520 * - only v4 sns endpoints, but v4 sig weights == 0 -> invalid ...
1521 */
1522static int ns2_sns_bss_valid_configuration(struct ns2_sns_state *gss)
1523{
1524 struct ns2_sns_bind *sbind;
1525 struct sns_endpoint *endpoint;
1526 const struct osmo_sockaddr *addr;
1527 int v4_sig = 0, v4_data = 0, v6_sig = 0, v6_data = 0;
1528 bool v4_endpoints = false;
1529 bool v6_endpoints = false;
1530
1531 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds))
1532 return 0;
1533
1534 llist_for_each_entry(sbind, &gss->binds, list) {
1535 addr = gprs_ns2_ip_bind_sockaddr(sbind->bind);
1536 if (!addr)
1537 continue;
1538 switch (addr->u.sa.sa_family) {
1539 case AF_INET:
1540 v4_sig += sbind->bind->sns_sig_weight;
1541 v4_data += sbind->bind->sns_data_weight;
1542 break;
1543 case AF_INET6:
1544 v6_sig += sbind->bind->sns_sig_weight;
1545 v6_data += sbind->bind->sns_data_weight;
1546 break;
1547 }
1548 }
1549
1550 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
1551 switch (endpoint->saddr.u.sa.sa_family) {
1552 case AF_INET:
1553 v4_endpoints = true;
1554 break;
1555 case AF_INET6:
1556 v6_endpoints = true;
1557 break;
1558 }
1559 }
1560
1561 return (v4_endpoints && v4_sig && v4_data) || (v6_endpoints && v6_sig && v6_data);
1562}
1563
Harald Welte9e37bf42021-03-02 20:48:31 +01001564/* allstate-action for BSS role */
1565static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1566{
1567 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1568 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1569
1570 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
1571 switch (event) {
1572 case GPRS_SNS_EV_REQ_NO_NSVC:
1573 /* ignore reselection running */
1574 if (gss->reselection_running)
1575 break;
1576
1577 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1578 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1579 break;
1580 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
1581 /* tear down previous state
1582 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1583 gss->reselection_running = true;
1584 gprs_ns2_free_nsvcs(nse);
Alexander Couzensd2c6c492021-06-06 01:57:52 +02001585 ns2_clear_elems(&gss->local);
1586 ns2_clear_elems(&gss->remote);
Harald Welte9e37bf42021-03-02 20:48:31 +01001587
1588 /* Choose the next sns endpoint. */
Alexander Couzens31d52e12021-06-05 20:04:04 +02001589 if (!ns2_sns_bss_valid_configuration(gss)) {
Harald Welte9e37bf42021-03-02 20:48:31 +01001590 gss->initial = NULL;
1591 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1592 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1593 return;
1594 } else if (!gss->initial) {
1595 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1596 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1597 /* last entry, continue with first */
1598 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1599 } else {
1600 /* next element is an entry */
1601 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1602 }
1603
1604 gss->reselection_running = false;
Harald Welte694dad52021-03-23 15:22:16 +01001605 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 +01001606 break;
1607 default:
1608 ns2_sns_st_all_action(fi, event, data);
1609 break;
1610 }
1611}
1612
Alexander Couzens6a161492020-07-12 13:45:50 +02001613static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1614 .name = "GPRS-NS2-SNS-BSS",
1615 .states = ns2_sns_bss_states,
1616 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001617 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1618 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001619 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1620 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001621 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001622 .cleanup = NULL,
1623 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001624 .event_names = gprs_sns_event_names,
1625 .pre_term = NULL,
1626 .log_subsys = DLNS,
1627};
1628
Harald Welte5bef2cc2020-09-18 22:33:24 +02001629/*! Allocate an IP-SNS FSM for the BSS side.
1630 * \param[in] nse NS Entity in which the FSM runs
1631 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001632 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001633struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1634 const char *id)
1635{
1636 struct osmo_fsm_inst *fi;
1637 struct ns2_sns_state *gss;
1638
1639 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1640 if (!fi)
1641 return fi;
1642
1643 gss = talloc_zero(fi, struct ns2_sns_state);
1644 if (!gss)
1645 goto err;
1646
1647 fi->priv = gss;
1648 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001649 gss->role = GPRS_SNS_ROLE_BSS;
Harald Welte24f4df52021-03-04 18:02:54 +01001650 /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */
1651 gss->num_max_ip4_remote = 8192;
1652 gss->num_max_ip6_remote = 8192;
Alexander Couzense769f522020-12-07 07:37:07 +01001653 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001654 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001655
1656 return fi;
1657err:
1658 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1659 return NULL;
1660}
1661
Harald Welte5bef2cc2020-09-18 22:33:24 +02001662/*! main entry point for receiving SNS messages from the network.
1663 * \param[in] nsvc NS-VC on which the message was received
1664 * \param[in] msg message buffer of the IP-SNS message
1665 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001666 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001667int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001668{
1669 struct gprs_ns2_nse *nse = nsvc->nse;
1670 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1671 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001672 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001673 struct osmo_fsm_inst *fi;
Alexander Couzens7619ed42021-03-24 17:44:03 +01001674 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001675
1676 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001677 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1678 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001679 rc = -EINVAL;
1680 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +02001681 }
1682
Alexander Couzens6a161492020-07-12 13:45:50 +02001683 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1684 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001685 gss = (struct ns2_sns_state *) fi->priv;
1686 if (!gss->sns_nsvc)
1687 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001688
Harald Weltef2949742021-01-20 14:54:14 +01001689 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1690 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1691
Alexander Couzens6a161492020-07-12 13:45:50 +02001692 switch (nsh->pdu_type) {
1693 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001694 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001695 break;
1696 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001697 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001698 break;
1699 case SNS_PDUT_CONFIG:
1700 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001701 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001702 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001703 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001704 break;
1705 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001706 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001707 break;
1708 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001709 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001710 break;
1711 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001712 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001713 break;
1714 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001715 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001716 break;
1717 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001718 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001719 break;
1720 default:
Harald Weltef2949742021-01-20 14:54:14 +01001721 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1722 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001723 rc = -EINVAL;
Alexander Couzens6a161492020-07-12 13:45:50 +02001724 }
1725
Alexander Couzens7619ed42021-03-24 17:44:03 +01001726out:
1727 msgb_free(msg);
1728
1729 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001730}
1731
1732#include <osmocom/vty/vty.h>
1733#include <osmocom/vty/misc.h>
1734
Harald Welte1262c4f2021-01-19 20:58:33 +01001735static 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 +02001736{
1737 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001738 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001739 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1740}
1741
Harald Welte1262c4f2021-01-19 20:58:33 +01001742static 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 +02001743{
1744 char ip_addr[INET6_ADDRSTRLEN] = {};
1745 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1746 strcpy(ip_addr, "Invalid IPv6");
1747
Harald Welte1262c4f2021-01-19 20:58:33 +01001748 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001749 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1750}
1751
Harald Welte5bef2cc2020-09-18 22:33:24 +02001752/*! Dump the IP-SNS state to a vty.
1753 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001754 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001755 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1756 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001757void 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 +02001758{
1759 struct ns2_sns_state *gss;
1760 unsigned int i;
1761
1762 if (!nse->bss_sns_fi)
1763 return;
1764
Harald Welte1262c4f2021-01-19 20:58:33 +01001765 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001766 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1767
Harald Welte1262c4f2021-01-19 20:58:33 +01001768 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1769 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001770
Alexander Couzens71128672021-06-05 18:44:01 +02001771 if (gss->local.num_ip4 && gss->remote.num_ip4) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001772 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001773 for (i = 0; i < gss->local.num_ip4; i++)
1774 vty_dump_sns_ip4(vty, prefix, &gss->local.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001775
Harald Welte1262c4f2021-01-19 20:58:33 +01001776 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001777 for (i = 0; i < gss->remote.num_ip4; i++)
1778 vty_dump_sns_ip4(vty, prefix, &gss->remote.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001779 }
1780
Alexander Couzens71128672021-06-05 18:44:01 +02001781 if (gss->local.num_ip6 && gss->remote.num_ip6) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001782 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001783 for (i = 0; i < gss->local.num_ip6; i++)
1784 vty_dump_sns_ip6(vty, prefix, &gss->local.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001785
Harald Welte1262c4f2021-01-19 20:58:33 +01001786 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001787 for (i = 0; i < gss->remote.num_ip6; i++)
1788 vty_dump_sns_ip6(vty, prefix, &gss->remote.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001789 }
1790}
1791
Alexander Couzens412bc342020-11-19 05:24:37 +01001792/*! write IP-SNS to a vty
1793 * \param[in] vty VTY to which the state shall be printed
1794 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001795void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001796{
1797 struct ns2_sns_state *gss;
1798 struct osmo_sockaddr_str addr_str;
1799 struct sns_endpoint *endpoint;
1800
1801 if (!nse->bss_sns_fi)
1802 return;
1803
1804 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1805 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001806 /* It's unlikely that an error happens, but let's better be safe. */
1807 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1808 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001809 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001810 }
1811}
1812
Alexander Couzense769f522020-12-07 07:37:07 +01001813static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1814 const struct osmo_sockaddr *saddr)
1815{
1816 struct sns_endpoint *endpoint;
1817
1818 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1819 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1820 return endpoint;
1821 }
1822
1823 return NULL;
1824}
1825
1826/*! gprs_ns2_sns_add_endpoint
1827 * \param[in] nse
1828 * \param[in] sockaddr
1829 * \return
1830 */
1831int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1832 const struct osmo_sockaddr *saddr)
1833{
1834 struct ns2_sns_state *gss;
1835 struct sns_endpoint *endpoint;
1836 bool do_selection = false;
1837
1838 if (nse->ll != GPRS_NS2_LL_UDP) {
1839 return -EINVAL;
1840 }
1841
Alexander Couzens138b96f2021-01-25 16:23:29 +01001842 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001843 return -EINVAL;
1844 }
1845
1846 gss = nse->bss_sns_fi->priv;
1847
1848 if (ns2_get_sns_endpoint(gss, saddr))
1849 return -EADDRINUSE;
1850
1851 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1852 if (!endpoint)
1853 return -ENOMEM;
1854
1855 endpoint->saddr = *saddr;
1856 if (llist_empty(&gss->sns_endpoints))
1857 do_selection = true;
1858
1859 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1860 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001861 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001862
1863 return 0;
1864}
1865
1866/*! gprs_ns2_sns_del_endpoint
1867 * \param[in] nse
1868 * \param[in] sockaddr
1869 * \return 0 on success, otherwise < 0
1870 */
1871int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1872 const struct osmo_sockaddr *saddr)
1873{
1874 struct ns2_sns_state *gss;
1875 struct sns_endpoint *endpoint;
1876
1877 if (nse->ll != GPRS_NS2_LL_UDP) {
1878 return -EINVAL;
1879 }
1880
Alexander Couzens138b96f2021-01-25 16:23:29 +01001881 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001882 return -EINVAL;
1883 }
1884
1885 gss = nse->bss_sns_fi->priv;
1886 endpoint = ns2_get_sns_endpoint(gss, saddr);
1887 if (!endpoint)
1888 return -ENOENT;
1889
1890 /* if this is an unused SNS endpoint it's done */
1891 if (gss->initial != endpoint) {
1892 llist_del(&endpoint->list);
1893 talloc_free(endpoint);
1894 return 0;
1895 }
1896
Alexander Couzens67725e22021-02-15 02:37:03 +01001897 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001898 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001899 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001900 "Closing all NS-VC and restart SNS-SIZE procedure"
1901 "with a remaining SNS endpoint.\n");
1902
1903 /* Continue with the next endpoint in the list.
1904 * Special case if the endpoint is at the start or end of the list */
1905 if (endpoint->list.prev == &gss->sns_endpoints ||
1906 endpoint->list.next == &gss->sns_endpoints)
1907 gss->initial = NULL;
1908 else
1909 gss->initial = llist_entry(endpoint->list.next->prev,
1910 struct sns_endpoint,
1911 list);
1912
1913 llist_del(&endpoint->list);
1914 gprs_ns2_free_nsvcs(nse);
1915 talloc_free(endpoint);
1916
1917 return 0;
1918}
1919
1920/*! gprs_ns2_sns_count
1921 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1922 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1923 */
1924int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1925{
1926 struct ns2_sns_state *gss;
1927 struct sns_endpoint *endpoint;
1928 int count = 0;
1929
1930 if (nse->ll != GPRS_NS2_LL_UDP) {
1931 return -EINVAL;
1932 }
1933
Alexander Couzens138b96f2021-01-25 16:23:29 +01001934 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001935 return -EINVAL;
1936 }
1937
1938 gss = nse->bss_sns_fi->priv;
1939 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1940 count++;
1941
1942 return count;
1943}
1944
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001945void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1946{
1947 struct ns2_sns_state *gss;
1948 struct gprs_ns2_vc *tmp;
1949
1950 if (!nse->bss_sns_fi)
1951 return;
1952
1953 gss = nse->bss_sns_fi->priv;
1954 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1955 return;
1956
1957 if (alive == gss->alive)
1958 return;
1959
1960 /* check if this is the current SNS NS-VC */
1961 if (nsvc == gss->sns_nsvc) {
1962 /* only replace the SNS NS-VC if there are other alive NS-VC.
1963 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1964 * and couldn't confirm yet if the NS-VC comes up */
1965 if (gss->alive && !alive)
1966 ns2_sns_replace_nsvc(nsvc);
1967 }
1968
1969 if (alive) {
1970 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001971 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001972 } else {
1973 /* is there at least another alive nsvc? */
1974 llist_for_each_entry(tmp, &nse->nsvc, list) {
1975 if (ns2_vc_is_unblocked(tmp))
1976 return;
1977 }
1978
1979 /* all NS-VC have failed */
1980 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001981 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001982 }
1983}
1984
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001985int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1986 struct gprs_ns2_vc_bind *bind)
1987{
1988 struct ns2_sns_state *gss;
1989 struct ns2_sns_bind *tmp;
1990
1991 OSMO_ASSERT(nse->bss_sns_fi);
1992 gss = nse->bss_sns_fi->priv;
1993
1994 if (!gprs_ns2_is_ip_bind(bind)) {
1995 return -EINVAL;
1996 }
1997
1998 if (!llist_empty(&gss->binds)) {
1999 llist_for_each_entry(tmp, &gss->binds, list) {
2000 if (tmp->bind == bind)
2001 return -EALREADY;
2002 }
2003 }
2004
2005 tmp = talloc_zero(gss, struct ns2_sns_bind);
2006 if (!tmp)
2007 return -ENOMEM;
2008 tmp->bind = bind;
2009 llist_add_tail(&tmp->list, &gss->binds);
2010
2011 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
2012 return 0;
2013}
2014
2015/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
2016int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
2017 struct gprs_ns2_vc_bind *bind)
2018{
2019 struct ns2_sns_state *gss;
2020 struct ns2_sns_bind *tmp, *tmp2;
2021 bool found = false;
2022
2023 if (!nse->bss_sns_fi)
2024 return -EINVAL;
2025
2026 gss = nse->bss_sns_fi->priv;
2027 if (gss->initial_bind && gss->initial_bind->bind == bind) {
2028 if (gss->initial_bind->list.prev == &gss->binds)
2029 gss->initial_bind = NULL;
2030 else
2031 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
2032 }
2033
2034 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
2035 if (tmp->bind == bind) {
2036 llist_del(&tmp->list);
2037 found = true;
Alexander Couzensa35c2962021-04-19 03:30:15 +02002038 break;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002039 }
2040 }
2041
2042 if (!found)
2043 return -ENOENT;
2044
2045 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
2046 return 0;
2047}
2048
Alexander Couzens71128672021-06-05 18:44:01 +02002049/* Update SNS weights for a bind (local endpoint).
2050 * \param[in] bind the bind which has been updated
Alexander Couzensc4704762021-02-08 23:13:12 +01002051 */
2052void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
2053{
2054 /* TODO: implement weights after binds per sns implemented */
2055}
2056
Harald Welte4f127462021-03-02 20:49:10 +01002057
2058
2059
2060/***********************************************************************
2061 * SGSN role
2062 ***********************************************************************/
2063
2064static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2065{
2066 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2067 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2068 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
2069}
2070
2071/* We're waiting for inbound SNS-CONFIG from the BSS */
2072static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2073{
2074 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2075 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2076 struct gprs_ns2_inst *nsi = nse->nsi;
2077 uint8_t cause;
2078 int rc;
2079
2080 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2081
2082 switch (event) {
2083 case GPRS_SNS_EV_RX_CONFIG:
2084 case GPRS_SNS_EV_RX_CONFIG_END:
2085 rc = ns_sns_append_remote_eps(fi, data);
2086 if (rc < 0) {
2087 cause = -rc;
2088 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2089 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2090 return;
2091 }
2092 /* only change state if last CONFIG was received */
2093 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
2094 /* ensure sum of data weight / sig weights is > 0 */
2095 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
2096 cause = NS_CAUSE_INVAL_WEIGH;
2097 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2098 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2099 break;
2100 }
2101 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2102 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2103 } else {
2104 /* just send CONFIG-ACK */
2105 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2106 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2107 }
2108 break;
2109 }
2110}
2111
2112static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2113{
2114 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2115 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2116
Harald Welte4f127462021-03-02 20:49:10 +01002117 /* transmit SGSN-oriented SNS-CONFIG */
Alexander Couzens71128672021-06-05 18:44:01 +02002118 ns2_tx_sns_config(gss->sns_nsvc, true, gss->local.ip4, gss->local.num_ip4,
2119 gss->local.ip6, gss->local.num_ip6);
Harald Welte4f127462021-03-02 20:49:10 +01002120}
2121
2122/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2123static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2124{
2125 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2126 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2127 struct tlv_parsed *tp = NULL;
2128
2129 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2130
2131 switch (event) {
2132 case GPRS_SNS_EV_RX_CONFIG_ACK:
2133 tp = data;
2134 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2135 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2136 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2137 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2138 break;
2139 }
2140 /* we currently only send one SNS-CONFIG with END FLAG */
2141 if (true) {
2142 create_missing_nsvcs(fi);
2143 /* start the test procedure on ALL NSVCs! */
2144 gprs_ns2_start_alive_all_nsvcs(nse);
2145 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2146 }
2147 break;
2148 }
2149}
2150
2151/* SGSN-side SNS state machine */
2152static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2153 [GPRS_SNS_ST_UNCONFIGURED] = {
2154 .in_event_mask = 0, /* handled by all_state_action */
2155 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2156 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2157 .name = "UNCONFIGURED",
2158 .action = ns2_sns_st_sgsn_unconfigured,
2159 },
2160 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
2161 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
2162 S(GPRS_SNS_EV_RX_CONFIG_END),
2163 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2164 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2165 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2166 .name = "SGSN_WAIT_CONFIG",
2167 .action = ns2_sns_st_sgsn_wait_config,
2168 },
2169 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
2170 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
2171 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2172 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2173 S(GPRS_SNS_ST_CONFIGURED),
2174 .name = "SGSN_WAIT_CONFIG_ACK",
2175 .action = ns2_sns_st_sgsn_wait_config_ack,
2176 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2177 },
2178 [GPRS_SNS_ST_CONFIGURED] = {
2179 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
2180 S(GPRS_SNS_EV_RX_DELETE) |
2181 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
2182 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
2183 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2184 .name = "CONFIGURED",
2185 /* shared with BSS side; once configured there's no difference */
2186 .action = ns2_sns_st_configured,
2187 .onenter = ns2_sns_st_configured_onenter,
2188 },
2189};
2190
2191static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2192{
2193 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2194 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2195 struct gprs_ns2_inst *nsi = nse->nsi;
2196
2197 gss->N++;
2198 switch (fi->T) {
2199 case 3:
2200 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2201 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2202 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2203 } else {
2204 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2205 }
2206 break;
2207 case 4:
2208 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2209 break;
2210 }
2211 return 0;
2212}
2213
2214
2215/* allstate-action for SGSN role */
2216static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2217{
2218 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2219 struct tlv_parsed *tp = NULL;
Harald Welte01fa6a32021-03-04 19:49:38 +01002220 size_t num_local_eps, num_remote_eps;
Harald Welte4f127462021-03-02 20:49:10 +01002221 uint8_t flag;
Harald Weltea2c5af52021-03-04 17:59:35 +01002222 uint8_t cause;
Harald Welte4f127462021-03-02 20:49:10 +01002223
2224 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2225
2226 switch (event) {
2227 case GPRS_SNS_EV_RX_SIZE:
2228 tp = (struct tlv_parsed *) data;
Harald Weltea2c5af52021-03-04 17:59:35 +01002229 /* check for mandatory / conditional IEs */
2230 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) ||
2231 !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) {
2232 cause = NS_CAUSE_MISSING_ESSENT_IE;
2233 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2234 break;
2235 }
2236 if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) &&
2237 !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) {
2238 cause = NS_CAUSE_MISSING_ESSENT_IE;
Harald Welte4f127462021-03-02 20:49:10 +01002239 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2240 break;
2241 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002242 if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2))
2243 gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR);
2244 if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2))
2245 gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR);
2246 /* decide if we go for IPv4 or IPv6 */
2247 if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, IPv6)) {
2248 gss->ip = IPv6;
Harald Welte2d807b62021-03-24 01:57:30 +01002249 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002250 num_local_eps = gss->local.num_ip6;
Harald Welte01fa6a32021-03-04 19:49:38 +01002251 num_remote_eps = gss->num_max_ip6_remote;
2252 } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, IPv4)) {
2253 gss->ip = IPv4;
Harald Welte2d807b62021-03-24 01:57:30 +01002254 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002255 num_local_eps = gss->local.num_ip4;
Harald Welte01fa6a32021-03-04 19:49:38 +01002256 num_remote_eps = gss->num_max_ip4_remote;
2257 } else {
Alexander Couzens71128672021-06-05 18:44:01 +02002258 if (gss->local.num_ip4 && !gss->num_max_ip4_remote)
Harald Welte01fa6a32021-03-04 19:49:38 +01002259 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
2260 else
2261 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
2262 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2263 break;
2264 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002265 /* ensure number of NS-VCs is sufficient for full mesh */
2266 gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC);
2267 if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) {
2268 LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, "
2269 "but BSS supports only %zu maximum NS-VCs\n", num_local_eps,
2270 num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs);
2271 cause = NS_CAUSE_INVAL_NR_NS_VC;
2272 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2273 break;
2274 }
2275 /* perform state reset, if requested */
Harald Welte4f127462021-03-02 20:49:10 +01002276 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2277 if (flag & 1) {
2278 struct gprs_ns2_vc *nsvc, *nsvc2;
2279 /* clear all state */
Harald Welte46eb7642021-03-04 17:49:59 +01002280 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
Harald Welte4f127462021-03-02 20:49:10 +01002281 gss->N = 0;
Alexander Couzensd2c6c492021-06-06 01:57:52 +02002282 ns2_clear_elems(&gss->local);
2283 ns2_clear_elems(&gss->remote);
Harald Welte4f127462021-03-02 20:49:10 +01002284 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2285 if (nsvc == gss->sns_nsvc) {
2286 /* keep the NSVC we need for SNS, but unconfigure it */
2287 nsvc->sig_weight = 0;
2288 nsvc->data_weight = 0;
2289 ns2_vc_force_unconfigured(nsvc);
2290 } else {
2291 /* free all other NS-VCs */
2292 gprs_ns2_free_nsvc(nsvc);
2293 }
2294 }
Harald Welte2d807b62021-03-24 01:57:30 +01002295 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte4f127462021-03-02 20:49:10 +01002296 }
2297 /* send SIZE_ACK */
2298 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2299 /* only wait for SNS-CONFIG in case of Reset flag */
2300 if (flag & 1)
2301 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2302 break;
2303 default:
2304 ns2_sns_st_all_action(fi, event, data);
2305 break;
2306 }
2307}
2308
2309static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2310 .name = "GPRS-NS2-SNS-SGSN",
2311 .states = ns2_sns_sgsn_states,
2312 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
2313 .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) |
2314 S(GPRS_SNS_EV_REQ_NO_NSVC) |
2315 S(GPRS_SNS_EV_REQ_ADD_BIND) |
2316 S(GPRS_SNS_EV_REQ_DELETE_BIND),
2317 .allstate_action = ns2_sns_st_all_action_sgsn,
2318 .cleanup = NULL,
2319 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2320 .event_names = gprs_sns_event_names,
2321 .pre_term = NULL,
2322 .log_subsys = DLNS,
2323};
2324
2325/*! Allocate an IP-SNS FSM for the SGSN side.
2326 * \param[in] nse NS Entity in which the FSM runs
2327 * \param[in] id string identifier
2328 * \returns FSM instance on success; NULL on error */
2329struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2330{
2331 struct osmo_fsm_inst *fi;
2332 struct ns2_sns_state *gss;
2333
2334 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2335 if (!fi)
2336 return fi;
2337
2338 gss = talloc_zero(fi, struct ns2_sns_state);
2339 if (!gss)
2340 goto err;
2341
2342 fi->priv = gss;
2343 gss->nse = nse;
2344 gss->role = GPRS_SNS_ROLE_SGSN;
2345 INIT_LLIST_HEAD(&gss->sns_endpoints);
2346 INIT_LLIST_HEAD(&gss->binds);
2347
2348 return fi;
2349err:
2350 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2351 return NULL;
2352}
2353
2354
2355
2356
Alexander Couzens6a161492020-07-12 13:45:50 +02002357/* initialize osmo_ctx on main tread */
2358static __attribute__((constructor)) void on_dso_load_ctx(void)
2359{
2360 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002361 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002362}