blob: 66dd421b31de38fe43aefb2f664693929586bf4c [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
Harald Welte46eb7642021-03-04 17:49:59 +0100298static void ns2_clear_ipv46_entries_local(struct ns2_sns_state *gss)
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100299{
Alexander Couzens71128672021-06-05 18:44:01 +0200300 TALLOC_FREE(gss->local.ip4);
301 TALLOC_FREE(gss->local.ip6);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100302
Alexander Couzens71128672021-06-05 18:44:01 +0200303 gss->local.num_ip4 = 0;
304 gss->local.num_ip6 = 0;
Harald Welte46eb7642021-03-04 17:49:59 +0100305}
306
307static void ns2_clear_ipv46_entries_remote(struct ns2_sns_state *gss)
308{
Alexander Couzens71128672021-06-05 18:44:01 +0200309 TALLOC_FREE(gss->remote.ip4);
310 TALLOC_FREE(gss->remote.ip6);
Harald Welte46eb7642021-03-04 17:49:59 +0100311
Alexander Couzens71128672021-06-05 18:44:01 +0200312 gss->remote.num_ip4 = 0;
313 gss->remote.num_ip6 = 0;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100314}
315
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100316static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
317 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200318{
319 struct gprs_ns2_inst *nsi = nse->nsi;
320 struct gprs_ns2_vc *nsvc;
321 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100322
323 /* for every bind, create a connection if bind type == IP */
324 llist_for_each_entry(bind, &nsi->binding, list) {
325 if (bind->ll != GPRS_NS2_LL_UDP)
326 continue;
327 /* ignore failed connection */
328 nsvc = gprs_ns2_ip_connect_inactive(bind,
329 remote,
330 nse, 0);
331 if (!nsvc) {
332 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
333 continue;
334 }
335
336 nsvc->sig_weight = sig_weight;
337 nsvc->data_weight = data_weight;
338 }
339}
340
341static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
342 struct gprs_ns2_nse *nse,
343 const struct gprs_ns_ie_ip4_elem *ip4)
344{
Alexander Couzensc068d862020-10-12 04:11:51 +0200345 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200346 /* copy over. Both data structures use network byte order */
347 remote.u.sin.sin_family = AF_INET;
348 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
349 remote.u.sin.sin_port = ip4->udp_port;
350
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100351 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200352}
353
354static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
355 struct gprs_ns2_nse *nse,
356 const struct gprs_ns_ie_ip6_elem *ip6)
357{
Alexander Couzens6a161492020-07-12 13:45:50 +0200358 struct osmo_sockaddr remote = {};
359 /* copy over. Both data structures use network byte order */
360 remote.u.sin6.sin6_family = AF_INET6;
361 remote.u.sin6.sin6_addr = ip6->ip_addr;
362 remote.u.sin6.sin6_port = ip6->udp_port;
363
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100364 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200365}
366
Harald Weltee8c61062021-03-24 13:16:27 +0100367static struct gprs_ns2_vc *nsvc_for_bind_and_remote(struct gprs_ns2_nse *nse,
368 struct gprs_ns2_vc_bind *bind,
369 const struct osmo_sockaddr *remote)
370{
371 struct gprs_ns2_vc *nsvc;
372
373 llist_for_each_entry(nsvc, &nse->nsvc, list) {
374 if (nsvc->bind != bind)
375 continue;
376
377 if (!osmo_sockaddr_cmp(remote, gprs_ns2_ip_vc_remote(nsvc)))
378 return nsvc;
379 }
380 return NULL;
381}
Alexander Couzens6a161492020-07-12 13:45:50 +0200382
383static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
384{
385 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
386 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
387 struct gprs_ns2_vc *nsvc;
Harald Welte3053bbb2021-03-24 13:22:18 +0100388 struct ns2_sns_bind *sbind;
Alexander Couzens6a161492020-07-12 13:45:50 +0200389 struct osmo_sockaddr remote = { };
390 unsigned int i;
391
Harald Weltee8c61062021-03-24 13:16:27 +0100392 /* iterate over all remote IPv4 endpoints */
Alexander Couzens71128672021-06-05 18:44:01 +0200393 for (i = 0; i < gss->remote.num_ip4; i++) {
394 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->remote.ip4[i];
Alexander Couzens6a161492020-07-12 13:45:50 +0200395
396 remote.u.sin.sin_family = AF_INET;
397 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
398 remote.u.sin.sin_port = ip4->udp_port;
399
Harald Welte3053bbb2021-03-24 13:22:18 +0100400 /* iterate over all local binds within this SNS */
401 llist_for_each_entry(sbind, &gss->binds, list) {
402 struct gprs_ns2_vc_bind *bind = sbind->bind;
403
Harald Weltee8c61062021-03-24 13:16:27 +0100404 /* we only care about UDP binds */
Daniel Willmann967e2c12021-01-14 16:58:17 +0100405 if (bind->ll != GPRS_NS2_LL_UDP)
406 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200407
Harald Weltee8c61062021-03-24 13:16:27 +0100408 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
409 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200410 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
411 if (!nsvc) {
412 /* TODO: add to a list to send back a NS-STATUS */
413 continue;
414 }
415 }
416
417 /* update data / signalling weight */
418 nsvc->data_weight = ip4->data_weight;
419 nsvc->sig_weight = ip4->sig_weight;
420 nsvc->sns_only = false;
421 }
422 }
423
Harald Weltee8c61062021-03-24 13:16:27 +0100424 /* iterate over all remote IPv4 endpoints */
Alexander Couzens71128672021-06-05 18:44:01 +0200425 for (i = 0; i < gss->remote.num_ip6; i++) {
426 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->remote.ip6[i];
Alexander Couzens6a161492020-07-12 13:45:50 +0200427
428 remote.u.sin6.sin6_family = AF_INET6;
429 remote.u.sin6.sin6_addr = ip6->ip_addr;
430 remote.u.sin6.sin6_port = ip6->udp_port;
431
Harald Welte3053bbb2021-03-24 13:22:18 +0100432 /* iterate over all local binds within this SNS */
433 llist_for_each_entry(sbind, &gss->binds, list) {
434 struct gprs_ns2_vc_bind *bind = sbind->bind;
435
Daniel Willmann967e2c12021-01-14 16:58:17 +0100436 if (bind->ll != GPRS_NS2_LL_UDP)
437 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200438
Harald Weltee8c61062021-03-24 13:16:27 +0100439 /* we only care about UDP binds */
440 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
441 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200442 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
443 if (!nsvc) {
444 /* TODO: add to a list to send back a NS-STATUS */
445 continue;
446 }
447 }
448
449 /* update data / signalling weight */
450 nsvc->data_weight = ip6->data_weight;
451 nsvc->sig_weight = ip6->sig_weight;
452 nsvc->sns_only = false;
453 }
454 }
455
456
457 return 0;
458}
459
460/* Add a given remote IPv4 element to gprs_sns_state */
461static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
462{
463 unsigned int i;
464
Alexander Couzens71128672021-06-05 18:44:01 +0200465 if (gss->remote.num_ip4 >= gss->num_max_ip4_remote)
Alexander Couzens6a161492020-07-12 13:45:50 +0200466 return -NS_CAUSE_INVAL_NR_NS_VC;
467
468 /* check for duplicates */
Alexander Couzens71128672021-06-05 18:44:01 +0200469 for (i = 0; i < gss->remote.num_ip4; i++) {
470 if (memcmp(&gss->remote.ip4[i], ip4, sizeof(*ip4)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200471 continue;
472 /* TODO: log message duplicate */
Alexander Couzens6a161492020-07-12 13:45:50 +0200473 return -NS_CAUSE_PROTO_ERR_UNSPEC;
474 }
475
Alexander Couzens71128672021-06-05 18:44:01 +0200476 gss->remote.ip4 = talloc_realloc(gss, gss->remote.ip4, struct gprs_ns_ie_ip4_elem,
477 gss->remote.num_ip4+1);
478 gss->remote.ip4[gss->remote.num_ip4] = *ip4;
479 gss->remote.num_ip4 += 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200480 return 0;
481}
482
483/* Remove a given remote IPv4 element from gprs_sns_state */
484static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
485{
486 unsigned int i;
487
Alexander Couzens71128672021-06-05 18:44:01 +0200488 for (i = 0; i < gss->remote.num_ip4; i++) {
489 if (memcmp(&gss->remote.ip4[i], ip4, sizeof(*ip4)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200490 continue;
491 /* all array elements < i remain as they are; all > i are shifted left by one */
Alexander Couzens71128672021-06-05 18:44:01 +0200492 memmove(&gss->remote.ip4[i], &gss->remote.ip4[i+1], gss->remote.num_ip4-i-1);
493 gss->remote.num_ip4 -= 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200494 return 0;
495 }
496 return -1;
497}
498
499/* update the weights for specified remote IPv4 */
500static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
501{
502 unsigned int i;
503
Alexander Couzens71128672021-06-05 18:44:01 +0200504 for (i = 0; i < gss->remote.num_ip4; i++) {
505 if (gss->remote.ip4[i].ip_addr != ip4->ip_addr ||
506 gss->remote.ip4[i].udp_port != ip4->udp_port)
Alexander Couzens6a161492020-07-12 13:45:50 +0200507 continue;
508
Alexander Couzens71128672021-06-05 18:44:01 +0200509 gss->remote.ip4[i].sig_weight = ip4->sig_weight;
510 gss->remote.ip4[i].data_weight = ip4->data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200511 return 0;
512 }
513 return -1;
514}
515
516/* Add a given remote IPv6 element to gprs_sns_state */
517static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
518{
Alexander Couzens71128672021-06-05 18:44:01 +0200519 if (gss->remote.num_ip6 >= gss->num_max_ip6_remote)
Alexander Couzens6a161492020-07-12 13:45:50 +0200520 return -NS_CAUSE_INVAL_NR_NS_VC;
521
Alexander Couzens71128672021-06-05 18:44:01 +0200522 gss->remote.ip6 = talloc_realloc(gss, gss->remote.ip6, struct gprs_ns_ie_ip6_elem,
523 gss->remote.num_ip6+1);
524 gss->remote.ip6[gss->remote.num_ip6] = *ip6;
525 gss->remote.num_ip6 += 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200526 return 0;
527}
528
529/* Remove a given remote IPv6 element from gprs_sns_state */
530static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
531{
532 unsigned int i;
533
Alexander Couzens71128672021-06-05 18:44:01 +0200534 for (i = 0; i < gss->remote.num_ip6; i++) {
535 if (memcmp(&gss->remote.ip6[i], ip6, sizeof(*ip6)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200536 continue;
537 /* all array elements < i remain as they are; all > i are shifted left by one */
Alexander Couzens71128672021-06-05 18:44:01 +0200538 memmove(&gss->remote.ip6[i], &gss->remote.ip6[i+1], gss->remote.num_ip6-i-1);
539 gss->remote.num_ip6 -= 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200540 return 0;
541 }
542 return -1;
543}
544
545/* update the weights for specified remote IPv6 */
546static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
547{
548 unsigned int i;
549
Alexander Couzens71128672021-06-05 18:44:01 +0200550 for (i = 0; i < gss->remote.num_ip6; i++) {
551 if (memcmp(&gss->remote.ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
552 gss->remote.ip6[i].udp_port != ip6->udp_port)
Alexander Couzens6a161492020-07-12 13:45:50 +0200553 continue;
Alexander Couzens71128672021-06-05 18:44:01 +0200554 gss->remote.ip6[i].sig_weight = ip6->sig_weight;
555 gss->remote.ip6[i].data_weight = ip6->data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200556 return 0;
557 }
558 return -1;
559}
560
561static 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)
562{
563 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
564 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
565 struct gprs_ns2_vc *nsvc;
566 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200567 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200568 uint8_t new_signal;
569 uint8_t new_data;
570
571 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
572 * signalling weights of all the peer IP endpoints configured for this NSE is
573 * equal to zero or if the resulting sum of the data weights of all the peer IP
574 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
575 * SNS-ACK PDU with a cause code of "Invalid weights". */
576
577 if (ip4) {
578 if (update_remote_ip4_elem(gss, ip4))
579 return -NS_CAUSE_UNKN_IP_EP;
580
581 /* copy over. Both data structures use network byte order */
582 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
583 sa.u.sin.sin_port = ip4->udp_port;
584 sa.u.sin.sin_family = AF_INET;
585 new_signal = ip4->sig_weight;
586 new_data = ip4->data_weight;
587 } else if (ip6) {
588 if (update_remote_ip6_elem(gss, ip6))
589 return -NS_CAUSE_UNKN_IP_EP;
590
591 /* copy over. Both data structures use network byte order */
592 sa.u.sin6.sin6_addr = ip6->ip_addr;
593 sa.u.sin6.sin6_port = ip6->udp_port;
594 sa.u.sin6.sin6_family = AF_INET6;
595 new_signal = ip6->sig_weight;
596 new_data = ip6->data_weight;
597 } else {
598 OSMO_ASSERT(false);
599 }
600
601 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200602 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200603 /* all nsvc in NSE should be IP/UDP nsvc */
604 OSMO_ASSERT(remote);
605
606 if (osmo_sockaddr_cmp(&sa, remote))
607 continue;
608
609 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
610 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
611 nsvc->sig_weight, new_signal);
612
613 nsvc->data_weight = new_data;
614 nsvc->sig_weight = new_signal;
615 }
616
617 return 0;
618}
619
620static int do_sns_delete(struct osmo_fsm_inst *fi,
621 const struct gprs_ns_ie_ip4_elem *ip4,
622 const struct gprs_ns_ie_ip6_elem *ip6)
623{
624 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
625 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
626 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200627 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200628 struct osmo_sockaddr sa = {};
629
630 if (ip4) {
631 if (remove_remote_ip4_elem(gss, ip4) < 0)
632 return -NS_CAUSE_UNKN_IP_EP;
633 /* copy over. Both data structures use network byte order */
634 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
635 sa.u.sin.sin_port = ip4->udp_port;
636 sa.u.sin.sin_family = AF_INET;
637 } else if (ip6) {
638 if (remove_remote_ip6_elem(gss, ip6))
639 return -NS_CAUSE_UNKN_IP_EP;
640
641 /* copy over. Both data structures use network byte order */
642 sa.u.sin6.sin6_addr = ip6->ip_addr;
643 sa.u.sin6.sin6_port = ip6->udp_port;
644 sa.u.sin6.sin6_family = AF_INET6;
645 } else {
646 OSMO_ASSERT(false);
647 }
648
649 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200650 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200651 /* all nsvc in NSE should be IP/UDP nsvc */
652 OSMO_ASSERT(remote);
653 if (osmo_sockaddr_cmp(&sa, remote))
654 continue;
655
656 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
657 gprs_ns2_free_nsvc(nsvc);
658 }
659
660 return 0;
661}
662
663static int do_sns_add(struct osmo_fsm_inst *fi,
664 const struct gprs_ns_ie_ip4_elem *ip4,
665 const struct gprs_ns_ie_ip6_elem *ip6)
666{
667 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
668 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
669 struct gprs_ns2_vc *nsvc;
670 int rc = 0;
671
672 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
673 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
674 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
675 switch (gss->ip) {
676 case IPv4:
677 rc = add_remote_ip4_elem(gss, ip4);
678 break;
679 case IPv6:
680 rc = add_remote_ip6_elem(gss, ip6);
681 break;
682 default:
683 /* the gss->ip is initialized with the bss */
684 OSMO_ASSERT(false);
685 }
686
687 if (rc)
688 return rc;
689
690 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
691 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
692 * unspecified" */
693 switch (gss->ip) {
694 case IPv4:
695 nsvc = nsvc_by_ip4_elem(nse, ip4);
696 if (nsvc) {
697 /* the nsvc should be already in sync with the ip4 / ip6 elements */
698 return -NS_CAUSE_PROTO_ERR_UNSPEC;
699 }
700
701 /* TODO: failure case */
702 ns2_nsvc_create_ip4(fi, nse, ip4);
703 break;
704 case IPv6:
705 nsvc = nsvc_by_ip6_elem(nse, ip6);
706 if (nsvc) {
707 /* the nsvc should be already in sync with the ip4 / ip6 elements */
708 return -NS_CAUSE_PROTO_ERR_UNSPEC;
709 }
710
711 /* TODO: failure case */
712 ns2_nsvc_create_ip6(fi, nse, ip6);
713 break;
714 }
715
716 gprs_ns2_start_alive_all_nsvcs(nse);
717
718 return 0;
719}
720
721
Harald Welte694dad52021-03-23 15:22:16 +0100722static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200723{
Harald Weltef61a9152021-03-02 22:20:17 +0100724 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
725 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzense769f522020-12-07 07:37:07 +0100726 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200727}
728
Harald Welte694dad52021-03-23 15:22:16 +0100729static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200730{
Harald Weltef61a9152021-03-02 22:20:17 +0100731 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200732 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
733 struct gprs_ns2_inst *nsi = nse->nsi;
734 struct tlv_parsed *tp = NULL;
735
Harald Weltef61a9152021-03-02 22:20:17 +0100736 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
737
Alexander Couzens6a161492020-07-12 13:45:50 +0200738 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100739 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200740 tp = data;
741 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
742 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
743 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
744 /* TODO: What to do? */
745 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100746 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200747 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
748 }
749 break;
750 default:
751 OSMO_ASSERT(0);
752 }
753}
754
Harald Welte01fa6a32021-03-04 19:49:38 +0100755static int ns2_sns_count_num_local_ep(struct osmo_fsm_inst *fi, enum ns2_sns_type stype)
756{
757 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
758 struct ns2_sns_bind *sbind;
759 int count = 0;
760
761 llist_for_each_entry(sbind, &gss->binds, list) {
762 const struct osmo_sockaddr *sa = gprs_ns2_ip_bind_sockaddr(sbind->bind);
763 if (!sa)
764 continue;
765
766 switch (stype) {
767 case IPv4:
768 if (sa->u.sas.ss_family == AF_INET)
769 count++;
770 break;
771 case IPv6:
772 if (sa->u.sas.ss_family == AF_INET6)
773 count++;
774 break;
775 }
776 }
777 return count;
778}
779
Harald Welte24920e22021-03-04 13:03:27 +0100780static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200781{
782 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100783 struct gprs_ns_ie_ip4_elem *ip4_elems;
784 struct gprs_ns_ie_ip6_elem *ip6_elems;
785 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100786 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100787 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100788 const struct osmo_sockaddr *sa;
789 struct osmo_sockaddr local;
790 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200791
Harald Welte46eb7642021-03-04 17:49:59 +0100792 ns2_clear_ipv46_entries_local(gss);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100793
Alexander Couzense769f522020-12-07 07:37:07 +0100794 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100795 if (gss->role == GPRS_SNS_ROLE_BSS) {
796 if (!gss->initial)
797 return;
798 remote = &gss->initial->saddr;
799 } else
800 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100801
802 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100803 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100804 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100805 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100806 return;
807 }
808
Alexander Couzense769f522020-12-07 07:37:07 +0100809 switch (gss->ip) {
810 case IPv4:
Alexander Couzens71128672021-06-05 18:44:01 +0200811 ip4_elems = talloc_realloc(fi, gss->local.ip4, struct gprs_ns_ie_ip4_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100812 if (!ip4_elems)
813 return;
814
Alexander Couzens71128672021-06-05 18:44:01 +0200815 gss->local.ip4 = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100816 llist_for_each_entry(sbind, &gss->binds, list) {
817 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100818 sa = gprs_ns2_ip_bind_sockaddr(bind);
819 if (!sa)
820 continue;
821
822 if (sa->u.sas.ss_family != AF_INET)
823 continue;
824
825 /* check if this is an specific bind */
826 if (sa->u.sin.sin_addr.s_addr == 0) {
827 if (osmo_sockaddr_local_ip(&local, remote))
828 continue;
829
830 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
831 } else {
832 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
833 }
834
835 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100836 ip4_elems->sig_weight = bind->sns_sig_weight;
837 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100838 ip4_elems++;
839 }
840
Alexander Couzens71128672021-06-05 18:44:01 +0200841 gss->local.num_ip4 = count;
842 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->local.num_ip4, 8);
Alexander Couzense769f522020-12-07 07:37:07 +0100843 break;
844 case IPv6:
845 /* IPv6 */
Alexander Couzens71128672021-06-05 18:44:01 +0200846 ip6_elems = talloc_realloc(fi, gss->local.ip6, struct gprs_ns_ie_ip6_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100847 if (!ip6_elems)
848 return;
849
Alexander Couzens71128672021-06-05 18:44:01 +0200850 gss->local.ip6 = ip6_elems;
Alexander Couzense769f522020-12-07 07:37:07 +0100851
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100852 llist_for_each_entry(sbind, &gss->binds, list) {
853 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100854 sa = gprs_ns2_ip_bind_sockaddr(bind);
855 if (!sa)
856 continue;
857
858 if (sa->u.sas.ss_family != AF_INET6)
859 continue;
860
861 /* check if this is an specific bind */
862 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
863 if (osmo_sockaddr_local_ip(&local, remote))
864 continue;
865
866 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
867 } else {
868 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
869 }
870
871 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100872 ip6_elems->sig_weight = bind->sns_sig_weight;
873 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100874
875 ip6_elems++;
876 }
Alexander Couzens71128672021-06-05 18:44:01 +0200877 gss->local.num_ip6 = count;
878 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->local.num_ip6, 8);
Alexander Couzense769f522020-12-07 07:37:07 +0100879 break;
880 }
Harald Welte24920e22021-03-04 13:03:27 +0100881}
882
Alexander Couzens6608ce92021-04-26 20:39:46 +0200883static void ns2_sns_choose_next_bind(struct ns2_sns_state *gss)
884{
885 /* take the first bind or take the next bind */
886 if (!gss->initial_bind || gss->initial_bind->list.next == &gss->binds)
887 gss->initial_bind = llist_first_entry_or_null(&gss->binds, struct ns2_sns_bind, list);
888 else
889 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
890}
891
Harald Welte24920e22021-03-04 13:03:27 +0100892/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Harald Welte694dad52021-03-23 15:22:16 +0100893static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Harald Welte24920e22021-03-04 13:03:27 +0100894{
895 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
896
Harald Weltef61a9152021-03-02 22:20:17 +0100897 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
898
Harald Welte24920e22021-03-04 13:03:27 +0100899 /* on a generic failure, the timer callback will recover */
900 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
901 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Harald Welte694dad52021-03-23 15:22:16 +0100902 if (old_state != GPRS_SNS_ST_BSS_SIZE)
Harald Welte24920e22021-03-04 13:03:27 +0100903 gss->N = 0;
904
905 gss->alive = false;
906
907 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens6608ce92021-04-26 20:39:46 +0200908 ns2_sns_choose_next_bind(gss);
Harald Welte24920e22021-03-04 13:03:27 +0100909
910 /* setup the NSVC */
911 if (!gss->sns_nsvc) {
912 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
913 struct osmo_sockaddr *remote = &gss->initial->saddr;
914 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
915 if (!gss->sns_nsvc)
916 return;
Harald Weltec962a2e2021-03-05 08:09:08 +0100917 /* A pre-configured endpoint shall not be used for NSE data or signalling traffic
918 * (with the exception of Size and Configuration procedures) unless it is configured
919 * by the SGSN using the auto-configuration procedures */
Harald Welte24920e22021-03-04 13:03:27 +0100920 gss->sns_nsvc->sns_only = true;
921 }
922
Alexander Couzens6a161492020-07-12 13:45:50 +0200923 if (gss->num_max_ip4_remote > 0)
Alexander Couzens71128672021-06-05 18:44:01 +0200924 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->local.num_ip4, -1);
Alexander Couzens6a161492020-07-12 13:45:50 +0200925 else
Alexander Couzens71128672021-06-05 18:44:01 +0200926 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->local.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200927}
928
Harald Welte694dad52021-03-23 15:22:16 +0100929static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200930{
Harald Weltef61a9152021-03-02 22:20:17 +0100931 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens3df58862021-02-05 17:18:08 +0100932 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Harald Weltef61a9152021-03-02 22:20:17 +0100933 struct tlv_parsed *tp = NULL;
934
935 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzens6a161492020-07-12 13:45:50 +0200936
937 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100938 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200939 tp = (struct tlv_parsed *) data;
940 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
941 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
942 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
943 /* TODO: What to do? */
944 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100945 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 +0200946 }
947 break;
948 default:
949 OSMO_ASSERT(0);
950 }
951}
952
Harald Welte694dad52021-03-23 15:22:16 +0100953static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200954{
955 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100956
Harald Weltef61a9152021-03-02 22:20:17 +0100957 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
958
Harald Welte694dad52021-03-23 15:22:16 +0100959 if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS)
Alexander Couzens790a9632021-02-05 17:18:39 +0100960 gss->N = 0;
961
Alexander Couzens6a161492020-07-12 13:45:50 +0200962 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200963 switch (gss->ip) {
964 case IPv4:
965 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzens71128672021-06-05 18:44:01 +0200966 gss->local.ip4, gss->local.num_ip4,
Alexander Couzense78207f2020-12-07 06:19:29 +0100967 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200968 break;
969 case IPv6:
970 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100971 NULL, 0,
Alexander Couzens71128672021-06-05 18:44:01 +0200972 gss->local.ip6, gss->local.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200973 break;
974 }
975}
976
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100977/* calculate the timeout of the configured state. the configured
978 * state will fail if not at least one NS-VC is alive within X second.
979 */
980static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
981{
982 int secs;
983 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
984 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
985 secs += nsi->timeout[NS_TOUT_TNS_TEST];
986
987 return secs;
988}
Alexander Couzens6a161492020-07-12 13:45:50 +0200989
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100990/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
991static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200992{
993 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200994
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100995 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
996 const struct gprs_ns_ie_ip4_elem *v4_list;
997 unsigned int num_v4;
998 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
999 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +02001000
Alexander Couzens71128672021-06-05 18:44:01 +02001001 if (num_v4 && gss->remote.ip6)
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001002 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +02001003
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001004 /* realloc to the new size */
Alexander Couzens71128672021-06-05 18:44:01 +02001005 gss->remote.ip4 = talloc_realloc(gss, gss->remote.ip4,
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001006 struct gprs_ns_ie_ip4_elem,
Alexander Couzens71128672021-06-05 18:44:01 +02001007 gss->remote.num_ip4 + num_v4);
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001008 /* append the new entries to the end of the list */
Alexander Couzens71128672021-06-05 18:44:01 +02001009 memcpy(&gss->remote.ip4[gss->remote.num_ip4], v4_list, num_v4*sizeof(*v4_list));
1010 gss->remote.num_ip4 += num_v4;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001011
1012 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
Alexander Couzens71128672021-06-05 18:44:01 +02001013 gss->remote.num_ip4);
Alexander Couzens6a161492020-07-12 13:45:50 +02001014 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001015
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001016 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1017 const struct gprs_ns_ie_ip6_elem *v6_list;
1018 unsigned int num_v6;
1019 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1020 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
1021
Alexander Couzens71128672021-06-05 18:44:01 +02001022 if (num_v6 && gss->remote.ip4)
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001023 return -NS_CAUSE_INVAL_NR_IPv6_EP;
1024
1025 /* realloc to the new size */
Alexander Couzens71128672021-06-05 18:44:01 +02001026 gss->remote.ip6 = talloc_realloc(gss, gss->remote.ip6,
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001027 struct gprs_ns_ie_ip6_elem,
Alexander Couzens71128672021-06-05 18:44:01 +02001028 gss->remote.num_ip6 + num_v6);
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001029 /* append the new entries to the end of the list */
Alexander Couzens71128672021-06-05 18:44:01 +02001030 memcpy(&gss->remote.ip6[gss->remote.num_ip6], v6_list, num_v6*sizeof(*v6_list));
1031 gss->remote.num_ip6 += num_v6;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001032
Alexander Couzens71128672021-06-05 18:44:01 +02001033 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %d entries\n",
1034 gss->remote.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +02001035 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001036
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001037 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001038}
1039
Harald Welte694dad52021-03-23 15:22:16 +01001040static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens790a9632021-02-05 17:18:39 +01001041{
1042 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1043
Harald Weltef61a9152021-03-02 22:20:17 +01001044 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1045
Harald Welte694dad52021-03-23 15:22:16 +01001046 if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN)
Alexander Couzens790a9632021-02-05 17:18:39 +01001047 gss->N = 0;
1048}
1049
Harald Welte694dad52021-03-23 15:22:16 +01001050static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +02001051{
1052 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001053 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1054 uint8_t cause;
1055 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001056
Harald Weltef61a9152021-03-02 22:20:17 +01001057 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1058
Alexander Couzens6a161492020-07-12 13:45:50 +02001059 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001060 case GPRS_SNS_EV_RX_CONFIG_END:
1061 case GPRS_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001062 rc = ns_sns_append_remote_eps(fi, data);
1063 if (rc < 0) {
1064 cause = -rc;
1065 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1066 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1067 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001068 }
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001069 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1070 /* check if sum of data / sig weights == 0 */
1071 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1072 cause = NS_CAUSE_INVAL_WEIGH;
1073 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1074 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1075 return;
1076 }
1077 create_missing_nsvcs(fi);
1078 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1079 /* start the test procedure on ALL NSVCs! */
1080 gprs_ns2_start_alive_all_nsvcs(nse);
1081 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1082 } else {
1083 /* just send CONFIG-ACK */
1084 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1085 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001086 }
1087 break;
1088 default:
1089 OSMO_ASSERT(0);
1090 }
1091}
1092
Alexander Couzens67725e22021-02-15 02:37:03 +01001093/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001094static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1095 struct ns2_sns_state *gss,
1096 struct tlv_parsed *tp)
1097{
1098 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1099 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1100 int num_v4 = 0, num_v6 = 0;
1101 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001102 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001103 int rc = 0;
1104
1105 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1106 * check uniqueness within the lists (no doublicate entries)
1107 * check not-known-by-us and sent back a list of unknown/known values
1108 * (abnormal behaviour according to 48.016)
1109 */
1110
1111 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1112 if (gss->ip == IPv4) {
1113 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1114 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1115 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1116 return;
1117 }
1118
1119 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1120 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001121 for (i = 0; i < num_v4; i++) {
1122 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001123 rc = do_sns_add(fi, &v4_list[i], NULL);
1124 if (rc < 0) {
1125 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001126 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001127 do_sns_delete(fi, &v4_list[j], NULL);
1128 cause = -rc;
1129 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1130 break;
1131 }
1132 }
1133 } else { /* IPv6 */
1134 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1135 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1136 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1137 return;
1138 }
1139
1140 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1141 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001142 for (i = 0; i < num_v6; i++) {
1143 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001144 rc = do_sns_add(fi, NULL, &v6_list[i]);
1145 if (rc < 0) {
1146 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001147 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001148 do_sns_delete(fi, NULL, &v6_list[j]);
1149 cause = -rc;
1150 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1151 break;
1152 }
1153 }
1154 }
1155
1156 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1157 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1158}
1159
1160static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1161 struct ns2_sns_state *gss,
1162 struct tlv_parsed *tp)
1163{
1164 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1165 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1166 int num_v4 = 0, num_v6 = 0;
1167 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001168 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001169 int rc = 0;
1170
1171 /* TODO: split up delete into v4 + v6
1172 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1173 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1174 */
1175 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1176 if (gss->ip == IPv4) {
1177 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1178 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1179 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001180 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001181 rc = do_sns_delete(fi, &v4_list[i], NULL);
1182 if (rc < 0) {
1183 cause = -rc;
1184 /* continue to delete others */
1185 }
1186 }
1187 if (cause != 0xff) {
1188 /* TODO: create list of not-deleted and return it */
1189 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1190 return;
1191 }
1192
1193 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1194 /* delete all NS-VCs for given IPv4 address */
1195 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1196 struct gprs_ns_ie_ip4_elem *ip4_remote;
1197 uint32_t ip_addr = *(uint32_t *)(ie+1);
1198 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1199 cause = NS_CAUSE_UNKN_IP_ADDR;
1200 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1201 return;
1202 }
1203 /* make a copy as do_sns_delete() will change the array underneath us */
Alexander Couzens71128672021-06-05 18:44:01 +02001204 ip4_remote = talloc_memdup(fi, gss->remote.ip4,
1205 gss->remote.num_ip4 * sizeof(*v4_list));
1206 for (i = 0; i < gss->remote.num_ip4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001207 if (ip4_remote[i].ip_addr == ip_addr) {
1208 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1209 if (rc < 0) {
1210 cause = -rc;
1211 /* continue to delete others */
1212 }
1213 }
1214 }
1215 talloc_free(ip4_remote);
1216 if (cause != 0xff) {
1217 /* TODO: create list of not-deleted and return it */
1218 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1219 return;
1220 }
1221 } else {
1222 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1223 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1224 return;
1225 }
1226 } else { /* IPv6 */
1227 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1228 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1229 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001230 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001231 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1232 if (rc < 0) {
1233 cause = -rc;
1234 /* continue to delete others */
1235 }
1236 }
1237 if (cause != 0xff) {
1238 /* TODO: create list of not-deleted and return it */
1239 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1240 return;
1241 }
1242 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1243 /* delete all NS-VCs for given IPv4 address */
1244 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1245 struct gprs_ns_ie_ip6_elem *ip6_remote;
1246 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001247 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001248 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1249 cause = NS_CAUSE_UNKN_IP_ADDR;
1250 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1251 return;
1252 }
1253 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1254 /* make a copy as do_sns_delete() will change the array underneath us */
Alexander Couzens71128672021-06-05 18:44:01 +02001255 ip6_remote = talloc_memdup(fi, gss->remote.ip6,
1256 gss->remote.num_ip6 * sizeof(*v4_list));
1257 for (i = 0; i < gss->remote.num_ip6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001258 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1259 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1260 if (rc < 0) {
1261 cause = -rc;
1262 /* continue to delete others */
1263 }
1264 }
1265 }
1266
1267 talloc_free(ip6_remote);
1268 if (cause != 0xff) {
1269 /* TODO: create list of not-deleted and return it */
1270 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1271 return;
1272 }
1273 } else {
1274 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1275 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1276 return;
1277 }
1278 }
1279 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1280}
1281
1282static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1283 struct ns2_sns_state *gss,
1284 struct tlv_parsed *tp)
1285{
1286 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1287 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1288 int num_v4 = 0, num_v6 = 0;
1289 uint8_t trans_id, cause = 0xff;
1290 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001291 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001292
1293 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1294 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1295 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1296 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001297 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001298 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1299 if (rc < 0) {
1300 cause = -rc;
1301 /* continue to others */
1302 }
1303 }
1304 if (cause != 0xff) {
1305 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1306 return;
1307 }
1308 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1309 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1310 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001311 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001312 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1313 if (rc < 0) {
1314 cause = -rc;
1315 /* continue to others */
1316 }
1317 }
1318 if (cause != 0xff) {
1319 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1320 return;
1321 }
1322 } else {
1323 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1324 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1325 return;
1326 }
1327 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1328}
1329
1330static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1331{
1332 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1333 struct tlv_parsed *tp = data;
1334
1335 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001336 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001337 ns2_sns_st_configured_add(fi, gss, tp);
1338 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001339 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001340 ns2_sns_st_configured_delete(fi, gss, tp);
1341 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001342 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001343 ns2_sns_st_configured_change(fi, gss, tp);
1344 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001345 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001346 osmo_timer_del(&fi->timer);
1347 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001348 }
1349}
1350
1351static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1352{
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001353 struct gprs_ns2_vc *nsvc;
1354 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001355 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001356 /* NS-VC status updates are only parsed in ST_CONFIGURED.
1357 * Do an initial check if there are any nsvc alive atm */
1358 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1359 if (ns2_vc_is_unblocked(nsvc)) {
1360 gss->alive = true;
1361 osmo_timer_del(&fi->timer);
1362 break;
1363 }
1364 }
1365
Alexander Couzens53e70092021-04-06 15:45:47 +02001366 /* remove the initial NSVC if the NSVC isn't part of the configuration */
1367 if (gss->sns_nsvc->sns_only)
1368 gprs_ns2_free_nsvc(gss->sns_nsvc);
1369
Alexander Couzens138b96f2021-01-25 16:23:29 +01001370 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001371}
1372
1373static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1374 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001375 .in_event_mask = 0, /* handled by all_state_action */
Alexander Couzens0a7c5ee2021-04-10 18:20:21 +02001376 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1377 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001378 .name = "UNCONFIGURED",
Harald Welte694dad52021-03-23 15:22:16 +01001379 .action = ns2_sns_st_bss_unconfigured,
Alexander Couzens6a161492020-07-12 13:45:50 +02001380 },
Harald Welte694dad52021-03-23 15:22:16 +01001381 [GPRS_SNS_ST_BSS_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001382 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001383 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001384 S(GPRS_SNS_ST_BSS_SIZE) |
1385 S(GPRS_SNS_ST_BSS_CONFIG_BSS),
1386 .name = "BSS_SIZE",
1387 .action = ns2_sns_st_bss_size,
1388 .onenter = ns2_sns_st_bss_size_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001389 },
Harald Welte694dad52021-03-23 15:22:16 +01001390 [GPRS_SNS_ST_BSS_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001391 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001392 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001393 S(GPRS_SNS_ST_BSS_CONFIG_BSS) |
1394 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
1395 S(GPRS_SNS_ST_BSS_SIZE),
1396 .name = "BSS_CONFIG_BSS",
1397 .action = ns2_sns_st_bss_config_bss,
1398 .onenter = ns2_sns_st_bss_config_bss_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001399 },
Harald Welte694dad52021-03-23 15:22:16 +01001400 [GPRS_SNS_ST_BSS_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001401 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1402 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001403 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001404 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens6a161492020-07-12 13:45:50 +02001405 S(GPRS_SNS_ST_CONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001406 S(GPRS_SNS_ST_BSS_SIZE),
1407 .name = "BSS_CONFIG_SGSN",
1408 .action = ns2_sns_st_bss_config_sgsn,
1409 .onenter = ns2_sns_st_bss_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001410 },
1411 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001412 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1413 S(GPRS_SNS_EV_RX_DELETE) |
1414 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1415 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001416 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001417 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001418 .name = "CONFIGURED",
1419 .action = ns2_sns_st_configured,
1420 .onenter = ns2_sns_st_configured_onenter,
1421 },
1422};
1423
1424static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1425{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001426 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001427 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1428 struct gprs_ns2_inst *nsi = nse->nsi;
1429
Alexander Couzens90ee9632020-12-07 06:18:32 +01001430 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001431 switch (fi->T) {
1432 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001433 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1434 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001435 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001436 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001437 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 +01001438 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001439 break;
1440 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001441 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001442 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 +01001443 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001444 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001445 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 +01001446 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001447 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001448 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001449 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1450 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 +01001451 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001452 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001453 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 +01001454 }
1455 break;
1456 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001457 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 +01001458 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001459 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001460 }
1461 return 0;
1462}
1463
Harald Welte9e37bf42021-03-02 20:48:31 +01001464/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001465static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1466{
1467 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001468 struct ns2_sns_bind *sbind;
1469 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001470
Alexander Couzense769f522020-12-07 07:37:07 +01001471 switch (event) {
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001472 case GPRS_SNS_EV_REQ_ADD_BIND:
1473 sbind = data;
1474 switch (fi->state) {
1475 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001476 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001477 break;
Harald Welte694dad52021-03-23 15:22:16 +01001478 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001479 /* TODO: add the ip4 element to the list */
1480 break;
Harald Welte694dad52021-03-23 15:22:16 +01001481 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1482 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001483 case GPRS_SNS_ST_CONFIGURED:
1484 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1485 break;
1486 }
1487 break;
1488 case GPRS_SNS_EV_REQ_DELETE_BIND:
1489 sbind = data;
1490 switch (fi->state) {
1491 case GPRS_SNS_ST_UNCONFIGURED:
1492 break;
Harald Welte694dad52021-03-23 15:22:16 +01001493 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001494 /* TODO: remove the ip4 element from the list */
1495 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1496 if (nsvc->bind == sbind->bind) {
1497 gprs_ns2_free_nsvc(nsvc);
1498 }
1499 }
1500 break;
Harald Welte694dad52021-03-23 15:22:16 +01001501 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1502 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001503 case GPRS_SNS_ST_CONFIGURED:
1504 /* TODO: do an delete SNS-IP procedure */
1505 /* TODO: remove the ip4 element to the list */
1506 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1507 if (nsvc->bind == sbind->bind) {
1508 gprs_ns2_free_nsvc(nsvc);
1509 }
1510 }
1511 break;
1512 }
1513 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1514 talloc_free(sbind);
1515 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001516 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001517}
1518
Alexander Couzens31d52e12021-06-05 20:04:04 +02001519/* validate the bss configuration (sns endpoint and binds)
1520 * - no endpoints -> invalid
1521 * - no binds -> invalid
1522 * - only v4 sns endpoints, only v6 binds -> invalid
1523 * - only v4 sns endpoints, but v4 sig weights == 0 -> invalid ...
1524 */
1525static int ns2_sns_bss_valid_configuration(struct ns2_sns_state *gss)
1526{
1527 struct ns2_sns_bind *sbind;
1528 struct sns_endpoint *endpoint;
1529 const struct osmo_sockaddr *addr;
1530 int v4_sig = 0, v4_data = 0, v6_sig = 0, v6_data = 0;
1531 bool v4_endpoints = false;
1532 bool v6_endpoints = false;
1533
1534 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds))
1535 return 0;
1536
1537 llist_for_each_entry(sbind, &gss->binds, list) {
1538 addr = gprs_ns2_ip_bind_sockaddr(sbind->bind);
1539 if (!addr)
1540 continue;
1541 switch (addr->u.sa.sa_family) {
1542 case AF_INET:
1543 v4_sig += sbind->bind->sns_sig_weight;
1544 v4_data += sbind->bind->sns_data_weight;
1545 break;
1546 case AF_INET6:
1547 v6_sig += sbind->bind->sns_sig_weight;
1548 v6_data += sbind->bind->sns_data_weight;
1549 break;
1550 }
1551 }
1552
1553 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
1554 switch (endpoint->saddr.u.sa.sa_family) {
1555 case AF_INET:
1556 v4_endpoints = true;
1557 break;
1558 case AF_INET6:
1559 v6_endpoints = true;
1560 break;
1561 }
1562 }
1563
1564 return (v4_endpoints && v4_sig && v4_data) || (v6_endpoints && v6_sig && v6_data);
1565}
1566
Harald Welte9e37bf42021-03-02 20:48:31 +01001567/* allstate-action for BSS role */
1568static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1569{
1570 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1571 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1572
1573 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
1574 switch (event) {
1575 case GPRS_SNS_EV_REQ_NO_NSVC:
1576 /* ignore reselection running */
1577 if (gss->reselection_running)
1578 break;
1579
1580 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1581 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1582 break;
1583 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
1584 /* tear down previous state
1585 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1586 gss->reselection_running = true;
1587 gprs_ns2_free_nsvcs(nse);
Harald Welte46eb7642021-03-04 17:49:59 +01001588 ns2_clear_ipv46_entries_local(gss);
1589 ns2_clear_ipv46_entries_remote(gss);
Harald Welte9e37bf42021-03-02 20:48:31 +01001590
1591 /* Choose the next sns endpoint. */
Alexander Couzens31d52e12021-06-05 20:04:04 +02001592 if (!ns2_sns_bss_valid_configuration(gss)) {
Harald Welte9e37bf42021-03-02 20:48:31 +01001593 gss->initial = NULL;
1594 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1595 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1596 return;
1597 } else if (!gss->initial) {
1598 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1599 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1600 /* last entry, continue with first */
1601 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1602 } else {
1603 /* next element is an entry */
1604 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1605 }
1606
1607 gss->reselection_running = false;
Harald Welte694dad52021-03-23 15:22:16 +01001608 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 +01001609 break;
1610 default:
1611 ns2_sns_st_all_action(fi, event, data);
1612 break;
1613 }
1614}
1615
Alexander Couzens6a161492020-07-12 13:45:50 +02001616static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1617 .name = "GPRS-NS2-SNS-BSS",
1618 .states = ns2_sns_bss_states,
1619 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001620 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1621 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001622 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1623 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001624 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001625 .cleanup = NULL,
1626 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001627 .event_names = gprs_sns_event_names,
1628 .pre_term = NULL,
1629 .log_subsys = DLNS,
1630};
1631
Harald Welte5bef2cc2020-09-18 22:33:24 +02001632/*! Allocate an IP-SNS FSM for the BSS side.
1633 * \param[in] nse NS Entity in which the FSM runs
1634 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001635 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001636struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1637 const char *id)
1638{
1639 struct osmo_fsm_inst *fi;
1640 struct ns2_sns_state *gss;
1641
1642 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1643 if (!fi)
1644 return fi;
1645
1646 gss = talloc_zero(fi, struct ns2_sns_state);
1647 if (!gss)
1648 goto err;
1649
1650 fi->priv = gss;
1651 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001652 gss->role = GPRS_SNS_ROLE_BSS;
Harald Welte24f4df52021-03-04 18:02:54 +01001653 /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */
1654 gss->num_max_ip4_remote = 8192;
1655 gss->num_max_ip6_remote = 8192;
Alexander Couzense769f522020-12-07 07:37:07 +01001656 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001657 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001658
1659 return fi;
1660err:
1661 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1662 return NULL;
1663}
1664
Harald Welte5bef2cc2020-09-18 22:33:24 +02001665/*! main entry point for receiving SNS messages from the network.
1666 * \param[in] nsvc NS-VC on which the message was received
1667 * \param[in] msg message buffer of the IP-SNS message
1668 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001669 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001670int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001671{
1672 struct gprs_ns2_nse *nse = nsvc->nse;
1673 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1674 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001675 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001676 struct osmo_fsm_inst *fi;
Alexander Couzens7619ed42021-03-24 17:44:03 +01001677 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001678
1679 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001680 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1681 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001682 rc = -EINVAL;
1683 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +02001684 }
1685
Alexander Couzens6a161492020-07-12 13:45:50 +02001686 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1687 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001688 gss = (struct ns2_sns_state *) fi->priv;
1689 if (!gss->sns_nsvc)
1690 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001691
Harald Weltef2949742021-01-20 14:54:14 +01001692 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1693 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1694
Alexander Couzens6a161492020-07-12 13:45:50 +02001695 switch (nsh->pdu_type) {
1696 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001697 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001698 break;
1699 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001700 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001701 break;
1702 case SNS_PDUT_CONFIG:
1703 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001704 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001705 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001706 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001707 break;
1708 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001709 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001710 break;
1711 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001712 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001713 break;
1714 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001715 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001716 break;
1717 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001718 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001719 break;
1720 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001721 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001722 break;
1723 default:
Harald Weltef2949742021-01-20 14:54:14 +01001724 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1725 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001726 rc = -EINVAL;
Alexander Couzens6a161492020-07-12 13:45:50 +02001727 }
1728
Alexander Couzens7619ed42021-03-24 17:44:03 +01001729out:
1730 msgb_free(msg);
1731
1732 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001733}
1734
1735#include <osmocom/vty/vty.h>
1736#include <osmocom/vty/misc.h>
1737
Harald Welte1262c4f2021-01-19 20:58:33 +01001738static 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 +02001739{
1740 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001741 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001742 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1743}
1744
Harald Welte1262c4f2021-01-19 20:58:33 +01001745static 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 +02001746{
1747 char ip_addr[INET6_ADDRSTRLEN] = {};
1748 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1749 strcpy(ip_addr, "Invalid IPv6");
1750
Harald Welte1262c4f2021-01-19 20:58:33 +01001751 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001752 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1753}
1754
Harald Welte5bef2cc2020-09-18 22:33:24 +02001755/*! Dump the IP-SNS state to a vty.
1756 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001757 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001758 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1759 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001760void 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 +02001761{
1762 struct ns2_sns_state *gss;
1763 unsigned int i;
1764
1765 if (!nse->bss_sns_fi)
1766 return;
1767
Harald Welte1262c4f2021-01-19 20:58:33 +01001768 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001769 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1770
Harald Welte1262c4f2021-01-19 20:58:33 +01001771 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1772 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001773
Alexander Couzens71128672021-06-05 18:44:01 +02001774 if (gss->local.num_ip4 && gss->remote.num_ip4) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001775 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001776 for (i = 0; i < gss->local.num_ip4; i++)
1777 vty_dump_sns_ip4(vty, prefix, &gss->local.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001778
Harald Welte1262c4f2021-01-19 20:58:33 +01001779 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001780 for (i = 0; i < gss->remote.num_ip4; i++)
1781 vty_dump_sns_ip4(vty, prefix, &gss->remote.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001782 }
1783
Alexander Couzens71128672021-06-05 18:44:01 +02001784 if (gss->local.num_ip6 && gss->remote.num_ip6) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001785 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001786 for (i = 0; i < gss->local.num_ip6; i++)
1787 vty_dump_sns_ip6(vty, prefix, &gss->local.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001788
Harald Welte1262c4f2021-01-19 20:58:33 +01001789 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001790 for (i = 0; i < gss->remote.num_ip6; i++)
1791 vty_dump_sns_ip6(vty, prefix, &gss->remote.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001792 }
1793}
1794
Alexander Couzens412bc342020-11-19 05:24:37 +01001795/*! write IP-SNS to a vty
1796 * \param[in] vty VTY to which the state shall be printed
1797 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001798void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001799{
1800 struct ns2_sns_state *gss;
1801 struct osmo_sockaddr_str addr_str;
1802 struct sns_endpoint *endpoint;
1803
1804 if (!nse->bss_sns_fi)
1805 return;
1806
1807 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1808 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001809 /* It's unlikely that an error happens, but let's better be safe. */
1810 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1811 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001812 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001813 }
1814}
1815
Alexander Couzense769f522020-12-07 07:37:07 +01001816static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1817 const struct osmo_sockaddr *saddr)
1818{
1819 struct sns_endpoint *endpoint;
1820
1821 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1822 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1823 return endpoint;
1824 }
1825
1826 return NULL;
1827}
1828
1829/*! gprs_ns2_sns_add_endpoint
1830 * \param[in] nse
1831 * \param[in] sockaddr
1832 * \return
1833 */
1834int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1835 const struct osmo_sockaddr *saddr)
1836{
1837 struct ns2_sns_state *gss;
1838 struct sns_endpoint *endpoint;
1839 bool do_selection = false;
1840
1841 if (nse->ll != GPRS_NS2_LL_UDP) {
1842 return -EINVAL;
1843 }
1844
Alexander Couzens138b96f2021-01-25 16:23:29 +01001845 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001846 return -EINVAL;
1847 }
1848
1849 gss = nse->bss_sns_fi->priv;
1850
1851 if (ns2_get_sns_endpoint(gss, saddr))
1852 return -EADDRINUSE;
1853
1854 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1855 if (!endpoint)
1856 return -ENOMEM;
1857
1858 endpoint->saddr = *saddr;
1859 if (llist_empty(&gss->sns_endpoints))
1860 do_selection = true;
1861
1862 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1863 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001864 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001865
1866 return 0;
1867}
1868
1869/*! gprs_ns2_sns_del_endpoint
1870 * \param[in] nse
1871 * \param[in] sockaddr
1872 * \return 0 on success, otherwise < 0
1873 */
1874int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1875 const struct osmo_sockaddr *saddr)
1876{
1877 struct ns2_sns_state *gss;
1878 struct sns_endpoint *endpoint;
1879
1880 if (nse->ll != GPRS_NS2_LL_UDP) {
1881 return -EINVAL;
1882 }
1883
Alexander Couzens138b96f2021-01-25 16:23:29 +01001884 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001885 return -EINVAL;
1886 }
1887
1888 gss = nse->bss_sns_fi->priv;
1889 endpoint = ns2_get_sns_endpoint(gss, saddr);
1890 if (!endpoint)
1891 return -ENOENT;
1892
1893 /* if this is an unused SNS endpoint it's done */
1894 if (gss->initial != endpoint) {
1895 llist_del(&endpoint->list);
1896 talloc_free(endpoint);
1897 return 0;
1898 }
1899
Alexander Couzens67725e22021-02-15 02:37:03 +01001900 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001901 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001902 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001903 "Closing all NS-VC and restart SNS-SIZE procedure"
1904 "with a remaining SNS endpoint.\n");
1905
1906 /* Continue with the next endpoint in the list.
1907 * Special case if the endpoint is at the start or end of the list */
1908 if (endpoint->list.prev == &gss->sns_endpoints ||
1909 endpoint->list.next == &gss->sns_endpoints)
1910 gss->initial = NULL;
1911 else
1912 gss->initial = llist_entry(endpoint->list.next->prev,
1913 struct sns_endpoint,
1914 list);
1915
1916 llist_del(&endpoint->list);
1917 gprs_ns2_free_nsvcs(nse);
1918 talloc_free(endpoint);
1919
1920 return 0;
1921}
1922
1923/*! gprs_ns2_sns_count
1924 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1925 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1926 */
1927int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1928{
1929 struct ns2_sns_state *gss;
1930 struct sns_endpoint *endpoint;
1931 int count = 0;
1932
1933 if (nse->ll != GPRS_NS2_LL_UDP) {
1934 return -EINVAL;
1935 }
1936
Alexander Couzens138b96f2021-01-25 16:23:29 +01001937 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001938 return -EINVAL;
1939 }
1940
1941 gss = nse->bss_sns_fi->priv;
1942 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1943 count++;
1944
1945 return count;
1946}
1947
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001948void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1949{
1950 struct ns2_sns_state *gss;
1951 struct gprs_ns2_vc *tmp;
1952
1953 if (!nse->bss_sns_fi)
1954 return;
1955
1956 gss = nse->bss_sns_fi->priv;
1957 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1958 return;
1959
1960 if (alive == gss->alive)
1961 return;
1962
1963 /* check if this is the current SNS NS-VC */
1964 if (nsvc == gss->sns_nsvc) {
1965 /* only replace the SNS NS-VC if there are other alive NS-VC.
1966 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1967 * and couldn't confirm yet if the NS-VC comes up */
1968 if (gss->alive && !alive)
1969 ns2_sns_replace_nsvc(nsvc);
1970 }
1971
1972 if (alive) {
1973 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001974 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001975 } else {
1976 /* is there at least another alive nsvc? */
1977 llist_for_each_entry(tmp, &nse->nsvc, list) {
1978 if (ns2_vc_is_unblocked(tmp))
1979 return;
1980 }
1981
1982 /* all NS-VC have failed */
1983 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001984 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001985 }
1986}
1987
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001988int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1989 struct gprs_ns2_vc_bind *bind)
1990{
1991 struct ns2_sns_state *gss;
1992 struct ns2_sns_bind *tmp;
1993
1994 OSMO_ASSERT(nse->bss_sns_fi);
1995 gss = nse->bss_sns_fi->priv;
1996
1997 if (!gprs_ns2_is_ip_bind(bind)) {
1998 return -EINVAL;
1999 }
2000
2001 if (!llist_empty(&gss->binds)) {
2002 llist_for_each_entry(tmp, &gss->binds, list) {
2003 if (tmp->bind == bind)
2004 return -EALREADY;
2005 }
2006 }
2007
2008 tmp = talloc_zero(gss, struct ns2_sns_bind);
2009 if (!tmp)
2010 return -ENOMEM;
2011 tmp->bind = bind;
2012 llist_add_tail(&tmp->list, &gss->binds);
2013
2014 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
2015 return 0;
2016}
2017
2018/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
2019int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
2020 struct gprs_ns2_vc_bind *bind)
2021{
2022 struct ns2_sns_state *gss;
2023 struct ns2_sns_bind *tmp, *tmp2;
2024 bool found = false;
2025
2026 if (!nse->bss_sns_fi)
2027 return -EINVAL;
2028
2029 gss = nse->bss_sns_fi->priv;
2030 if (gss->initial_bind && gss->initial_bind->bind == bind) {
2031 if (gss->initial_bind->list.prev == &gss->binds)
2032 gss->initial_bind = NULL;
2033 else
2034 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
2035 }
2036
2037 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
2038 if (tmp->bind == bind) {
2039 llist_del(&tmp->list);
2040 found = true;
Alexander Couzensa35c2962021-04-19 03:30:15 +02002041 break;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002042 }
2043 }
2044
2045 if (!found)
2046 return -ENOENT;
2047
2048 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
2049 return 0;
2050}
2051
Alexander Couzens71128672021-06-05 18:44:01 +02002052/* Update SNS weights for a bind (local endpoint).
2053 * \param[in] bind the bind which has been updated
Alexander Couzensc4704762021-02-08 23:13:12 +01002054 */
2055void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
2056{
2057 /* TODO: implement weights after binds per sns implemented */
2058}
2059
Harald Welte4f127462021-03-02 20:49:10 +01002060
2061
2062
2063/***********************************************************************
2064 * SGSN role
2065 ***********************************************************************/
2066
2067static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2068{
2069 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2070 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2071 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
2072}
2073
2074/* We're waiting for inbound SNS-CONFIG from the BSS */
2075static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2076{
2077 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2078 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2079 struct gprs_ns2_inst *nsi = nse->nsi;
2080 uint8_t cause;
2081 int rc;
2082
2083 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2084
2085 switch (event) {
2086 case GPRS_SNS_EV_RX_CONFIG:
2087 case GPRS_SNS_EV_RX_CONFIG_END:
2088 rc = ns_sns_append_remote_eps(fi, data);
2089 if (rc < 0) {
2090 cause = -rc;
2091 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2092 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2093 return;
2094 }
2095 /* only change state if last CONFIG was received */
2096 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
2097 /* ensure sum of data weight / sig weights is > 0 */
2098 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
2099 cause = NS_CAUSE_INVAL_WEIGH;
2100 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2101 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2102 break;
2103 }
2104 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2105 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2106 } else {
2107 /* just send CONFIG-ACK */
2108 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2109 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2110 }
2111 break;
2112 }
2113}
2114
2115static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2116{
2117 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2118 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2119
Harald Welte4f127462021-03-02 20:49:10 +01002120 /* transmit SGSN-oriented SNS-CONFIG */
Alexander Couzens71128672021-06-05 18:44:01 +02002121 ns2_tx_sns_config(gss->sns_nsvc, true, gss->local.ip4, gss->local.num_ip4,
2122 gss->local.ip6, gss->local.num_ip6);
Harald Welte4f127462021-03-02 20:49:10 +01002123}
2124
2125/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2126static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2127{
2128 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2129 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2130 struct tlv_parsed *tp = NULL;
2131
2132 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2133
2134 switch (event) {
2135 case GPRS_SNS_EV_RX_CONFIG_ACK:
2136 tp = data;
2137 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2138 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2139 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2140 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2141 break;
2142 }
2143 /* we currently only send one SNS-CONFIG with END FLAG */
2144 if (true) {
2145 create_missing_nsvcs(fi);
2146 /* start the test procedure on ALL NSVCs! */
2147 gprs_ns2_start_alive_all_nsvcs(nse);
2148 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2149 }
2150 break;
2151 }
2152}
2153
2154/* SGSN-side SNS state machine */
2155static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2156 [GPRS_SNS_ST_UNCONFIGURED] = {
2157 .in_event_mask = 0, /* handled by all_state_action */
2158 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2159 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2160 .name = "UNCONFIGURED",
2161 .action = ns2_sns_st_sgsn_unconfigured,
2162 },
2163 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
2164 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
2165 S(GPRS_SNS_EV_RX_CONFIG_END),
2166 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2167 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2168 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2169 .name = "SGSN_WAIT_CONFIG",
2170 .action = ns2_sns_st_sgsn_wait_config,
2171 },
2172 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
2173 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
2174 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2175 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2176 S(GPRS_SNS_ST_CONFIGURED),
2177 .name = "SGSN_WAIT_CONFIG_ACK",
2178 .action = ns2_sns_st_sgsn_wait_config_ack,
2179 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2180 },
2181 [GPRS_SNS_ST_CONFIGURED] = {
2182 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
2183 S(GPRS_SNS_EV_RX_DELETE) |
2184 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
2185 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
2186 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2187 .name = "CONFIGURED",
2188 /* shared with BSS side; once configured there's no difference */
2189 .action = ns2_sns_st_configured,
2190 .onenter = ns2_sns_st_configured_onenter,
2191 },
2192};
2193
2194static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2195{
2196 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2197 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2198 struct gprs_ns2_inst *nsi = nse->nsi;
2199
2200 gss->N++;
2201 switch (fi->T) {
2202 case 3:
2203 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2204 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2205 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2206 } else {
2207 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2208 }
2209 break;
2210 case 4:
2211 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2212 break;
2213 }
2214 return 0;
2215}
2216
2217
2218/* allstate-action for SGSN role */
2219static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2220{
2221 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2222 struct tlv_parsed *tp = NULL;
Harald Welte01fa6a32021-03-04 19:49:38 +01002223 size_t num_local_eps, num_remote_eps;
Harald Welte4f127462021-03-02 20:49:10 +01002224 uint8_t flag;
Harald Weltea2c5af52021-03-04 17:59:35 +01002225 uint8_t cause;
Harald Welte4f127462021-03-02 20:49:10 +01002226
2227 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2228
2229 switch (event) {
2230 case GPRS_SNS_EV_RX_SIZE:
2231 tp = (struct tlv_parsed *) data;
Harald Weltea2c5af52021-03-04 17:59:35 +01002232 /* check for mandatory / conditional IEs */
2233 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) ||
2234 !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) {
2235 cause = NS_CAUSE_MISSING_ESSENT_IE;
2236 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2237 break;
2238 }
2239 if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) &&
2240 !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) {
2241 cause = NS_CAUSE_MISSING_ESSENT_IE;
Harald Welte4f127462021-03-02 20:49:10 +01002242 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2243 break;
2244 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002245 if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2))
2246 gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR);
2247 if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2))
2248 gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR);
2249 /* decide if we go for IPv4 or IPv6 */
2250 if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, IPv6)) {
2251 gss->ip = IPv6;
Harald Welte2d807b62021-03-24 01:57:30 +01002252 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002253 num_local_eps = gss->local.num_ip6;
Harald Welte01fa6a32021-03-04 19:49:38 +01002254 num_remote_eps = gss->num_max_ip6_remote;
2255 } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, IPv4)) {
2256 gss->ip = IPv4;
Harald Welte2d807b62021-03-24 01:57:30 +01002257 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002258 num_local_eps = gss->local.num_ip4;
Harald Welte01fa6a32021-03-04 19:49:38 +01002259 num_remote_eps = gss->num_max_ip4_remote;
2260 } else {
Alexander Couzens71128672021-06-05 18:44:01 +02002261 if (gss->local.num_ip4 && !gss->num_max_ip4_remote)
Harald Welte01fa6a32021-03-04 19:49:38 +01002262 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
2263 else
2264 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
2265 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2266 break;
2267 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002268 /* ensure number of NS-VCs is sufficient for full mesh */
2269 gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC);
2270 if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) {
2271 LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, "
2272 "but BSS supports only %zu maximum NS-VCs\n", num_local_eps,
2273 num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs);
2274 cause = NS_CAUSE_INVAL_NR_NS_VC;
2275 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2276 break;
2277 }
2278 /* perform state reset, if requested */
Harald Welte4f127462021-03-02 20:49:10 +01002279 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2280 if (flag & 1) {
2281 struct gprs_ns2_vc *nsvc, *nsvc2;
2282 /* clear all state */
Harald Welte46eb7642021-03-04 17:49:59 +01002283 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
Harald Welte4f127462021-03-02 20:49:10 +01002284 gss->N = 0;
Harald Welte46eb7642021-03-04 17:49:59 +01002285 ns2_clear_ipv46_entries_local(gss);
2286 ns2_clear_ipv46_entries_remote(gss);
Harald Welte4f127462021-03-02 20:49:10 +01002287 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2288 if (nsvc == gss->sns_nsvc) {
2289 /* keep the NSVC we need for SNS, but unconfigure it */
2290 nsvc->sig_weight = 0;
2291 nsvc->data_weight = 0;
2292 ns2_vc_force_unconfigured(nsvc);
2293 } else {
2294 /* free all other NS-VCs */
2295 gprs_ns2_free_nsvc(nsvc);
2296 }
2297 }
Harald Welte2d807b62021-03-24 01:57:30 +01002298 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte4f127462021-03-02 20:49:10 +01002299 }
2300 /* send SIZE_ACK */
2301 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2302 /* only wait for SNS-CONFIG in case of Reset flag */
2303 if (flag & 1)
2304 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2305 break;
2306 default:
2307 ns2_sns_st_all_action(fi, event, data);
2308 break;
2309 }
2310}
2311
2312static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2313 .name = "GPRS-NS2-SNS-SGSN",
2314 .states = ns2_sns_sgsn_states,
2315 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
2316 .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) |
2317 S(GPRS_SNS_EV_REQ_NO_NSVC) |
2318 S(GPRS_SNS_EV_REQ_ADD_BIND) |
2319 S(GPRS_SNS_EV_REQ_DELETE_BIND),
2320 .allstate_action = ns2_sns_st_all_action_sgsn,
2321 .cleanup = NULL,
2322 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2323 .event_names = gprs_sns_event_names,
2324 .pre_term = NULL,
2325 .log_subsys = DLNS,
2326};
2327
2328/*! Allocate an IP-SNS FSM for the SGSN side.
2329 * \param[in] nse NS Entity in which the FSM runs
2330 * \param[in] id string identifier
2331 * \returns FSM instance on success; NULL on error */
2332struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2333{
2334 struct osmo_fsm_inst *fi;
2335 struct ns2_sns_state *gss;
2336
2337 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2338 if (!fi)
2339 return fi;
2340
2341 gss = talloc_zero(fi, struct ns2_sns_state);
2342 if (!gss)
2343 goto err;
2344
2345 fi->priv = gss;
2346 gss->nse = nse;
2347 gss->role = GPRS_SNS_ROLE_SGSN;
2348 INIT_LLIST_HEAD(&gss->sns_endpoints);
2349 INIT_LLIST_HEAD(&gss->binds);
2350
2351 return fi;
2352err:
2353 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2354 return NULL;
2355}
2356
2357
2358
2359
Alexander Couzens6a161492020-07-12 13:45:50 +02002360/* initialize osmo_ctx on main tread */
2361static __attribute__((constructor)) void on_dso_load_ctx(void)
2362{
2363 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002364 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002365}