blob: 1df8fc01407b9e0bb5b40fa5e886c0c7ec71558c [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
64enum gprs_sns_bss_state {
65 GPRS_SNS_ST_UNCONFIGURED,
66 GPRS_SNS_ST_SIZE, /*!< SNS-SIZE procedure ongoing */
67 GPRS_SNS_ST_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */
68 GPRS_SNS_ST_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */
69 GPRS_SNS_ST_CONFIGURED,
70};
71
72enum gprs_sns_event {
Alexander Couzens67725e22021-02-15 02:37:03 +010073 GPRS_SNS_EV_REQ_SELECT_ENDPOINT, /*!< Select a SNS endpoint from the list */
74 GPRS_SNS_EV_RX_SIZE,
75 GPRS_SNS_EV_RX_SIZE_ACK,
76 GPRS_SNS_EV_RX_CONFIG,
77 GPRS_SNS_EV_RX_CONFIG_END, /*!< SNS-CONFIG with end flag received */
78 GPRS_SNS_EV_RX_CONFIG_ACK,
79 GPRS_SNS_EV_RX_ADD,
80 GPRS_SNS_EV_RX_DELETE,
81 GPRS_SNS_EV_RX_CHANGE_WEIGHT,
Harald Welteb9f23872021-03-02 20:48:54 +010082 GPRS_SNS_EV_RX_ACK, /*!< Rx of SNS-ACK (response to ADD/DELETE/CHG_WEIGHT */
Harald Welte04647e12021-03-02 18:50:40 +010083 GPRS_SNS_EV_REQ_NO_NSVC, /*!< no more NS-VC remaining (all dead) */
Alexander Couzens67725e22021-02-15 02:37:03 +010084 GPRS_SNS_EV_REQ_NSVC_ALIVE, /*!< a NS-VC became alive */
Harald Welte04647e12021-03-02 18:50:40 +010085 GPRS_SNS_EV_REQ_ADD_BIND, /*!< add a new local bind to this NSE */
86 GPRS_SNS_EV_REQ_DELETE_BIND, /*!< remove a local bind from this NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +020087};
88
89static const struct value_string gprs_sns_event_names[] = {
Alexander Couzens67725e22021-02-15 02:37:03 +010090 { GPRS_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" },
91 { GPRS_SNS_EV_RX_SIZE, "RX_SIZE" },
92 { GPRS_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" },
93 { GPRS_SNS_EV_RX_CONFIG, "RX_CONFIG" },
94 { GPRS_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" },
95 { GPRS_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" },
96 { GPRS_SNS_EV_RX_ADD, "RX_ADD" },
97 { GPRS_SNS_EV_RX_DELETE, "RX_DELETE" },
Harald Welteb9f23872021-03-02 20:48:54 +010098 { GPRS_SNS_EV_RX_ACK, "RX_ACK" },
Alexander Couzens67725e22021-02-15 02:37:03 +010099 { GPRS_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" },
100 { GPRS_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" },
101 { GPRS_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"},
102 { GPRS_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"},
103 { GPRS_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200104 { 0, NULL }
105};
106
Alexander Couzense769f522020-12-07 07:37:07 +0100107struct sns_endpoint {
108 struct llist_head list;
109 struct osmo_sockaddr saddr;
110};
111
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100112struct ns2_sns_bind {
113 struct llist_head list;
114 struct gprs_ns2_vc_bind *bind;
115};
116
Alexander Couzens6a161492020-07-12 13:45:50 +0200117struct ns2_sns_state {
118 struct gprs_ns2_nse *nse;
119
120 enum ns2_sns_type ip;
121
Alexander Couzense769f522020-12-07 07:37:07 +0100122 /* holds the list of initial SNS endpoints */
123 struct llist_head sns_endpoints;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100124 /* list of used struct ns2_sns_bind */
125 struct llist_head binds;
126 /* pointer to the bind which was used to initiate the SNS connection */
127 struct ns2_sns_bind *initial_bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100128 /* prevent recursive reselection */
129 bool reselection_running;
130
131 /* The current initial SNS endpoints.
132 * The initial connection will be moved into the NSE
133 * if configured via SNS. Otherwise it will be removed
134 * in configured state. */
135 struct sns_endpoint *initial;
Alexander Couzens6a161492020-07-12 13:45:50 +0200136 /* all SNS PDU will be sent over this nsvc */
137 struct gprs_ns2_vc *sns_nsvc;
Alexander Couzens90ee9632020-12-07 06:18:32 +0100138 /* timer N */
139 int N;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100140 /* true if at least one nsvc is alive */
141 bool alive;
Alexander Couzens6a161492020-07-12 13:45:50 +0200142
143 /* local configuration to send to the remote end */
144 struct gprs_ns_ie_ip4_elem *ip4_local;
145 size_t num_ip4_local;
146
147 /* local configuration to send to the remote end */
148 struct gprs_ns_ie_ip6_elem *ip6_local;
149 size_t num_ip6_local;
150
151 /* local configuration about our capabilities in terms of connections to
152 * remote (SGSN) side */
153 size_t num_max_nsvcs;
154 size_t num_max_ip4_remote;
155 size_t num_max_ip6_remote;
156
157 /* remote configuration as received */
158 struct gprs_ns_ie_ip4_elem *ip4_remote;
159 unsigned int num_ip4_remote;
160
161 /* remote configuration as received */
162 struct gprs_ns_ie_ip6_elem *ip6_remote;
163 unsigned int num_ip6_remote;
164};
165
166static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi)
167{
168 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
169 return gss->nse;
170}
171
172/* helper function to compute the sum of all (data or signaling) weights */
173static int ip4_weight_sum(const struct gprs_ns_ie_ip4_elem *ip4, unsigned int num,
174 bool data_weight)
175{
176 unsigned int i;
177 int weight_sum = 0;
178
179 for (i = 0; i < num; i++) {
180 if (data_weight)
181 weight_sum += ip4[i].data_weight;
182 else
183 weight_sum += ip4[i].sig_weight;
184 }
185 return weight_sum;
186}
187#define ip4_weight_sum_data(x,y) ip4_weight_sum(x, y, true)
188#define ip4_weight_sum_sig(x,y) ip4_weight_sum(x, y, false)
189
190/* helper function to compute the sum of all (data or signaling) weights */
191static int ip6_weight_sum(const struct gprs_ns_ie_ip6_elem *ip6, unsigned int num,
192 bool data_weight)
193{
194 unsigned int i;
195 int weight_sum = 0;
196
197 for (i = 0; i < num; i++) {
198 if (data_weight)
199 weight_sum += ip6[i].data_weight;
200 else
201 weight_sum += ip6[i].sig_weight;
202 }
203 return weight_sum;
204}
205#define ip6_weight_sum_data(x,y) ip6_weight_sum(x, y, true)
206#define ip6_weight_sum_sig(x,y) ip6_weight_sum(x, y, false)
207
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100208static int nss_weight_sum(const struct ns2_sns_state *nss, bool data_weight)
209{
210 return ip4_weight_sum(nss->ip4_remote, nss->num_ip4_remote, data_weight) +
211 ip6_weight_sum(nss->ip6_remote, nss->num_ip6_remote, data_weight);
212}
213#define nss_weight_sum_data(nss) nss_weight_sum(nss, true)
214#define nss_weight_sum_sig(nss) nss_weight_sum(nss, false)
215
Alexander Couzens6a161492020-07-12 13:45:50 +0200216static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
217 const struct gprs_ns_ie_ip4_elem *ip4)
218{
219 struct osmo_sockaddr sa;
220 /* copy over. Both data structures use network byte order */
221 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
222 sa.u.sin.sin_port = ip4->udp_port;
223 sa.u.sin.sin_family = AF_INET;
224
Alexander Couzens38b19e82020-09-23 23:56:37 +0200225 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200226}
227
228static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
229 const struct gprs_ns_ie_ip6_elem *ip6)
230{
231 struct osmo_sockaddr sa;
232 /* copy over. Both data structures use network byte order */
233 sa.u.sin6.sin6_addr = ip6->ip_addr;
234 sa.u.sin6.sin6_port = ip6->udp_port;
235 sa.u.sin6.sin6_family = AF_INET;
236
Alexander Couzens38b19e82020-09-23 23:56:37 +0200237 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200238}
239
Alexander Couzens125298f2020-10-11 21:22:42 +0200240/*! Return the initial SNS remote socket address
241 * \param nse NS Entity
242 * \return address of the initial SNS connection; NULL in case of error
243 */
244const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
245{
246 struct ns2_sns_state *gss;
247
248 if (!nse->bss_sns_fi)
249 return NULL;
250
251 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100252 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200253}
254
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100255/*! called when a nsvc is beeing freed or the nsvc became dead */
256void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200257{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100258 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200259 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100260 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200261 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200262
263 if (!fi)
264 return;
265
266 gss = (struct ns2_sns_state *) fi->priv;
267 if (nsvc != gss->sns_nsvc)
268 return;
269
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100270 gss->sns_nsvc = NULL;
271 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200272 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100273 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200274 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100275 return;
276 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200277 }
278 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100279 /* the SNS is waiting for its first NS-VC to come up
280 * choose any other nsvc */
281 llist_for_each_entry(tmp, &nse->nsvc, list) {
282 if (nsvc != tmp) {
283 gss->sns_nsvc = tmp;
284 return;
285 }
286 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200287 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100288
Alexander Couzens67725e22021-02-15 02:37:03 +0100289 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200290}
291
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100292static void ns2_clear_ipv46_entries(struct ns2_sns_state *gss)
293{
294 TALLOC_FREE(gss->ip4_local);
295 TALLOC_FREE(gss->ip4_remote);
296 TALLOC_FREE(gss->ip6_local);
297 TALLOC_FREE(gss->ip6_remote);
298
299 gss->num_ip4_local = 0;
300 gss->num_ip4_remote = 0;
301 gss->num_ip6_local = 0;
302 gss->num_ip6_remote = 0;
303}
304
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100305static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
306 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200307{
308 struct gprs_ns2_inst *nsi = nse->nsi;
309 struct gprs_ns2_vc *nsvc;
310 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100311
312 /* for every bind, create a connection if bind type == IP */
313 llist_for_each_entry(bind, &nsi->binding, list) {
314 if (bind->ll != GPRS_NS2_LL_UDP)
315 continue;
316 /* ignore failed connection */
317 nsvc = gprs_ns2_ip_connect_inactive(bind,
318 remote,
319 nse, 0);
320 if (!nsvc) {
321 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
322 continue;
323 }
324
325 nsvc->sig_weight = sig_weight;
326 nsvc->data_weight = data_weight;
327 }
328}
329
330static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
331 struct gprs_ns2_nse *nse,
332 const struct gprs_ns_ie_ip4_elem *ip4)
333{
Alexander Couzensc068d862020-10-12 04:11:51 +0200334 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200335 /* copy over. Both data structures use network byte order */
336 remote.u.sin.sin_family = AF_INET;
337 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
338 remote.u.sin.sin_port = ip4->udp_port;
339
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100340 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200341}
342
343static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
344 struct gprs_ns2_nse *nse,
345 const struct gprs_ns_ie_ip6_elem *ip6)
346{
Alexander Couzens6a161492020-07-12 13:45:50 +0200347 struct osmo_sockaddr remote = {};
348 /* copy over. Both data structures use network byte order */
349 remote.u.sin6.sin6_family = AF_INET6;
350 remote.u.sin6.sin6_addr = ip6->ip_addr;
351 remote.u.sin6.sin6_port = ip6->udp_port;
352
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100353 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200354}
355
356
357static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
358{
359 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
360 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
361 struct gprs_ns2_vc *nsvc;
362 struct gprs_ns2_vc_bind *bind;
363 struct osmo_sockaddr remote = { };
364 unsigned int i;
365
366 for (i = 0; i < gss->num_ip4_remote; i++) {
367 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->ip4_remote[i];
368
369 remote.u.sin.sin_family = AF_INET;
370 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
371 remote.u.sin.sin_port = ip4->udp_port;
372
373 llist_for_each_entry(bind, &nse->nsi->binding, list) {
374 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100375 if (bind->ll != GPRS_NS2_LL_UDP)
376 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200377
378 llist_for_each_entry(nsvc, &nse->nsvc, list) {
379 if (nsvc->bind != bind)
380 continue;
381
Alexander Couzensc4229a42020-10-11 20:58:04 +0200382 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200383 found = true;
384 break;
385 }
386 }
387
388 if (!found) {
389 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
390 if (!nsvc) {
391 /* TODO: add to a list to send back a NS-STATUS */
392 continue;
393 }
394 }
395
396 /* update data / signalling weight */
397 nsvc->data_weight = ip4->data_weight;
398 nsvc->sig_weight = ip4->sig_weight;
399 nsvc->sns_only = false;
400 }
401 }
402
403 for (i = 0; i < gss->num_ip6_remote; i++) {
404 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->ip6_remote[i];
405
406 remote.u.sin6.sin6_family = AF_INET6;
407 remote.u.sin6.sin6_addr = ip6->ip_addr;
408 remote.u.sin6.sin6_port = ip6->udp_port;
409
410 llist_for_each_entry(bind, &nse->nsi->binding, list) {
411 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100412 if (bind->ll != GPRS_NS2_LL_UDP)
413 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200414
415 llist_for_each_entry(nsvc, &nse->nsvc, list) {
416 if (nsvc->bind != bind)
417 continue;
418
Alexander Couzensc4229a42020-10-11 20:58:04 +0200419 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200420 found = true;
421 break;
422 }
423 }
424
425 if (!found) {
426 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
427 if (!nsvc) {
428 /* TODO: add to a list to send back a NS-STATUS */
429 continue;
430 }
431 }
432
433 /* update data / signalling weight */
434 nsvc->data_weight = ip6->data_weight;
435 nsvc->sig_weight = ip6->sig_weight;
436 nsvc->sns_only = false;
437 }
438 }
439
440
441 return 0;
442}
443
444/* Add a given remote IPv4 element to gprs_sns_state */
445static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
446{
447 unsigned int i;
448
449 if (gss->num_ip4_remote >= gss->num_max_ip4_remote)
450 return -NS_CAUSE_INVAL_NR_NS_VC;
451
452 /* check for duplicates */
453 for (i = 0; i < gss->num_ip4_remote; i++) {
454 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
455 continue;
456 /* TODO: log message duplicate */
Alexander Couzens6a161492020-07-12 13:45:50 +0200457 return -NS_CAUSE_PROTO_ERR_UNSPEC;
458 }
459
460 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem,
461 gss->num_ip4_remote+1);
462 gss->ip4_remote[gss->num_ip4_remote] = *ip4;
463 gss->num_ip4_remote += 1;
464 return 0;
465}
466
467/* Remove a given remote IPv4 element from gprs_sns_state */
468static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
469{
470 unsigned int i;
471
472 for (i = 0; i < gss->num_ip4_remote; i++) {
473 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
474 continue;
475 /* all array elements < i remain as they are; all > i are shifted left by one */
476 memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1);
477 gss->num_ip4_remote -= 1;
478 return 0;
479 }
480 return -1;
481}
482
483/* update the weights for specified remote IPv4 */
484static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
485{
486 unsigned int i;
487
488 for (i = 0; i < gss->num_ip4_remote; i++) {
489 if (gss->ip4_remote[i].ip_addr != ip4->ip_addr ||
490 gss->ip4_remote[i].udp_port != ip4->udp_port)
491 continue;
492
493 gss->ip4_remote[i].sig_weight = ip4->sig_weight;
494 gss->ip4_remote[i].data_weight = ip4->data_weight;
495 return 0;
496 }
497 return -1;
498}
499
500/* Add a given remote IPv6 element to gprs_sns_state */
501static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
502{
503 if (gss->num_ip6_remote >= gss->num_max_ip6_remote)
504 return -NS_CAUSE_INVAL_NR_NS_VC;
505
506 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, struct gprs_ns_ie_ip6_elem,
507 gss->num_ip6_remote+1);
508 gss->ip6_remote[gss->num_ip6_remote] = *ip6;
509 gss->num_ip6_remote += 1;
510 return 0;
511}
512
513/* Remove a given remote IPv6 element from gprs_sns_state */
514static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
515{
516 unsigned int i;
517
518 for (i = 0; i < gss->num_ip6_remote; i++) {
519 if (memcmp(&gss->ip6_remote[i], ip6, sizeof(*ip6)))
520 continue;
521 /* all array elements < i remain as they are; all > i are shifted left by one */
522 memmove(&gss->ip6_remote[i], &gss->ip6_remote[i+1], gss->num_ip6_remote-i-1);
523 gss->num_ip6_remote -= 1;
524 return 0;
525 }
526 return -1;
527}
528
529/* update the weights for specified remote IPv6 */
530static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
531{
532 unsigned int i;
533
534 for (i = 0; i < gss->num_ip6_remote; i++) {
535 if (memcmp(&gss->ip6_remote[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
536 gss->ip6_remote[i].udp_port != ip6->udp_port)
537 continue;
538 gss->ip6_remote[i].sig_weight = ip6->sig_weight;
539 gss->ip6_remote[i].data_weight = ip6->data_weight;
540 return 0;
541 }
542 return -1;
543}
544
545static 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)
546{
547 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
548 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
549 struct gprs_ns2_vc *nsvc;
550 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200551 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200552 uint8_t new_signal;
553 uint8_t new_data;
554
555 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
556 * signalling weights of all the peer IP endpoints configured for this NSE is
557 * equal to zero or if the resulting sum of the data weights of all the peer IP
558 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
559 * SNS-ACK PDU with a cause code of "Invalid weights". */
560
561 if (ip4) {
562 if (update_remote_ip4_elem(gss, ip4))
563 return -NS_CAUSE_UNKN_IP_EP;
564
565 /* copy over. Both data structures use network byte order */
566 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
567 sa.u.sin.sin_port = ip4->udp_port;
568 sa.u.sin.sin_family = AF_INET;
569 new_signal = ip4->sig_weight;
570 new_data = ip4->data_weight;
571 } else if (ip6) {
572 if (update_remote_ip6_elem(gss, ip6))
573 return -NS_CAUSE_UNKN_IP_EP;
574
575 /* copy over. Both data structures use network byte order */
576 sa.u.sin6.sin6_addr = ip6->ip_addr;
577 sa.u.sin6.sin6_port = ip6->udp_port;
578 sa.u.sin6.sin6_family = AF_INET6;
579 new_signal = ip6->sig_weight;
580 new_data = ip6->data_weight;
581 } else {
582 OSMO_ASSERT(false);
583 }
584
585 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200586 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200587 /* all nsvc in NSE should be IP/UDP nsvc */
588 OSMO_ASSERT(remote);
589
590 if (osmo_sockaddr_cmp(&sa, remote))
591 continue;
592
593 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
594 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
595 nsvc->sig_weight, new_signal);
596
597 nsvc->data_weight = new_data;
598 nsvc->sig_weight = new_signal;
599 }
600
601 return 0;
602}
603
604static int do_sns_delete(struct osmo_fsm_inst *fi,
605 const struct gprs_ns_ie_ip4_elem *ip4,
606 const struct gprs_ns_ie_ip6_elem *ip6)
607{
608 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
609 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
610 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200611 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200612 struct osmo_sockaddr sa = {};
613
614 if (ip4) {
615 if (remove_remote_ip4_elem(gss, ip4) < 0)
616 return -NS_CAUSE_UNKN_IP_EP;
617 /* copy over. Both data structures use network byte order */
618 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
619 sa.u.sin.sin_port = ip4->udp_port;
620 sa.u.sin.sin_family = AF_INET;
621 } else if (ip6) {
622 if (remove_remote_ip6_elem(gss, ip6))
623 return -NS_CAUSE_UNKN_IP_EP;
624
625 /* copy over. Both data structures use network byte order */
626 sa.u.sin6.sin6_addr = ip6->ip_addr;
627 sa.u.sin6.sin6_port = ip6->udp_port;
628 sa.u.sin6.sin6_family = AF_INET6;
629 } else {
630 OSMO_ASSERT(false);
631 }
632
633 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200634 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200635 /* all nsvc in NSE should be IP/UDP nsvc */
636 OSMO_ASSERT(remote);
637 if (osmo_sockaddr_cmp(&sa, remote))
638 continue;
639
640 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
641 gprs_ns2_free_nsvc(nsvc);
642 }
643
644 return 0;
645}
646
647static int do_sns_add(struct osmo_fsm_inst *fi,
648 const struct gprs_ns_ie_ip4_elem *ip4,
649 const struct gprs_ns_ie_ip6_elem *ip6)
650{
651 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
652 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
653 struct gprs_ns2_vc *nsvc;
654 int rc = 0;
655
656 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
657 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
658 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
659 switch (gss->ip) {
660 case IPv4:
661 rc = add_remote_ip4_elem(gss, ip4);
662 break;
663 case IPv6:
664 rc = add_remote_ip6_elem(gss, ip6);
665 break;
666 default:
667 /* the gss->ip is initialized with the bss */
668 OSMO_ASSERT(false);
669 }
670
671 if (rc)
672 return rc;
673
674 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
675 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
676 * unspecified" */
677 switch (gss->ip) {
678 case IPv4:
679 nsvc = nsvc_by_ip4_elem(nse, ip4);
680 if (nsvc) {
681 /* the nsvc should be already in sync with the ip4 / ip6 elements */
682 return -NS_CAUSE_PROTO_ERR_UNSPEC;
683 }
684
685 /* TODO: failure case */
686 ns2_nsvc_create_ip4(fi, nse, ip4);
687 break;
688 case IPv6:
689 nsvc = nsvc_by_ip6_elem(nse, ip6);
690 if (nsvc) {
691 /* the nsvc should be already in sync with the ip4 / ip6 elements */
692 return -NS_CAUSE_PROTO_ERR_UNSPEC;
693 }
694
695 /* TODO: failure case */
696 ns2_nsvc_create_ip6(fi, nse, ip6);
697 break;
698 }
699
700 gprs_ns2_start_alive_all_nsvcs(nse);
701
702 return 0;
703}
704
705
706static void ns2_sns_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
707{
Alexander Couzense769f522020-12-07 07:37:07 +0100708 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200709}
710
711static void ns2_sns_st_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
712{
713 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
714 struct gprs_ns2_inst *nsi = nse->nsi;
715 struct tlv_parsed *tp = NULL;
716
717 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100718 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200719 tp = data;
720 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
721 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
722 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
723 /* TODO: What to do? */
724 } else {
725 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_BSS,
726 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
727 }
728 break;
729 default:
730 OSMO_ASSERT(0);
731 }
732}
733
Harald Welte24920e22021-03-04 13:03:27 +0100734static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200735{
736 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100737 struct gprs_ns_ie_ip4_elem *ip4_elems;
738 struct gprs_ns_ie_ip6_elem *ip6_elems;
739 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100740 struct ns2_sns_bind *sbind;
Alexander Couzense769f522020-12-07 07:37:07 +0100741 struct osmo_sockaddr *remote;
742 const struct osmo_sockaddr *sa;
743 struct osmo_sockaddr local;
744 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200745
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100746 ns2_clear_ipv46_entries(gss);
747
Alexander Couzense769f522020-12-07 07:37:07 +0100748 /* no initial available */
749 if (!gss->initial)
750 return;
751
752 remote = &gss->initial->saddr;
753
754 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100755 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100756 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100757 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100758 return;
759 }
760
Alexander Couzense769f522020-12-07 07:37:07 +0100761 switch (gss->ip) {
762 case IPv4:
763 ip4_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip4_elem) * count);
764 if (!ip4_elems)
765 return;
766
767 gss->ip4_local = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100768 llist_for_each_entry(sbind, &gss->binds, list) {
769 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100770 sa = gprs_ns2_ip_bind_sockaddr(bind);
771 if (!sa)
772 continue;
773
774 if (sa->u.sas.ss_family != AF_INET)
775 continue;
776
777 /* check if this is an specific bind */
778 if (sa->u.sin.sin_addr.s_addr == 0) {
779 if (osmo_sockaddr_local_ip(&local, remote))
780 continue;
781
782 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
783 } else {
784 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
785 }
786
787 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100788 ip4_elems->sig_weight = bind->sns_sig_weight;
789 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100790 ip4_elems++;
791 }
792
793 gss->num_ip4_local = count;
794 gss->num_max_ip4_remote = 4;
795 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->num_ip4_local, 8);
796 break;
797 case IPv6:
798 /* IPv6 */
799 ip6_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip6_elem) * count);
800 if (!ip6_elems)
801 return;
802
803 gss->ip6_local = ip6_elems;
804
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100805 llist_for_each_entry(sbind, &gss->binds, list) {
806 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100807 sa = gprs_ns2_ip_bind_sockaddr(bind);
808 if (!sa)
809 continue;
810
811 if (sa->u.sas.ss_family != AF_INET6)
812 continue;
813
814 /* check if this is an specific bind */
815 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
816 if (osmo_sockaddr_local_ip(&local, remote))
817 continue;
818
819 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
820 } else {
821 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
822 }
823
824 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100825 ip6_elems->sig_weight = bind->sns_sig_weight;
826 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100827
828 ip6_elems++;
829 }
830 gss->num_ip6_local = count;
831 gss->num_max_ip6_remote = 4;
832 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->num_ip6_local, 8);
833 break;
834 }
Harald Welte24920e22021-03-04 13:03:27 +0100835}
836
837/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
838static void ns2_sns_st_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
839{
840 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
841
842 /* on a generic failure, the timer callback will recover */
843 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
844 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
845 if (old_state != GPRS_SNS_ST_SIZE)
846 gss->N = 0;
847
848 gss->alive = false;
849
850 ns2_sns_compute_local_ep_from_binds(fi);
851
852 /* take the first bind or take the next bind */
853 if (!gss->initial_bind) {
854 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
855 } else {
856 if (gss->initial_bind->list.next != &gss->binds) {
857 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
858 } else {
859 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
860 }
861 }
862
863
864 /* setup the NSVC */
865 if (!gss->sns_nsvc) {
866 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
867 struct osmo_sockaddr *remote = &gss->initial->saddr;
868 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
869 if (!gss->sns_nsvc)
870 return;
871 gss->sns_nsvc->sns_only = true;
872 }
873
Alexander Couzense769f522020-12-07 07:37:07 +0100874
Alexander Couzens6a161492020-07-12 13:45:50 +0200875 if (gss->num_max_ip4_remote > 0)
876 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->num_max_ip4_remote, -1);
877 else
878 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->num_max_ip6_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200879}
880
881static void ns2_sns_st_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
882{
883 struct tlv_parsed *tp = NULL;
Alexander Couzens3df58862021-02-05 17:18:08 +0100884 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6a161492020-07-12 13:45:50 +0200885
886 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100887 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200888 tp = (struct tlv_parsed *) data;
889 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
890 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
891 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
892 /* TODO: What to do? */
893 } else {
Alexander Couzens3df58862021-02-05 17:18:08 +0100894 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_SGSN, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 3);
Alexander Couzens6a161492020-07-12 13:45:50 +0200895 }
896 break;
897 default:
898 OSMO_ASSERT(0);
899 }
900}
901
902static void ns2_sns_st_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
903{
904 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100905
906 if (old_state != GPRS_SNS_ST_CONFIG_BSS)
907 gss->N = 0;
908
Alexander Couzens6a161492020-07-12 13:45:50 +0200909 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200910 switch (gss->ip) {
911 case IPv4:
912 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100913 gss->ip4_local, gss->num_ip4_local,
914 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200915 break;
916 case IPv6:
917 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100918 NULL, 0,
919 gss->ip6_local, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200920 break;
921 }
922}
923
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100924/* calculate the timeout of the configured state. the configured
925 * state will fail if not at least one NS-VC is alive within X second.
926 */
927static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
928{
929 int secs;
930 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
931 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
932 secs += nsi->timeout[NS_TOUT_TNS_TEST];
933
934 return secs;
935}
Alexander Couzens6a161492020-07-12 13:45:50 +0200936
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100937/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
938static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200939{
940 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200941
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100942 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
943 const struct gprs_ns_ie_ip4_elem *v4_list;
944 unsigned int num_v4;
945 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
946 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +0200947
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100948 if (num_v4 && gss->ip6_remote)
949 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +0200950
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100951 /* realloc to the new size */
952 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote,
953 struct gprs_ns_ie_ip4_elem,
954 gss->num_ip4_remote + num_v4);
955 /* append the new entries to the end of the list */
956 memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list));
957 gss->num_ip4_remote += num_v4;
958
959 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
960 gss->num_ip4_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200961 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200962
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100963 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
964 const struct gprs_ns_ie_ip6_elem *v6_list;
965 unsigned int num_v6;
966 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
967 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
968
969 if (num_v6 && gss->ip4_remote)
970 return -NS_CAUSE_INVAL_NR_IPv6_EP;
971
972 /* realloc to the new size */
973 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote,
974 struct gprs_ns_ie_ip6_elem,
975 gss->num_ip6_remote + num_v6);
976 /* append the new entries to the end of the list */
977 memcpy(&gss->ip6_remote[gss->num_ip6_remote], v6_list, num_v6*sizeof(*v6_list));
978 gss->num_ip6_remote += num_v6;
979
980 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %u entries\n",
981 gss->num_ip6_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200982 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200983
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100984 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200985}
986
Alexander Couzens790a9632021-02-05 17:18:39 +0100987static void ns2_sns_st_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
988{
989 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
990
991 if (old_state != GPRS_SNS_ST_CONFIG_SGSN)
992 gss->N = 0;
993}
994
Alexander Couzens6a161492020-07-12 13:45:50 +0200995static void ns2_sns_st_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
996{
997 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100998 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
999 uint8_t cause;
1000 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001001
1002 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001003 case GPRS_SNS_EV_RX_CONFIG_END:
1004 case GPRS_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001005 rc = ns_sns_append_remote_eps(fi, data);
1006 if (rc < 0) {
1007 cause = -rc;
1008 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1009 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1010 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001011 }
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001012 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1013 /* check if sum of data / sig weights == 0 */
1014 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1015 cause = NS_CAUSE_INVAL_WEIGH;
1016 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1017 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1018 return;
1019 }
1020 create_missing_nsvcs(fi);
1021 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1022 /* start the test procedure on ALL NSVCs! */
1023 gprs_ns2_start_alive_all_nsvcs(nse);
1024 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1025 } else {
1026 /* just send CONFIG-ACK */
1027 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1028 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001029 }
1030 break;
1031 default:
1032 OSMO_ASSERT(0);
1033 }
1034}
1035
Alexander Couzens67725e22021-02-15 02:37:03 +01001036/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001037static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1038 struct ns2_sns_state *gss,
1039 struct tlv_parsed *tp)
1040{
1041 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1042 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1043 int num_v4 = 0, num_v6 = 0;
1044 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001045 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001046 int rc = 0;
1047
1048 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1049 * check uniqueness within the lists (no doublicate entries)
1050 * check not-known-by-us and sent back a list of unknown/known values
1051 * (abnormal behaviour according to 48.016)
1052 */
1053
1054 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1055 if (gss->ip == IPv4) {
1056 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1057 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1058 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1059 return;
1060 }
1061
1062 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1063 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001064 for (i = 0; i < num_v4; i++) {
1065 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001066 rc = do_sns_add(fi, &v4_list[i], NULL);
1067 if (rc < 0) {
1068 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001069 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001070 do_sns_delete(fi, &v4_list[j], NULL);
1071 cause = -rc;
1072 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1073 break;
1074 }
1075 }
1076 } else { /* IPv6 */
1077 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1078 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1079 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1080 return;
1081 }
1082
1083 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1084 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001085 for (i = 0; i < num_v6; i++) {
1086 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001087 rc = do_sns_add(fi, NULL, &v6_list[i]);
1088 if (rc < 0) {
1089 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001090 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001091 do_sns_delete(fi, NULL, &v6_list[j]);
1092 cause = -rc;
1093 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1094 break;
1095 }
1096 }
1097 }
1098
1099 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1100 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1101}
1102
1103static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1104 struct ns2_sns_state *gss,
1105 struct tlv_parsed *tp)
1106{
1107 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1108 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1109 int num_v4 = 0, num_v6 = 0;
1110 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001111 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001112 int rc = 0;
1113
1114 /* TODO: split up delete into v4 + v6
1115 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1116 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1117 */
1118 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1119 if (gss->ip == IPv4) {
1120 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1121 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1122 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001123 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001124 rc = do_sns_delete(fi, &v4_list[i], NULL);
1125 if (rc < 0) {
1126 cause = -rc;
1127 /* continue to delete others */
1128 }
1129 }
1130 if (cause != 0xff) {
1131 /* TODO: create list of not-deleted and return it */
1132 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1133 return;
1134 }
1135
1136 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1137 /* delete all NS-VCs for given IPv4 address */
1138 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1139 struct gprs_ns_ie_ip4_elem *ip4_remote;
1140 uint32_t ip_addr = *(uint32_t *)(ie+1);
1141 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1142 cause = NS_CAUSE_UNKN_IP_ADDR;
1143 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1144 return;
1145 }
1146 /* make a copy as do_sns_delete() will change the array underneath us */
1147 ip4_remote = talloc_memdup(fi, gss->ip4_remote,
1148 gss->num_ip4_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001149 for (i = 0; i < gss->num_ip4_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001150 if (ip4_remote[i].ip_addr == ip_addr) {
1151 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1152 if (rc < 0) {
1153 cause = -rc;
1154 /* continue to delete others */
1155 }
1156 }
1157 }
1158 talloc_free(ip4_remote);
1159 if (cause != 0xff) {
1160 /* TODO: create list of not-deleted and return it */
1161 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1162 return;
1163 }
1164 } else {
1165 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1166 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1167 return;
1168 }
1169 } else { /* IPv6 */
1170 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1171 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1172 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001173 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001174 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1175 if (rc < 0) {
1176 cause = -rc;
1177 /* continue to delete others */
1178 }
1179 }
1180 if (cause != 0xff) {
1181 /* TODO: create list of not-deleted and return it */
1182 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1183 return;
1184 }
1185 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1186 /* delete all NS-VCs for given IPv4 address */
1187 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1188 struct gprs_ns_ie_ip6_elem *ip6_remote;
1189 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001190 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001191 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1192 cause = NS_CAUSE_UNKN_IP_ADDR;
1193 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1194 return;
1195 }
1196 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1197 /* make a copy as do_sns_delete() will change the array underneath us */
1198 ip6_remote = talloc_memdup(fi, gss->ip6_remote,
1199 gss->num_ip6_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001200 for (i = 0; i < gss->num_ip6_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001201 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1202 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1203 if (rc < 0) {
1204 cause = -rc;
1205 /* continue to delete others */
1206 }
1207 }
1208 }
1209
1210 talloc_free(ip6_remote);
1211 if (cause != 0xff) {
1212 /* TODO: create list of not-deleted and return it */
1213 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1214 return;
1215 }
1216 } else {
1217 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1218 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1219 return;
1220 }
1221 }
1222 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1223}
1224
1225static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1226 struct ns2_sns_state *gss,
1227 struct tlv_parsed *tp)
1228{
1229 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1230 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1231 int num_v4 = 0, num_v6 = 0;
1232 uint8_t trans_id, cause = 0xff;
1233 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001234 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001235
1236 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1237 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1238 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1239 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001240 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001241 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1242 if (rc < 0) {
1243 cause = -rc;
1244 /* continue to others */
1245 }
1246 }
1247 if (cause != 0xff) {
1248 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1249 return;
1250 }
1251 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1252 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1253 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001254 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001255 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1256 if (rc < 0) {
1257 cause = -rc;
1258 /* continue to others */
1259 }
1260 }
1261 if (cause != 0xff) {
1262 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1263 return;
1264 }
1265 } else {
1266 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1267 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1268 return;
1269 }
1270 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1271}
1272
1273static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1274{
1275 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1276 struct tlv_parsed *tp = data;
1277
1278 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001279 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001280 ns2_sns_st_configured_add(fi, gss, tp);
1281 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001282 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001283 ns2_sns_st_configured_delete(fi, gss, tp);
1284 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001285 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001286 ns2_sns_st_configured_change(fi, gss, tp);
1287 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001288 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001289 osmo_timer_del(&fi->timer);
1290 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001291 }
1292}
1293
1294static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1295{
1296 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens138b96f2021-01-25 16:23:29 +01001297 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001298}
1299
1300static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1301 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001302 .in_event_mask = 0, /* handled by all_state_action */
Alexander Couzens6a161492020-07-12 13:45:50 +02001303 .out_state_mask = S(GPRS_SNS_ST_SIZE),
1304 .name = "UNCONFIGURED",
1305 .action = ns2_sns_st_unconfigured,
1306 },
1307 [GPRS_SNS_ST_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001308 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001309 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1310 S(GPRS_SNS_ST_SIZE) |
1311 S(GPRS_SNS_ST_CONFIG_BSS),
1312 .name = "SIZE",
1313 .action = ns2_sns_st_size,
1314 .onenter = ns2_sns_st_size_onenter,
1315 },
1316 [GPRS_SNS_ST_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001317 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001318 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1319 S(GPRS_SNS_ST_CONFIG_BSS) |
1320 S(GPRS_SNS_ST_CONFIG_SGSN) |
1321 S(GPRS_SNS_ST_SIZE),
1322 .name = "CONFIG_BSS",
1323 .action = ns2_sns_st_config_bss,
1324 .onenter = ns2_sns_st_config_bss_onenter,
1325 },
1326 [GPRS_SNS_ST_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001327 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1328 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001329 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1330 S(GPRS_SNS_ST_CONFIG_SGSN) |
1331 S(GPRS_SNS_ST_CONFIGURED) |
1332 S(GPRS_SNS_ST_SIZE),
1333 .name = "CONFIG_SGSN",
1334 .action = ns2_sns_st_config_sgsn,
Alexander Couzens790a9632021-02-05 17:18:39 +01001335 .onenter = ns2_sns_st_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001336 },
1337 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001338 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1339 S(GPRS_SNS_EV_RX_DELETE) |
1340 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1341 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001342 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1343 S(GPRS_SNS_ST_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001344 .name = "CONFIGURED",
1345 .action = ns2_sns_st_configured,
1346 .onenter = ns2_sns_st_configured_onenter,
1347 },
1348};
1349
1350static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1351{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001352 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001353 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1354 struct gprs_ns2_inst *nsi = nse->nsi;
1355
Alexander Couzens90ee9632020-12-07 06:18:32 +01001356 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001357 switch (fi->T) {
1358 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001359 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1360 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001361 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001362 } else {
Alexander Couzenscc65a252020-12-21 14:03:58 +01001363 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nsi->timeout[NS_TOUT_TSNS_PROV], 1);
Alexander Couzensa367d082020-12-21 14:06:24 +01001364 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001365 break;
1366 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001367 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001368 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 +01001369 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001370 } else {
Alexander Couzenscc65a252020-12-21 14:03:58 +01001371 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_BSS, nsi->timeout[NS_TOUT_TSNS_PROV], 2);
Alexander Couzensa367d082020-12-21 14:06:24 +01001372 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001373 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001374 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001375 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1376 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 +01001377 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001378 } else {
1379 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_SGSN, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
1380 }
1381 break;
1382 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001383 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 +01001384 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001385 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001386 }
1387 return 0;
1388}
1389
Harald Welte9e37bf42021-03-02 20:48:31 +01001390/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001391static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1392{
1393 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001394 struct ns2_sns_bind *sbind;
1395 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001396
Alexander Couzense769f522020-12-07 07:37:07 +01001397 switch (event) {
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001398 case GPRS_SNS_EV_REQ_ADD_BIND:
1399 sbind = data;
1400 switch (fi->state) {
1401 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001402 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001403 break;
1404 case GPRS_SNS_ST_SIZE:
1405 /* TODO: add the ip4 element to the list */
1406 break;
1407 case GPRS_SNS_ST_CONFIG_BSS:
1408 case GPRS_SNS_ST_CONFIG_SGSN:
1409 case GPRS_SNS_ST_CONFIGURED:
1410 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1411 break;
1412 }
1413 break;
1414 case GPRS_SNS_EV_REQ_DELETE_BIND:
1415 sbind = data;
1416 switch (fi->state) {
1417 case GPRS_SNS_ST_UNCONFIGURED:
1418 break;
1419 case GPRS_SNS_ST_SIZE:
1420 /* TODO: remove the ip4 element from the list */
1421 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1422 if (nsvc->bind == sbind->bind) {
1423 gprs_ns2_free_nsvc(nsvc);
1424 }
1425 }
1426 break;
1427 case GPRS_SNS_ST_CONFIG_BSS:
1428 case GPRS_SNS_ST_CONFIG_SGSN:
1429 case GPRS_SNS_ST_CONFIGURED:
1430 /* TODO: do an delete SNS-IP procedure */
1431 /* TODO: remove the ip4 element to the list */
1432 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1433 if (nsvc->bind == sbind->bind) {
1434 gprs_ns2_free_nsvc(nsvc);
1435 }
1436 }
1437 break;
1438 }
1439 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1440 talloc_free(sbind);
1441 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001442 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001443}
1444
Harald Welte9e37bf42021-03-02 20:48:31 +01001445/* allstate-action for BSS role */
1446static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1447{
1448 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1449 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1450
1451 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
1452 switch (event) {
1453 case GPRS_SNS_EV_REQ_NO_NSVC:
1454 /* ignore reselection running */
1455 if (gss->reselection_running)
1456 break;
1457
1458 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1459 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1460 break;
1461 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
1462 /* tear down previous state
1463 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1464 gss->reselection_running = true;
1465 gprs_ns2_free_nsvcs(nse);
1466 ns2_clear_ipv46_entries(gss);
1467
1468 /* Choose the next sns endpoint. */
1469 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds)) {
1470 gss->initial = NULL;
1471 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1472 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1473 return;
1474 } else if (!gss->initial) {
1475 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1476 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1477 /* last entry, continue with first */
1478 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1479 } else {
1480 /* next element is an entry */
1481 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1482 }
1483
1484 gss->reselection_running = false;
1485 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 1);
1486 break;
1487 default:
1488 ns2_sns_st_all_action(fi, event, data);
1489 break;
1490 }
1491}
1492
Alexander Couzens6a161492020-07-12 13:45:50 +02001493static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1494 .name = "GPRS-NS2-SNS-BSS",
1495 .states = ns2_sns_bss_states,
1496 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001497 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1498 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001499 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1500 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001501 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001502 .cleanup = NULL,
1503 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001504 .event_names = gprs_sns_event_names,
1505 .pre_term = NULL,
1506 .log_subsys = DLNS,
1507};
1508
Harald Welte5bef2cc2020-09-18 22:33:24 +02001509/*! Allocate an IP-SNS FSM for the BSS side.
1510 * \param[in] nse NS Entity in which the FSM runs
1511 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001512 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001513struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1514 const char *id)
1515{
1516 struct osmo_fsm_inst *fi;
1517 struct ns2_sns_state *gss;
1518
1519 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1520 if (!fi)
1521 return fi;
1522
1523 gss = talloc_zero(fi, struct ns2_sns_state);
1524 if (!gss)
1525 goto err;
1526
1527 fi->priv = gss;
1528 gss->nse = nse;
Alexander Couzense769f522020-12-07 07:37:07 +01001529 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001530 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001531
1532 return fi;
1533err:
1534 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1535 return NULL;
1536}
1537
Harald Welte5bef2cc2020-09-18 22:33:24 +02001538/*! main entry point for receiving SNS messages from the network.
1539 * \param[in] nsvc NS-VC on which the message was received
1540 * \param[in] msg message buffer of the IP-SNS message
1541 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001542 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001543int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001544{
1545 struct gprs_ns2_nse *nse = nsvc->nse;
1546 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1547 uint16_t nsei = nsvc->nse->nsei;
1548 struct osmo_fsm_inst *fi;
1549
1550 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001551 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1552 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001553 return -EINVAL;
1554 }
1555
Alexander Couzens6a161492020-07-12 13:45:50 +02001556 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1557 fi = nse->bss_sns_fi;
1558
Harald Weltef2949742021-01-20 14:54:14 +01001559 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1560 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1561
Alexander Couzens6a161492020-07-12 13:45:50 +02001562 switch (nsh->pdu_type) {
1563 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001564 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001565 break;
1566 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001567 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001568 break;
1569 case SNS_PDUT_CONFIG:
1570 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001571 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001572 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001573 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001574 break;
1575 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001576 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001577 break;
1578 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001579 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001580 break;
1581 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001582 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001583 break;
1584 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001585 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001586 break;
1587 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001588 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001589 break;
1590 default:
Harald Weltef2949742021-01-20 14:54:14 +01001591 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1592 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001593 return -EINVAL;
1594 }
1595
1596 return 0;
1597}
1598
1599#include <osmocom/vty/vty.h>
1600#include <osmocom/vty/misc.h>
1601
Harald Welte1262c4f2021-01-19 20:58:33 +01001602static 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 +02001603{
1604 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001605 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001606 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1607}
1608
Harald Welte1262c4f2021-01-19 20:58:33 +01001609static 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 +02001610{
1611 char ip_addr[INET6_ADDRSTRLEN] = {};
1612 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1613 strcpy(ip_addr, "Invalid IPv6");
1614
Harald Welte1262c4f2021-01-19 20:58:33 +01001615 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001616 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1617}
1618
Harald Welte5bef2cc2020-09-18 22:33:24 +02001619/*! Dump the IP-SNS state to a vty.
1620 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001621 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001622 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1623 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001624void 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 +02001625{
1626 struct ns2_sns_state *gss;
1627 unsigned int i;
1628
1629 if (!nse->bss_sns_fi)
1630 return;
1631
Harald Welte1262c4f2021-01-19 20:58:33 +01001632 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001633 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1634
Harald Welte1262c4f2021-01-19 20:58:33 +01001635 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1636 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001637
1638 if (gss->num_ip4_local && gss->num_ip4_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001639 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001640 for (i = 0; i < gss->num_ip4_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001641 vty_dump_sns_ip4(vty, prefix, &gss->ip4_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001642
Harald Welte1262c4f2021-01-19 20:58:33 +01001643 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001644 for (i = 0; i < gss->num_ip4_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001645 vty_dump_sns_ip4(vty, prefix, &gss->ip4_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001646 }
1647
1648 if (gss->num_ip6_local && gss->num_ip6_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001649 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001650 for (i = 0; i < gss->num_ip6_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001651 vty_dump_sns_ip6(vty, prefix, &gss->ip6_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001652
Harald Welte1262c4f2021-01-19 20:58:33 +01001653 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001654 for (i = 0; i < gss->num_ip6_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001655 vty_dump_sns_ip6(vty, prefix, &gss->ip6_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001656 }
1657}
1658
Alexander Couzens412bc342020-11-19 05:24:37 +01001659/*! write IP-SNS to a vty
1660 * \param[in] vty VTY to which the state shall be printed
1661 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001662void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001663{
1664 struct ns2_sns_state *gss;
1665 struct osmo_sockaddr_str addr_str;
1666 struct sns_endpoint *endpoint;
1667
1668 if (!nse->bss_sns_fi)
1669 return;
1670
1671 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1672 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001673 /* It's unlikely that an error happens, but let's better be safe. */
1674 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1675 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001676 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001677 }
1678}
1679
Alexander Couzense769f522020-12-07 07:37:07 +01001680static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1681 const struct osmo_sockaddr *saddr)
1682{
1683 struct sns_endpoint *endpoint;
1684
1685 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1686 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1687 return endpoint;
1688 }
1689
1690 return NULL;
1691}
1692
1693/*! gprs_ns2_sns_add_endpoint
1694 * \param[in] nse
1695 * \param[in] sockaddr
1696 * \return
1697 */
1698int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1699 const struct osmo_sockaddr *saddr)
1700{
1701 struct ns2_sns_state *gss;
1702 struct sns_endpoint *endpoint;
1703 bool do_selection = false;
1704
1705 if (nse->ll != GPRS_NS2_LL_UDP) {
1706 return -EINVAL;
1707 }
1708
Alexander Couzens138b96f2021-01-25 16:23:29 +01001709 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001710 return -EINVAL;
1711 }
1712
1713 gss = nse->bss_sns_fi->priv;
1714
1715 if (ns2_get_sns_endpoint(gss, saddr))
1716 return -EADDRINUSE;
1717
1718 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1719 if (!endpoint)
1720 return -ENOMEM;
1721
1722 endpoint->saddr = *saddr;
1723 if (llist_empty(&gss->sns_endpoints))
1724 do_selection = true;
1725
1726 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1727 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001728 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001729
1730 return 0;
1731}
1732
1733/*! gprs_ns2_sns_del_endpoint
1734 * \param[in] nse
1735 * \param[in] sockaddr
1736 * \return 0 on success, otherwise < 0
1737 */
1738int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1739 const struct osmo_sockaddr *saddr)
1740{
1741 struct ns2_sns_state *gss;
1742 struct sns_endpoint *endpoint;
1743
1744 if (nse->ll != GPRS_NS2_LL_UDP) {
1745 return -EINVAL;
1746 }
1747
Alexander Couzens138b96f2021-01-25 16:23:29 +01001748 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001749 return -EINVAL;
1750 }
1751
1752 gss = nse->bss_sns_fi->priv;
1753 endpoint = ns2_get_sns_endpoint(gss, saddr);
1754 if (!endpoint)
1755 return -ENOENT;
1756
1757 /* if this is an unused SNS endpoint it's done */
1758 if (gss->initial != endpoint) {
1759 llist_del(&endpoint->list);
1760 talloc_free(endpoint);
1761 return 0;
1762 }
1763
Alexander Couzens67725e22021-02-15 02:37:03 +01001764 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001765 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001766 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001767 "Closing all NS-VC and restart SNS-SIZE procedure"
1768 "with a remaining SNS endpoint.\n");
1769
1770 /* Continue with the next endpoint in the list.
1771 * Special case if the endpoint is at the start or end of the list */
1772 if (endpoint->list.prev == &gss->sns_endpoints ||
1773 endpoint->list.next == &gss->sns_endpoints)
1774 gss->initial = NULL;
1775 else
1776 gss->initial = llist_entry(endpoint->list.next->prev,
1777 struct sns_endpoint,
1778 list);
1779
1780 llist_del(&endpoint->list);
1781 gprs_ns2_free_nsvcs(nse);
1782 talloc_free(endpoint);
1783
1784 return 0;
1785}
1786
1787/*! gprs_ns2_sns_count
1788 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1789 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1790 */
1791int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1792{
1793 struct ns2_sns_state *gss;
1794 struct sns_endpoint *endpoint;
1795 int count = 0;
1796
1797 if (nse->ll != GPRS_NS2_LL_UDP) {
1798 return -EINVAL;
1799 }
1800
Alexander Couzens138b96f2021-01-25 16:23:29 +01001801 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001802 return -EINVAL;
1803 }
1804
1805 gss = nse->bss_sns_fi->priv;
1806 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1807 count++;
1808
1809 return count;
1810}
1811
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001812void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1813{
1814 struct ns2_sns_state *gss;
1815 struct gprs_ns2_vc *tmp;
1816
1817 if (!nse->bss_sns_fi)
1818 return;
1819
1820 gss = nse->bss_sns_fi->priv;
1821 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1822 return;
1823
1824 if (alive == gss->alive)
1825 return;
1826
1827 /* check if this is the current SNS NS-VC */
1828 if (nsvc == gss->sns_nsvc) {
1829 /* only replace the SNS NS-VC if there are other alive NS-VC.
1830 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1831 * and couldn't confirm yet if the NS-VC comes up */
1832 if (gss->alive && !alive)
1833 ns2_sns_replace_nsvc(nsvc);
1834 }
1835
1836 if (alive) {
1837 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001838 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001839 } else {
1840 /* is there at least another alive nsvc? */
1841 llist_for_each_entry(tmp, &nse->nsvc, list) {
1842 if (ns2_vc_is_unblocked(tmp))
1843 return;
1844 }
1845
1846 /* all NS-VC have failed */
1847 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001848 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001849 }
1850}
1851
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001852int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1853 struct gprs_ns2_vc_bind *bind)
1854{
1855 struct ns2_sns_state *gss;
1856 struct ns2_sns_bind *tmp;
1857
1858 OSMO_ASSERT(nse->bss_sns_fi);
1859 gss = nse->bss_sns_fi->priv;
1860
1861 if (!gprs_ns2_is_ip_bind(bind)) {
1862 return -EINVAL;
1863 }
1864
1865 if (!llist_empty(&gss->binds)) {
1866 llist_for_each_entry(tmp, &gss->binds, list) {
1867 if (tmp->bind == bind)
1868 return -EALREADY;
1869 }
1870 }
1871
1872 tmp = talloc_zero(gss, struct ns2_sns_bind);
1873 if (!tmp)
1874 return -ENOMEM;
1875 tmp->bind = bind;
1876 llist_add_tail(&tmp->list, &gss->binds);
1877
1878 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
1879 return 0;
1880}
1881
1882/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
1883int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
1884 struct gprs_ns2_vc_bind *bind)
1885{
1886 struct ns2_sns_state *gss;
1887 struct ns2_sns_bind *tmp, *tmp2;
1888 bool found = false;
1889
1890 if (!nse->bss_sns_fi)
1891 return -EINVAL;
1892
1893 gss = nse->bss_sns_fi->priv;
1894 if (gss->initial_bind && gss->initial_bind->bind == bind) {
1895 if (gss->initial_bind->list.prev == &gss->binds)
1896 gss->initial_bind = NULL;
1897 else
1898 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
1899 }
1900
1901 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
1902 if (tmp->bind == bind) {
1903 llist_del(&tmp->list);
1904 found = true;
1905 }
1906 }
1907
1908 if (!found)
1909 return -ENOENT;
1910
1911 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
1912 return 0;
1913}
1914
Alexander Couzensc4704762021-02-08 23:13:12 +01001915/* Update SNS weights
1916 * \param[in] nsvc the NSVC which should be updated
1917 */
1918void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
1919{
1920 /* TODO: implement weights after binds per sns implemented */
1921}
1922
Alexander Couzens6a161492020-07-12 13:45:50 +02001923/* initialize osmo_ctx on main tread */
1924static __attribute__((constructor)) void on_dso_load_ctx(void)
1925{
1926 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
1927}