blob: 715dd8ef59a6fa1be5fad033609ebd3ef8b4bef8 [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
6/* (C) 2018 by Harald Welte <laforge@gnumonks.org>
7 * (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
208static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
209 const struct gprs_ns_ie_ip4_elem *ip4)
210{
211 struct osmo_sockaddr sa;
212 /* copy over. Both data structures use network byte order */
213 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
214 sa.u.sin.sin_port = ip4->udp_port;
215 sa.u.sin.sin_family = AF_INET;
216
Alexander Couzens38b19e82020-09-23 23:56:37 +0200217 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200218}
219
220static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
221 const struct gprs_ns_ie_ip6_elem *ip6)
222{
223 struct osmo_sockaddr sa;
224 /* copy over. Both data structures use network byte order */
225 sa.u.sin6.sin6_addr = ip6->ip_addr;
226 sa.u.sin6.sin6_port = ip6->udp_port;
227 sa.u.sin6.sin6_family = AF_INET;
228
Alexander Couzens38b19e82020-09-23 23:56:37 +0200229 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200230}
231
Alexander Couzens125298f2020-10-11 21:22:42 +0200232/*! Return the initial SNS remote socket address
233 * \param nse NS Entity
234 * \return address of the initial SNS connection; NULL in case of error
235 */
236const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
237{
238 struct ns2_sns_state *gss;
239
240 if (!nse->bss_sns_fi)
241 return NULL;
242
243 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100244 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200245}
246
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100247/*! called when a nsvc is beeing freed or the nsvc became dead */
248void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200249{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100250 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200251 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100252 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200253 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200254
255 if (!fi)
256 return;
257
258 gss = (struct ns2_sns_state *) fi->priv;
259 if (nsvc != gss->sns_nsvc)
260 return;
261
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100262 gss->sns_nsvc = NULL;
263 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200264 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100265 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200266 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100267 return;
268 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200269 }
270 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100271 /* the SNS is waiting for its first NS-VC to come up
272 * choose any other nsvc */
273 llist_for_each_entry(tmp, &nse->nsvc, list) {
274 if (nsvc != tmp) {
275 gss->sns_nsvc = tmp;
276 return;
277 }
278 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200279 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100280
Alexander Couzens67725e22021-02-15 02:37:03 +0100281 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200282}
283
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100284static void ns2_clear_ipv46_entries(struct ns2_sns_state *gss)
285{
286 TALLOC_FREE(gss->ip4_local);
287 TALLOC_FREE(gss->ip4_remote);
288 TALLOC_FREE(gss->ip6_local);
289 TALLOC_FREE(gss->ip6_remote);
290
291 gss->num_ip4_local = 0;
292 gss->num_ip4_remote = 0;
293 gss->num_ip6_local = 0;
294 gss->num_ip6_remote = 0;
295}
296
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100297static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
298 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200299{
300 struct gprs_ns2_inst *nsi = nse->nsi;
301 struct gprs_ns2_vc *nsvc;
302 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100303
304 /* for every bind, create a connection if bind type == IP */
305 llist_for_each_entry(bind, &nsi->binding, list) {
306 if (bind->ll != GPRS_NS2_LL_UDP)
307 continue;
308 /* ignore failed connection */
309 nsvc = gprs_ns2_ip_connect_inactive(bind,
310 remote,
311 nse, 0);
312 if (!nsvc) {
313 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
314 continue;
315 }
316
317 nsvc->sig_weight = sig_weight;
318 nsvc->data_weight = data_weight;
319 }
320}
321
322static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
323 struct gprs_ns2_nse *nse,
324 const struct gprs_ns_ie_ip4_elem *ip4)
325{
Alexander Couzensc068d862020-10-12 04:11:51 +0200326 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200327 /* copy over. Both data structures use network byte order */
328 remote.u.sin.sin_family = AF_INET;
329 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
330 remote.u.sin.sin_port = ip4->udp_port;
331
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100332 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200333}
334
335static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
336 struct gprs_ns2_nse *nse,
337 const struct gprs_ns_ie_ip6_elem *ip6)
338{
Alexander Couzens6a161492020-07-12 13:45:50 +0200339 struct osmo_sockaddr remote = {};
340 /* copy over. Both data structures use network byte order */
341 remote.u.sin6.sin6_family = AF_INET6;
342 remote.u.sin6.sin6_addr = ip6->ip_addr;
343 remote.u.sin6.sin6_port = ip6->udp_port;
344
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100345 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200346}
347
348
349static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
350{
351 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
352 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
353 struct gprs_ns2_vc *nsvc;
354 struct gprs_ns2_vc_bind *bind;
355 struct osmo_sockaddr remote = { };
356 unsigned int i;
357
358 for (i = 0; i < gss->num_ip4_remote; i++) {
359 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->ip4_remote[i];
360
361 remote.u.sin.sin_family = AF_INET;
362 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
363 remote.u.sin.sin_port = ip4->udp_port;
364
365 llist_for_each_entry(bind, &nse->nsi->binding, list) {
366 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100367 if (bind->ll != GPRS_NS2_LL_UDP)
368 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200369
370 llist_for_each_entry(nsvc, &nse->nsvc, list) {
371 if (nsvc->bind != bind)
372 continue;
373
Alexander Couzensc4229a42020-10-11 20:58:04 +0200374 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200375 found = true;
376 break;
377 }
378 }
379
380 if (!found) {
381 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
382 if (!nsvc) {
383 /* TODO: add to a list to send back a NS-STATUS */
384 continue;
385 }
386 }
387
388 /* update data / signalling weight */
389 nsvc->data_weight = ip4->data_weight;
390 nsvc->sig_weight = ip4->sig_weight;
391 nsvc->sns_only = false;
392 }
393 }
394
395 for (i = 0; i < gss->num_ip6_remote; i++) {
396 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->ip6_remote[i];
397
398 remote.u.sin6.sin6_family = AF_INET6;
399 remote.u.sin6.sin6_addr = ip6->ip_addr;
400 remote.u.sin6.sin6_port = ip6->udp_port;
401
402 llist_for_each_entry(bind, &nse->nsi->binding, list) {
403 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100404 if (bind->ll != GPRS_NS2_LL_UDP)
405 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200406
407 llist_for_each_entry(nsvc, &nse->nsvc, list) {
408 if (nsvc->bind != bind)
409 continue;
410
Alexander Couzensc4229a42020-10-11 20:58:04 +0200411 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200412 found = true;
413 break;
414 }
415 }
416
417 if (!found) {
418 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
419 if (!nsvc) {
420 /* TODO: add to a list to send back a NS-STATUS */
421 continue;
422 }
423 }
424
425 /* update data / signalling weight */
426 nsvc->data_weight = ip6->data_weight;
427 nsvc->sig_weight = ip6->sig_weight;
428 nsvc->sns_only = false;
429 }
430 }
431
432
433 return 0;
434}
435
436/* Add a given remote IPv4 element to gprs_sns_state */
437static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
438{
439 unsigned int i;
440
441 if (gss->num_ip4_remote >= gss->num_max_ip4_remote)
442 return -NS_CAUSE_INVAL_NR_NS_VC;
443
444 /* check for duplicates */
445 for (i = 0; i < gss->num_ip4_remote; i++) {
446 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
447 continue;
448 /* TODO: log message duplicate */
449 /* TODO: check if this is the correct cause code */
450 return -NS_CAUSE_PROTO_ERR_UNSPEC;
451 }
452
453 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem,
454 gss->num_ip4_remote+1);
455 gss->ip4_remote[gss->num_ip4_remote] = *ip4;
456 gss->num_ip4_remote += 1;
457 return 0;
458}
459
460/* Remove a given remote IPv4 element from gprs_sns_state */
461static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
462{
463 unsigned int i;
464
465 for (i = 0; i < gss->num_ip4_remote; i++) {
466 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
467 continue;
468 /* all array elements < i remain as they are; all > i are shifted left by one */
469 memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1);
470 gss->num_ip4_remote -= 1;
471 return 0;
472 }
473 return -1;
474}
475
476/* update the weights for specified remote IPv4 */
477static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
478{
479 unsigned int i;
480
481 for (i = 0; i < gss->num_ip4_remote; i++) {
482 if (gss->ip4_remote[i].ip_addr != ip4->ip_addr ||
483 gss->ip4_remote[i].udp_port != ip4->udp_port)
484 continue;
485
486 gss->ip4_remote[i].sig_weight = ip4->sig_weight;
487 gss->ip4_remote[i].data_weight = ip4->data_weight;
488 return 0;
489 }
490 return -1;
491}
492
493/* Add a given remote IPv6 element to gprs_sns_state */
494static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
495{
496 if (gss->num_ip6_remote >= gss->num_max_ip6_remote)
497 return -NS_CAUSE_INVAL_NR_NS_VC;
498
499 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, struct gprs_ns_ie_ip6_elem,
500 gss->num_ip6_remote+1);
501 gss->ip6_remote[gss->num_ip6_remote] = *ip6;
502 gss->num_ip6_remote += 1;
503 return 0;
504}
505
506/* Remove a given remote IPv6 element from gprs_sns_state */
507static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
508{
509 unsigned int i;
510
511 for (i = 0; i < gss->num_ip6_remote; i++) {
512 if (memcmp(&gss->ip6_remote[i], ip6, sizeof(*ip6)))
513 continue;
514 /* all array elements < i remain as they are; all > i are shifted left by one */
515 memmove(&gss->ip6_remote[i], &gss->ip6_remote[i+1], gss->num_ip6_remote-i-1);
516 gss->num_ip6_remote -= 1;
517 return 0;
518 }
519 return -1;
520}
521
522/* update the weights for specified remote IPv6 */
523static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
524{
525 unsigned int i;
526
527 for (i = 0; i < gss->num_ip6_remote; i++) {
528 if (memcmp(&gss->ip6_remote[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
529 gss->ip6_remote[i].udp_port != ip6->udp_port)
530 continue;
531 gss->ip6_remote[i].sig_weight = ip6->sig_weight;
532 gss->ip6_remote[i].data_weight = ip6->data_weight;
533 return 0;
534 }
535 return -1;
536}
537
538static 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)
539{
540 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
541 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
542 struct gprs_ns2_vc *nsvc;
543 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200544 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200545 uint8_t new_signal;
546 uint8_t new_data;
547
548 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
549 * signalling weights of all the peer IP endpoints configured for this NSE is
550 * equal to zero or if the resulting sum of the data weights of all the peer IP
551 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
552 * SNS-ACK PDU with a cause code of "Invalid weights". */
553
554 if (ip4) {
555 if (update_remote_ip4_elem(gss, ip4))
556 return -NS_CAUSE_UNKN_IP_EP;
557
558 /* copy over. Both data structures use network byte order */
559 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
560 sa.u.sin.sin_port = ip4->udp_port;
561 sa.u.sin.sin_family = AF_INET;
562 new_signal = ip4->sig_weight;
563 new_data = ip4->data_weight;
564 } else if (ip6) {
565 if (update_remote_ip6_elem(gss, ip6))
566 return -NS_CAUSE_UNKN_IP_EP;
567
568 /* copy over. Both data structures use network byte order */
569 sa.u.sin6.sin6_addr = ip6->ip_addr;
570 sa.u.sin6.sin6_port = ip6->udp_port;
571 sa.u.sin6.sin6_family = AF_INET6;
572 new_signal = ip6->sig_weight;
573 new_data = ip6->data_weight;
574 } else {
575 OSMO_ASSERT(false);
576 }
577
578 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200579 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200580 /* all nsvc in NSE should be IP/UDP nsvc */
581 OSMO_ASSERT(remote);
582
583 if (osmo_sockaddr_cmp(&sa, remote))
584 continue;
585
586 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
587 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
588 nsvc->sig_weight, new_signal);
589
590 nsvc->data_weight = new_data;
591 nsvc->sig_weight = new_signal;
592 }
593
594 return 0;
595}
596
597static int do_sns_delete(struct osmo_fsm_inst *fi,
598 const struct gprs_ns_ie_ip4_elem *ip4,
599 const struct gprs_ns_ie_ip6_elem *ip6)
600{
601 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
602 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
603 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200604 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200605 struct osmo_sockaddr sa = {};
606
607 if (ip4) {
608 if (remove_remote_ip4_elem(gss, ip4) < 0)
609 return -NS_CAUSE_UNKN_IP_EP;
610 /* copy over. Both data structures use network byte order */
611 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
612 sa.u.sin.sin_port = ip4->udp_port;
613 sa.u.sin.sin_family = AF_INET;
614 } else if (ip6) {
615 if (remove_remote_ip6_elem(gss, ip6))
616 return -NS_CAUSE_UNKN_IP_EP;
617
618 /* copy over. Both data structures use network byte order */
619 sa.u.sin6.sin6_addr = ip6->ip_addr;
620 sa.u.sin6.sin6_port = ip6->udp_port;
621 sa.u.sin6.sin6_family = AF_INET6;
622 } else {
623 OSMO_ASSERT(false);
624 }
625
626 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200627 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200628 /* all nsvc in NSE should be IP/UDP nsvc */
629 OSMO_ASSERT(remote);
630 if (osmo_sockaddr_cmp(&sa, remote))
631 continue;
632
633 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
634 gprs_ns2_free_nsvc(nsvc);
635 }
636
637 return 0;
638}
639
640static int do_sns_add(struct osmo_fsm_inst *fi,
641 const struct gprs_ns_ie_ip4_elem *ip4,
642 const struct gprs_ns_ie_ip6_elem *ip6)
643{
644 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
645 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
646 struct gprs_ns2_vc *nsvc;
647 int rc = 0;
648
649 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
650 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
651 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
652 switch (gss->ip) {
653 case IPv4:
654 rc = add_remote_ip4_elem(gss, ip4);
655 break;
656 case IPv6:
657 rc = add_remote_ip6_elem(gss, ip6);
658 break;
659 default:
660 /* the gss->ip is initialized with the bss */
661 OSMO_ASSERT(false);
662 }
663
664 if (rc)
665 return rc;
666
667 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
668 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
669 * unspecified" */
670 switch (gss->ip) {
671 case IPv4:
672 nsvc = nsvc_by_ip4_elem(nse, ip4);
673 if (nsvc) {
674 /* the nsvc should be already in sync with the ip4 / ip6 elements */
675 return -NS_CAUSE_PROTO_ERR_UNSPEC;
676 }
677
678 /* TODO: failure case */
679 ns2_nsvc_create_ip4(fi, nse, ip4);
680 break;
681 case IPv6:
682 nsvc = nsvc_by_ip6_elem(nse, ip6);
683 if (nsvc) {
684 /* the nsvc should be already in sync with the ip4 / ip6 elements */
685 return -NS_CAUSE_PROTO_ERR_UNSPEC;
686 }
687
688 /* TODO: failure case */
689 ns2_nsvc_create_ip6(fi, nse, ip6);
690 break;
691 }
692
693 gprs_ns2_start_alive_all_nsvcs(nse);
694
695 return 0;
696}
697
698
699static void ns2_sns_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
700{
Alexander Couzense769f522020-12-07 07:37:07 +0100701 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200702}
703
704static void ns2_sns_st_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
705{
706 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
707 struct gprs_ns2_inst *nsi = nse->nsi;
708 struct tlv_parsed *tp = NULL;
709
710 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100711 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200712 tp = data;
713 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
714 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
715 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
716 /* TODO: What to do? */
717 } else {
718 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_BSS,
719 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
720 }
721 break;
722 default:
723 OSMO_ASSERT(0);
724 }
725}
726
Alexander Couzense769f522020-12-07 07:37:07 +0100727/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Alexander Couzens6a161492020-07-12 13:45:50 +0200728static void ns2_sns_st_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
729{
730 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100731 struct gprs_ns_ie_ip4_elem *ip4_elems;
732 struct gprs_ns_ie_ip6_elem *ip6_elems;
733 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100734 struct ns2_sns_bind *sbind;
Alexander Couzense769f522020-12-07 07:37:07 +0100735 struct osmo_sockaddr *remote;
736 const struct osmo_sockaddr *sa;
737 struct osmo_sockaddr local;
738 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200739
Alexander Couzense769f522020-12-07 07:37:07 +0100740 /* on a generic failure, the timer callback will recover */
Alexander Couzens6a161492020-07-12 13:45:50 +0200741 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
Alexander Couzens138b96f2021-01-25 16:23:29 +0100742 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Alexander Couzens790a9632021-02-05 17:18:39 +0100743 if (old_state != GPRS_SNS_ST_SIZE)
744 gss->N = 0;
745
Alexander Couzens6a161492020-07-12 13:45:50 +0200746
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100747 gss->alive = false;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100748 ns2_clear_ipv46_entries(gss);
749
Alexander Couzense769f522020-12-07 07:37:07 +0100750 /* no initial available */
751 if (!gss->initial)
752 return;
753
754 remote = &gss->initial->saddr;
755
756 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100757 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100758 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100759 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100760 return;
761 }
762
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100763 /* take the first bind or take the next bind */
764 if (!gss->initial_bind) {
765 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
766 } else {
767 if (gss->initial_bind->list.next != &gss->binds) {
768 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
769 } else {
770 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
Alexander Couzens81ae0aa2020-12-07 05:49:43 +0100771 }
Alexander Couzense769f522020-12-07 07:37:07 +0100772 }
773
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100774 bind = gss->initial_bind->bind;
775
Alexander Couzense769f522020-12-07 07:37:07 +0100776 /* setup the NSVC */
777 if (!gss->sns_nsvc) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100778 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
Alexander Couzense769f522020-12-07 07:37:07 +0100779 if (!gss->sns_nsvc)
780 return;
781 gss->sns_nsvc->sns_only = true;
782 }
783
784 switch (gss->ip) {
785 case IPv4:
786 ip4_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip4_elem) * count);
787 if (!ip4_elems)
788 return;
789
790 gss->ip4_local = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100791 llist_for_each_entry(sbind, &gss->binds, list) {
792 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100793 sa = gprs_ns2_ip_bind_sockaddr(bind);
794 if (!sa)
795 continue;
796
797 if (sa->u.sas.ss_family != AF_INET)
798 continue;
799
800 /* check if this is an specific bind */
801 if (sa->u.sin.sin_addr.s_addr == 0) {
802 if (osmo_sockaddr_local_ip(&local, remote))
803 continue;
804
805 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
806 } else {
807 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
808 }
809
810 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100811 ip4_elems->sig_weight = bind->sns_sig_weight;
812 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100813 ip4_elems++;
814 }
815
816 gss->num_ip4_local = count;
817 gss->num_max_ip4_remote = 4;
818 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->num_ip4_local, 8);
819 break;
820 case IPv6:
821 /* IPv6 */
822 ip6_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip6_elem) * count);
823 if (!ip6_elems)
824 return;
825
826 gss->ip6_local = ip6_elems;
827
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100828 llist_for_each_entry(sbind, &gss->binds, list) {
829 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100830 sa = gprs_ns2_ip_bind_sockaddr(bind);
831 if (!sa)
832 continue;
833
834 if (sa->u.sas.ss_family != AF_INET6)
835 continue;
836
837 /* check if this is an specific bind */
838 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
839 if (osmo_sockaddr_local_ip(&local, remote))
840 continue;
841
842 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
843 } else {
844 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
845 }
846
847 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100848 ip6_elems->sig_weight = bind->sns_sig_weight;
849 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100850
851 ip6_elems++;
852 }
853 gss->num_ip6_local = count;
854 gss->num_max_ip6_remote = 4;
855 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->num_ip6_local, 8);
856 break;
857 }
858
Alexander Couzens6a161492020-07-12 13:45:50 +0200859 if (gss->num_max_ip4_remote > 0)
860 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->num_max_ip4_remote, -1);
861 else
862 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 +0200863}
864
865static void ns2_sns_st_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
866{
867 struct tlv_parsed *tp = NULL;
Alexander Couzens3df58862021-02-05 17:18:08 +0100868 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6a161492020-07-12 13:45:50 +0200869
870 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100871 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200872 tp = (struct tlv_parsed *) data;
873 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
874 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
875 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
876 /* TODO: What to do? */
877 } else {
Alexander Couzens3df58862021-02-05 17:18:08 +0100878 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 +0200879 }
880 break;
881 default:
882 OSMO_ASSERT(0);
883 }
884}
885
886static void ns2_sns_st_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
887{
888 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100889
890 if (old_state != GPRS_SNS_ST_CONFIG_BSS)
891 gss->N = 0;
892
Alexander Couzens6a161492020-07-12 13:45:50 +0200893 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200894 switch (gss->ip) {
895 case IPv4:
896 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100897 gss->ip4_local, gss->num_ip4_local,
898 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200899 break;
900 case IPv6:
901 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100902 NULL, 0,
903 gss->ip6_local, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200904 break;
905 }
906}
907
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100908/* calculate the timeout of the configured state. the configured
909 * state will fail if not at least one NS-VC is alive within X second.
910 */
911static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
912{
913 int secs;
914 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
915 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
916 secs += nsi->timeout[NS_TOUT_TNS_TEST];
917
918 return secs;
919}
Alexander Couzens6a161492020-07-12 13:45:50 +0200920
921static void ns_sns_st_config_sgsn_ip4(struct osmo_fsm_inst *fi, uint32_t event, void *data)
922{
923 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
924 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
925 const struct gprs_ns_ie_ip4_elem *v4_list;
926 unsigned int num_v4;
927 struct tlv_parsed *tp = NULL;
928
929 uint8_t cause;
930
931 tp = (struct tlv_parsed *) data;
932
933 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
934 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
935 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
936 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
937 return;
938 }
939 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
940 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
941 /* realloc to the new size */
942 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote,
943 struct gprs_ns_ie_ip4_elem,
944 gss->num_ip4_remote+num_v4);
945 /* append the new entries to the end of the list */
946 memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list));
947 gss->num_ip4_remote += num_v4;
948
949 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
950 gss->num_ip4_remote);
Alexander Couzens67725e22021-02-15 02:37:03 +0100951 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200952 /* check if sum of data / sig weights == 0 */
953 if (ip4_weight_sum_data(gss->ip4_remote, gss->num_ip4_remote) == 0 ||
954 ip4_weight_sum_sig(gss->ip4_remote, gss->num_ip4_remote) == 0) {
955 cause = NS_CAUSE_INVAL_WEIGH;
956 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
957 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
958 return;
959 }
960 create_missing_nsvcs(fi);
961 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
962 /* start the test procedure on ALL NSVCs! */
963 gprs_ns2_start_alive_all_nsvcs(nse);
Alexander Couzens3df58862021-02-05 17:18:08 +0100964 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
Alexander Couzens6a161492020-07-12 13:45:50 +0200965 } else {
966 /* just send CONFIG-ACK */
967 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +0100968 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200969 }
970}
971
972static void ns_sns_st_config_sgsn_ip6(struct osmo_fsm_inst *fi, uint32_t event, void *data)
973{
974 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
975 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
976 const struct gprs_ns_ie_ip6_elem *v6_list;
977 unsigned int num_v6;
978 struct tlv_parsed *tp = NULL;
979
980 uint8_t cause;
981
982 tp = (struct tlv_parsed *) data;
983
984 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
985 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
986 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
987 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
988 return;
989 }
990 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
991 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
992 /* realloc to the new size */
993 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote,
994 struct gprs_ns_ie_ip6_elem,
995 gss->num_ip6_remote+num_v6);
996 /* append the new entries to the end of the list */
997 memcpy(&gss->ip6_remote[gss->num_ip6_remote], v6_list, num_v6*sizeof(*v6_list));
998 gss->num_ip6_remote += num_v6;
999
1000 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %u entries\n",
1001 gss->num_ip6_remote);
Alexander Couzens67725e22021-02-15 02:37:03 +01001002 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001003 /* check if sum of data / sig weights == 0 */
1004 if (ip6_weight_sum_data(gss->ip6_remote, gss->num_ip6_remote) == 0 ||
1005 ip6_weight_sum_sig(gss->ip6_remote, gss->num_ip6_remote) == 0) {
1006 cause = NS_CAUSE_INVAL_WEIGH;
1007 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1008 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1009 return;
1010 }
1011 create_missing_nsvcs(fi);
1012 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1013 /* start the test procedure on ALL NSVCs! */
1014 gprs_ns2_start_alive_all_nsvcs(nse);
1015 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1016 } else {
1017 /* just send CONFIG-ACK */
1018 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
Alexander Couzens790a9632021-02-05 17:18:39 +01001019 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001020 }
1021}
1022
Alexander Couzens790a9632021-02-05 17:18:39 +01001023static void ns2_sns_st_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1024{
1025 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1026
1027 if (old_state != GPRS_SNS_ST_CONFIG_SGSN)
1028 gss->N = 0;
1029}
1030
Alexander Couzens6a161492020-07-12 13:45:50 +02001031static void ns2_sns_st_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1032{
1033 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1034
1035 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001036 case GPRS_SNS_EV_RX_CONFIG_END:
1037 case GPRS_SNS_EV_RX_CONFIG:
Alexander Couzens6a161492020-07-12 13:45:50 +02001038
1039#if 0 /* part of incoming SNS-SIZE (doesn't happen on BSS side */
1040 if (TLVP_PRESENT(tp, NS_IE_RESET_FLAG)) {
1041 /* reset all existing config */
1042 if (gss->ip4_remote)
1043 talloc_free(gss->ip4_remote);
1044 gss->num_ip4_remote = 0;
1045 }
1046#endif
1047 /* TODO: reject IPv6 elements on IPv4 mode and vice versa */
1048 switch (gss->ip) {
1049 case IPv4:
1050 ns_sns_st_config_sgsn_ip4(fi, event, data);
1051 break;
1052 case IPv6:
1053 ns_sns_st_config_sgsn_ip6(fi, event, data);
1054 break;
1055 default:
1056 OSMO_ASSERT(0);
1057 }
1058 break;
1059 default:
1060 OSMO_ASSERT(0);
1061 }
1062}
1063
Alexander Couzens67725e22021-02-15 02:37:03 +01001064/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001065static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1066 struct ns2_sns_state *gss,
1067 struct tlv_parsed *tp)
1068{
1069 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1070 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1071 int num_v4 = 0, num_v6 = 0;
1072 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001073 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001074 int rc = 0;
1075
1076 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1077 * check uniqueness within the lists (no doublicate entries)
1078 * check not-known-by-us and sent back a list of unknown/known values
1079 * (abnormal behaviour according to 48.016)
1080 */
1081
1082 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1083 if (gss->ip == IPv4) {
1084 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1085 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1086 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1087 return;
1088 }
1089
1090 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1091 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001092 for (i = 0; i < num_v4; i++) {
1093 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001094 rc = do_sns_add(fi, &v4_list[i], NULL);
1095 if (rc < 0) {
1096 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001097 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001098 do_sns_delete(fi, &v4_list[j], NULL);
1099 cause = -rc;
1100 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1101 break;
1102 }
1103 }
1104 } else { /* IPv6 */
1105 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1106 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1107 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1108 return;
1109 }
1110
1111 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1112 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001113 for (i = 0; i < num_v6; i++) {
1114 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001115 rc = do_sns_add(fi, NULL, &v6_list[i]);
1116 if (rc < 0) {
1117 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001118 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001119 do_sns_delete(fi, NULL, &v6_list[j]);
1120 cause = -rc;
1121 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1122 break;
1123 }
1124 }
1125 }
1126
1127 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1128 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1129}
1130
1131static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1132 struct ns2_sns_state *gss,
1133 struct tlv_parsed *tp)
1134{
1135 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1136 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1137 int num_v4 = 0, num_v6 = 0;
1138 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001139 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001140 int rc = 0;
1141
1142 /* TODO: split up delete into v4 + v6
1143 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1144 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1145 */
1146 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1147 if (gss->ip == IPv4) {
1148 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1149 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1150 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001151 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001152 rc = do_sns_delete(fi, &v4_list[i], NULL);
1153 if (rc < 0) {
1154 cause = -rc;
1155 /* continue to delete others */
1156 }
1157 }
1158 if (cause != 0xff) {
1159 /* TODO: create list of not-deleted and return it */
1160 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1161 return;
1162 }
1163
1164 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1165 /* delete all NS-VCs for given IPv4 address */
1166 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1167 struct gprs_ns_ie_ip4_elem *ip4_remote;
1168 uint32_t ip_addr = *(uint32_t *)(ie+1);
1169 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1170 cause = NS_CAUSE_UNKN_IP_ADDR;
1171 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1172 return;
1173 }
1174 /* make a copy as do_sns_delete() will change the array underneath us */
1175 ip4_remote = talloc_memdup(fi, gss->ip4_remote,
1176 gss->num_ip4_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001177 for (i = 0; i < gss->num_ip4_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001178 if (ip4_remote[i].ip_addr == ip_addr) {
1179 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1180 if (rc < 0) {
1181 cause = -rc;
1182 /* continue to delete others */
1183 }
1184 }
1185 }
1186 talloc_free(ip4_remote);
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 } else {
1193 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1194 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1195 return;
1196 }
1197 } else { /* IPv6 */
1198 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1199 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1200 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001201 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001202 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1203 if (rc < 0) {
1204 cause = -rc;
1205 /* continue to delete others */
1206 }
1207 }
1208 if (cause != 0xff) {
1209 /* TODO: create list of not-deleted and return it */
1210 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1211 return;
1212 }
1213 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1214 /* delete all NS-VCs for given IPv4 address */
1215 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1216 struct gprs_ns_ie_ip6_elem *ip6_remote;
1217 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001218 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001219 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1220 cause = NS_CAUSE_UNKN_IP_ADDR;
1221 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1222 return;
1223 }
1224 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1225 /* make a copy as do_sns_delete() will change the array underneath us */
1226 ip6_remote = talloc_memdup(fi, gss->ip6_remote,
1227 gss->num_ip6_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001228 for (i = 0; i < gss->num_ip6_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001229 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1230 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1231 if (rc < 0) {
1232 cause = -rc;
1233 /* continue to delete others */
1234 }
1235 }
1236 }
1237
1238 talloc_free(ip6_remote);
1239 if (cause != 0xff) {
1240 /* TODO: create list of not-deleted and return it */
1241 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1242 return;
1243 }
1244 } else {
1245 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1246 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1247 return;
1248 }
1249 }
1250 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1251}
1252
1253static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1254 struct ns2_sns_state *gss,
1255 struct tlv_parsed *tp)
1256{
1257 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1258 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1259 int num_v4 = 0, num_v6 = 0;
1260 uint8_t trans_id, cause = 0xff;
1261 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001262 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001263
1264 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1265 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1266 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1267 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001268 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001269 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1270 if (rc < 0) {
1271 cause = -rc;
1272 /* continue to others */
1273 }
1274 }
1275 if (cause != 0xff) {
1276 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1277 return;
1278 }
1279 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1280 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1281 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001282 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001283 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1284 if (rc < 0) {
1285 cause = -rc;
1286 /* continue to others */
1287 }
1288 }
1289 if (cause != 0xff) {
1290 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1291 return;
1292 }
1293 } else {
1294 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1295 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1296 return;
1297 }
1298 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1299}
1300
1301static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1302{
1303 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1304 struct tlv_parsed *tp = data;
1305
1306 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001307 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001308 ns2_sns_st_configured_add(fi, gss, tp);
1309 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001310 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001311 ns2_sns_st_configured_delete(fi, gss, tp);
1312 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001313 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001314 ns2_sns_st_configured_change(fi, gss, tp);
1315 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001316 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001317 osmo_timer_del(&fi->timer);
1318 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001319 }
1320}
1321
1322static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1323{
1324 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens138b96f2021-01-25 16:23:29 +01001325 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001326}
1327
1328static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1329 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001330 .in_event_mask = 0, /* handled by all_state_action */
Alexander Couzens6a161492020-07-12 13:45:50 +02001331 .out_state_mask = S(GPRS_SNS_ST_SIZE),
1332 .name = "UNCONFIGURED",
1333 .action = ns2_sns_st_unconfigured,
1334 },
1335 [GPRS_SNS_ST_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001336 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001337 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1338 S(GPRS_SNS_ST_SIZE) |
1339 S(GPRS_SNS_ST_CONFIG_BSS),
1340 .name = "SIZE",
1341 .action = ns2_sns_st_size,
1342 .onenter = ns2_sns_st_size_onenter,
1343 },
1344 [GPRS_SNS_ST_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001345 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001346 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1347 S(GPRS_SNS_ST_CONFIG_BSS) |
1348 S(GPRS_SNS_ST_CONFIG_SGSN) |
1349 S(GPRS_SNS_ST_SIZE),
1350 .name = "CONFIG_BSS",
1351 .action = ns2_sns_st_config_bss,
1352 .onenter = ns2_sns_st_config_bss_onenter,
1353 },
1354 [GPRS_SNS_ST_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001355 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1356 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001357 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1358 S(GPRS_SNS_ST_CONFIG_SGSN) |
1359 S(GPRS_SNS_ST_CONFIGURED) |
1360 S(GPRS_SNS_ST_SIZE),
1361 .name = "CONFIG_SGSN",
1362 .action = ns2_sns_st_config_sgsn,
Alexander Couzens790a9632021-02-05 17:18:39 +01001363 .onenter = ns2_sns_st_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001364 },
1365 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001366 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1367 S(GPRS_SNS_EV_RX_DELETE) |
1368 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1369 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001370 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1371 S(GPRS_SNS_ST_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001372 .name = "CONFIGURED",
1373 .action = ns2_sns_st_configured,
1374 .onenter = ns2_sns_st_configured_onenter,
1375 },
1376};
1377
1378static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1379{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001380 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001381 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1382 struct gprs_ns2_inst *nsi = nse->nsi;
1383
Alexander Couzens90ee9632020-12-07 06:18:32 +01001384 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001385 switch (fi->T) {
1386 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001387 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1388 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001389 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001390 } else {
Alexander Couzenscc65a252020-12-21 14:03:58 +01001391 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nsi->timeout[NS_TOUT_TSNS_PROV], 1);
Alexander Couzensa367d082020-12-21 14:06:24 +01001392 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001393 break;
1394 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001395 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001396 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 +01001397 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001398 } else {
Alexander Couzenscc65a252020-12-21 14:03:58 +01001399 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 +01001400 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001401 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001402 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001403 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1404 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 +01001405 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001406 } else {
1407 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_SGSN, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
1408 }
1409 break;
1410 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001411 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 +01001412 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001413 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001414 }
1415 return 0;
1416}
1417
1418static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1419{
Alexander Couzense769f522020-12-07 07:37:07 +01001420 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001421 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001422 struct ns2_sns_bind *sbind;
1423 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001424
Alexander Couzens67725e22021-02-15 02:37:03 +01001425 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
Alexander Couzense769f522020-12-07 07:37:07 +01001426 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001427 case GPRS_SNS_EV_REQ_NO_NSVC:
Alexander Couzens3ad73362020-12-21 13:53:00 +01001428 /* ignore reselection running */
1429 if (gss->reselection_running)
1430 break;
1431
1432 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001433 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001434 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001435 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
Alexander Couzense769f522020-12-07 07:37:07 +01001436 /* tear down previous state
1437 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1438 gss->reselection_running = true;
1439 gprs_ns2_free_nsvcs(nse);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +01001440 ns2_clear_ipv46_entries(gss);
Alexander Couzense769f522020-12-07 07:37:07 +01001441
1442 /* Choose the next sns endpoint. */
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001443 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds)) {
Alexander Couzense769f522020-12-07 07:37:07 +01001444 gss->initial = NULL;
Alexander Couzens138b96f2021-01-25 16:23:29 +01001445 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
Alexander Couzense769f522020-12-07 07:37:07 +01001446 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1447 return;
1448 } else if (!gss->initial) {
1449 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1450 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1451 /* last entry, continue with first */
1452 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1453 } else {
1454 /* next element is an entry */
1455 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1456 }
1457
1458 gss->reselection_running = false;
1459 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 1);
1460 break;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001461 case GPRS_SNS_EV_REQ_ADD_BIND:
1462 sbind = data;
1463 switch (fi->state) {
1464 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001465 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001466 break;
1467 case GPRS_SNS_ST_SIZE:
1468 /* TODO: add the ip4 element to the list */
1469 break;
1470 case GPRS_SNS_ST_CONFIG_BSS:
1471 case GPRS_SNS_ST_CONFIG_SGSN:
1472 case GPRS_SNS_ST_CONFIGURED:
1473 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1474 break;
1475 }
1476 break;
1477 case GPRS_SNS_EV_REQ_DELETE_BIND:
1478 sbind = data;
1479 switch (fi->state) {
1480 case GPRS_SNS_ST_UNCONFIGURED:
1481 break;
1482 case GPRS_SNS_ST_SIZE:
1483 /* TODO: remove the ip4 element from the list */
1484 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1485 if (nsvc->bind == sbind->bind) {
1486 gprs_ns2_free_nsvc(nsvc);
1487 }
1488 }
1489 break;
1490 case GPRS_SNS_ST_CONFIG_BSS:
1491 case GPRS_SNS_ST_CONFIG_SGSN:
1492 case GPRS_SNS_ST_CONFIGURED:
1493 /* TODO: do an delete SNS-IP procedure */
1494 /* TODO: remove the ip4 element to 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;
1501 }
1502 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1503 talloc_free(sbind);
1504 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001505 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001506}
1507
1508static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1509 .name = "GPRS-NS2-SNS-BSS",
1510 .states = ns2_sns_bss_states,
1511 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001512 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1513 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001514 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1515 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Alexander Couzens6a161492020-07-12 13:45:50 +02001516 .allstate_action = ns2_sns_st_all_action,
1517 .cleanup = NULL,
1518 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001519 .event_names = gprs_sns_event_names,
1520 .pre_term = NULL,
1521 .log_subsys = DLNS,
1522};
1523
Harald Welte5bef2cc2020-09-18 22:33:24 +02001524/*! Allocate an IP-SNS FSM for the BSS side.
1525 * \param[in] nse NS Entity in which the FSM runs
1526 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001527 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001528struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1529 const char *id)
1530{
1531 struct osmo_fsm_inst *fi;
1532 struct ns2_sns_state *gss;
1533
1534 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1535 if (!fi)
1536 return fi;
1537
1538 gss = talloc_zero(fi, struct ns2_sns_state);
1539 if (!gss)
1540 goto err;
1541
1542 fi->priv = gss;
1543 gss->nse = nse;
Alexander Couzense769f522020-12-07 07:37:07 +01001544 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001545 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001546
1547 return fi;
1548err:
1549 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1550 return NULL;
1551}
1552
Harald Welte5bef2cc2020-09-18 22:33:24 +02001553/*! main entry point for receiving SNS messages from the network.
1554 * \param[in] nsvc NS-VC on which the message was received
1555 * \param[in] msg message buffer of the IP-SNS message
1556 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001557 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001558int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001559{
1560 struct gprs_ns2_nse *nse = nsvc->nse;
1561 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1562 uint16_t nsei = nsvc->nse->nsei;
1563 struct osmo_fsm_inst *fi;
1564
1565 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001566 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1567 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001568 return -EINVAL;
1569 }
1570
Alexander Couzens6a161492020-07-12 13:45:50 +02001571 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1572 fi = nse->bss_sns_fi;
1573
Harald Weltef2949742021-01-20 14:54:14 +01001574 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1575 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1576
Alexander Couzens6a161492020-07-12 13:45:50 +02001577 switch (nsh->pdu_type) {
1578 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001579 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001580 break;
1581 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001582 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001583 break;
1584 case SNS_PDUT_CONFIG:
1585 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001586 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001587 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001588 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001589 break;
1590 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001591 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001592 break;
1593 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001594 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001595 break;
1596 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001597 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001598 break;
1599 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001600 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001601 break;
1602 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001603 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001604 break;
1605 default:
Harald Weltef2949742021-01-20 14:54:14 +01001606 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1607 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001608 return -EINVAL;
1609 }
1610
1611 return 0;
1612}
1613
1614#include <osmocom/vty/vty.h>
1615#include <osmocom/vty/misc.h>
1616
Harald Welte1262c4f2021-01-19 20:58:33 +01001617static 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 +02001618{
1619 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001620 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001621 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1622}
1623
Harald Welte1262c4f2021-01-19 20:58:33 +01001624static 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 +02001625{
1626 char ip_addr[INET6_ADDRSTRLEN] = {};
1627 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1628 strcpy(ip_addr, "Invalid IPv6");
1629
Harald Welte1262c4f2021-01-19 20:58:33 +01001630 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001631 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1632}
1633
Harald Welte5bef2cc2020-09-18 22:33:24 +02001634/*! Dump the IP-SNS state to a vty.
1635 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001636 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001637 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1638 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001639void 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 +02001640{
1641 struct ns2_sns_state *gss;
1642 unsigned int i;
1643
1644 if (!nse->bss_sns_fi)
1645 return;
1646
Harald Welte1262c4f2021-01-19 20:58:33 +01001647 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001648 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1649
Harald Welte1262c4f2021-01-19 20:58:33 +01001650 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1651 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001652
1653 if (gss->num_ip4_local && gss->num_ip4_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001654 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001655 for (i = 0; i < gss->num_ip4_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001656 vty_dump_sns_ip4(vty, prefix, &gss->ip4_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001657
Harald Welte1262c4f2021-01-19 20:58:33 +01001658 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001659 for (i = 0; i < gss->num_ip4_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001660 vty_dump_sns_ip4(vty, prefix, &gss->ip4_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001661 }
1662
1663 if (gss->num_ip6_local && gss->num_ip6_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001664 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001665 for (i = 0; i < gss->num_ip6_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001666 vty_dump_sns_ip6(vty, prefix, &gss->ip6_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001667
Harald Welte1262c4f2021-01-19 20:58:33 +01001668 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001669 for (i = 0; i < gss->num_ip6_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001670 vty_dump_sns_ip6(vty, prefix, &gss->ip6_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001671 }
1672}
1673
Alexander Couzens412bc342020-11-19 05:24:37 +01001674/*! write IP-SNS to a vty
1675 * \param[in] vty VTY to which the state shall be printed
1676 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001677void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001678{
1679 struct ns2_sns_state *gss;
1680 struct osmo_sockaddr_str addr_str;
1681 struct sns_endpoint *endpoint;
1682
1683 if (!nse->bss_sns_fi)
1684 return;
1685
1686 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1687 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001688 /* It's unlikely that an error happens, but let's better be safe. */
1689 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1690 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001691 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001692 }
1693}
1694
Alexander Couzense769f522020-12-07 07:37:07 +01001695static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1696 const struct osmo_sockaddr *saddr)
1697{
1698 struct sns_endpoint *endpoint;
1699
1700 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1701 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1702 return endpoint;
1703 }
1704
1705 return NULL;
1706}
1707
1708/*! gprs_ns2_sns_add_endpoint
1709 * \param[in] nse
1710 * \param[in] sockaddr
1711 * \return
1712 */
1713int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1714 const struct osmo_sockaddr *saddr)
1715{
1716 struct ns2_sns_state *gss;
1717 struct sns_endpoint *endpoint;
1718 bool do_selection = false;
1719
1720 if (nse->ll != GPRS_NS2_LL_UDP) {
1721 return -EINVAL;
1722 }
1723
Alexander Couzens138b96f2021-01-25 16:23:29 +01001724 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001725 return -EINVAL;
1726 }
1727
1728 gss = nse->bss_sns_fi->priv;
1729
1730 if (ns2_get_sns_endpoint(gss, saddr))
1731 return -EADDRINUSE;
1732
1733 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1734 if (!endpoint)
1735 return -ENOMEM;
1736
1737 endpoint->saddr = *saddr;
1738 if (llist_empty(&gss->sns_endpoints))
1739 do_selection = true;
1740
1741 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1742 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001743 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001744
1745 return 0;
1746}
1747
1748/*! gprs_ns2_sns_del_endpoint
1749 * \param[in] nse
1750 * \param[in] sockaddr
1751 * \return 0 on success, otherwise < 0
1752 */
1753int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1754 const struct osmo_sockaddr *saddr)
1755{
1756 struct ns2_sns_state *gss;
1757 struct sns_endpoint *endpoint;
1758
1759 if (nse->ll != GPRS_NS2_LL_UDP) {
1760 return -EINVAL;
1761 }
1762
Alexander Couzens138b96f2021-01-25 16:23:29 +01001763 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001764 return -EINVAL;
1765 }
1766
1767 gss = nse->bss_sns_fi->priv;
1768 endpoint = ns2_get_sns_endpoint(gss, saddr);
1769 if (!endpoint)
1770 return -ENOENT;
1771
1772 /* if this is an unused SNS endpoint it's done */
1773 if (gss->initial != endpoint) {
1774 llist_del(&endpoint->list);
1775 talloc_free(endpoint);
1776 return 0;
1777 }
1778
Alexander Couzens67725e22021-02-15 02:37:03 +01001779 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001780 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001781 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001782 "Closing all NS-VC and restart SNS-SIZE procedure"
1783 "with a remaining SNS endpoint.\n");
1784
1785 /* Continue with the next endpoint in the list.
1786 * Special case if the endpoint is at the start or end of the list */
1787 if (endpoint->list.prev == &gss->sns_endpoints ||
1788 endpoint->list.next == &gss->sns_endpoints)
1789 gss->initial = NULL;
1790 else
1791 gss->initial = llist_entry(endpoint->list.next->prev,
1792 struct sns_endpoint,
1793 list);
1794
1795 llist_del(&endpoint->list);
1796 gprs_ns2_free_nsvcs(nse);
1797 talloc_free(endpoint);
1798
1799 return 0;
1800}
1801
1802/*! gprs_ns2_sns_count
1803 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1804 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1805 */
1806int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1807{
1808 struct ns2_sns_state *gss;
1809 struct sns_endpoint *endpoint;
1810 int count = 0;
1811
1812 if (nse->ll != GPRS_NS2_LL_UDP) {
1813 return -EINVAL;
1814 }
1815
Alexander Couzens138b96f2021-01-25 16:23:29 +01001816 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001817 return -EINVAL;
1818 }
1819
1820 gss = nse->bss_sns_fi->priv;
1821 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1822 count++;
1823
1824 return count;
1825}
1826
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001827void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1828{
1829 struct ns2_sns_state *gss;
1830 struct gprs_ns2_vc *tmp;
1831
1832 if (!nse->bss_sns_fi)
1833 return;
1834
1835 gss = nse->bss_sns_fi->priv;
1836 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1837 return;
1838
1839 if (alive == gss->alive)
1840 return;
1841
1842 /* check if this is the current SNS NS-VC */
1843 if (nsvc == gss->sns_nsvc) {
1844 /* only replace the SNS NS-VC if there are other alive NS-VC.
1845 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1846 * and couldn't confirm yet if the NS-VC comes up */
1847 if (gss->alive && !alive)
1848 ns2_sns_replace_nsvc(nsvc);
1849 }
1850
1851 if (alive) {
1852 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001853 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001854 } else {
1855 /* is there at least another alive nsvc? */
1856 llist_for_each_entry(tmp, &nse->nsvc, list) {
1857 if (ns2_vc_is_unblocked(tmp))
1858 return;
1859 }
1860
1861 /* all NS-VC have failed */
1862 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001863 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001864 }
1865}
1866
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001867int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1868 struct gprs_ns2_vc_bind *bind)
1869{
1870 struct ns2_sns_state *gss;
1871 struct ns2_sns_bind *tmp;
1872
1873 OSMO_ASSERT(nse->bss_sns_fi);
1874 gss = nse->bss_sns_fi->priv;
1875
1876 if (!gprs_ns2_is_ip_bind(bind)) {
1877 return -EINVAL;
1878 }
1879
1880 if (!llist_empty(&gss->binds)) {
1881 llist_for_each_entry(tmp, &gss->binds, list) {
1882 if (tmp->bind == bind)
1883 return -EALREADY;
1884 }
1885 }
1886
1887 tmp = talloc_zero(gss, struct ns2_sns_bind);
1888 if (!tmp)
1889 return -ENOMEM;
1890 tmp->bind = bind;
1891 llist_add_tail(&tmp->list, &gss->binds);
1892
1893 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
1894 return 0;
1895}
1896
1897/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
1898int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
1899 struct gprs_ns2_vc_bind *bind)
1900{
1901 struct ns2_sns_state *gss;
1902 struct ns2_sns_bind *tmp, *tmp2;
1903 bool found = false;
1904
1905 if (!nse->bss_sns_fi)
1906 return -EINVAL;
1907
1908 gss = nse->bss_sns_fi->priv;
1909 if (gss->initial_bind && gss->initial_bind->bind == bind) {
1910 if (gss->initial_bind->list.prev == &gss->binds)
1911 gss->initial_bind = NULL;
1912 else
1913 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
1914 }
1915
1916 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
1917 if (tmp->bind == bind) {
1918 llist_del(&tmp->list);
1919 found = true;
1920 }
1921 }
1922
1923 if (!found)
1924 return -ENOENT;
1925
1926 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
1927 return 0;
1928}
1929
Alexander Couzensc4704762021-02-08 23:13:12 +01001930/* Update SNS weights
1931 * \param[in] nsvc the NSVC which should be updated
1932 */
1933void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
1934{
1935 /* TODO: implement weights after binds per sns implemented */
1936}
1937
Alexander Couzens6a161492020-07-12 13:45:50 +02001938/* initialize osmo_ctx on main tread */
1939static __attribute__((constructor)) void on_dso_load_ctx(void)
1940{
1941 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
1942}