blob: b80a8801b422d9e1808a8cfa7648ad27172b2ebd [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 Couzense769f522020-12-07 07:37:07 +010073 GPRS_SNS_EV_SELECT_ENDPOINT, /*!< Select a SNS endpoint from the list */
Alexander Couzens6a161492020-07-12 13:45:50 +020074 GPRS_SNS_EV_SIZE,
75 GPRS_SNS_EV_SIZE_ACK,
76 GPRS_SNS_EV_CONFIG,
77 GPRS_SNS_EV_CONFIG_END, /*!< SNS-CONFIG with end flag received */
78 GPRS_SNS_EV_CONFIG_ACK,
79 GPRS_SNS_EV_ADD,
80 GPRS_SNS_EV_DELETE,
81 GPRS_SNS_EV_CHANGE_WEIGHT,
82 GPRS_SNS_EV_NO_NSVC,
Alexander Couzensbe7cecc2021-02-03 18:25:27 +010083 GPRS_SNS_EV_NSVC_ALIVE, /*!< a NS-VC became alive */
Alexander Couzens6a161492020-07-12 13:45:50 +020084};
85
86static const struct value_string gprs_sns_event_names[] = {
Alexander Couzense769f522020-12-07 07:37:07 +010087 { GPRS_SNS_EV_SELECT_ENDPOINT, "SELECT_ENDPOINT" },
Alexander Couzens6a161492020-07-12 13:45:50 +020088 { GPRS_SNS_EV_SIZE, "SIZE" },
89 { GPRS_SNS_EV_SIZE_ACK, "SIZE_ACK" },
90 { GPRS_SNS_EV_CONFIG, "CONFIG" },
91 { GPRS_SNS_EV_CONFIG_END, "CONFIG_END" },
92 { GPRS_SNS_EV_CONFIG_ACK, "CONFIG_ACK" },
93 { GPRS_SNS_EV_ADD, "ADD" },
94 { GPRS_SNS_EV_DELETE, "DELETE" },
95 { GPRS_SNS_EV_CHANGE_WEIGHT, "CHANGE_WEIGHT" },
Pau Espin Pedrol0a446a12020-10-27 13:30:08 +010096 { GPRS_SNS_EV_NO_NSVC, "NO_NSVC" },
Alexander Couzensbe7cecc2021-02-03 18:25:27 +010097 { GPRS_SNS_EV_NSVC_ALIVE, "NSVC_ALIVE"},
Alexander Couzens6a161492020-07-12 13:45:50 +020098 { 0, NULL }
99};
100
Alexander Couzense769f522020-12-07 07:37:07 +0100101struct sns_endpoint {
102 struct llist_head list;
103 struct osmo_sockaddr saddr;
104};
105
Alexander Couzens6a161492020-07-12 13:45:50 +0200106struct ns2_sns_state {
107 struct gprs_ns2_nse *nse;
108
109 enum ns2_sns_type ip;
110
Alexander Couzense769f522020-12-07 07:37:07 +0100111 /* holds the list of initial SNS endpoints */
112 struct llist_head sns_endpoints;
113 /* prevent recursive reselection */
114 bool reselection_running;
115
116 /* The current initial SNS endpoints.
117 * The initial connection will be moved into the NSE
118 * if configured via SNS. Otherwise it will be removed
119 * in configured state. */
120 struct sns_endpoint *initial;
Alexander Couzens6a161492020-07-12 13:45:50 +0200121 /* all SNS PDU will be sent over this nsvc */
122 struct gprs_ns2_vc *sns_nsvc;
Alexander Couzens81ae0aa2020-12-07 05:49:43 +0100123 /* iterate over the binds after all remote has been tested */
124 int bind_offset;
Alexander Couzens90ee9632020-12-07 06:18:32 +0100125 /* timer N */
126 int N;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100127 /* true if at least one nsvc is alive */
128 bool alive;
Alexander Couzens6a161492020-07-12 13:45:50 +0200129
130 /* local configuration to send to the remote end */
131 struct gprs_ns_ie_ip4_elem *ip4_local;
132 size_t num_ip4_local;
133
134 /* local configuration to send to the remote end */
135 struct gprs_ns_ie_ip6_elem *ip6_local;
136 size_t num_ip6_local;
137
138 /* local configuration about our capabilities in terms of connections to
139 * remote (SGSN) side */
140 size_t num_max_nsvcs;
141 size_t num_max_ip4_remote;
142 size_t num_max_ip6_remote;
143
144 /* remote configuration as received */
145 struct gprs_ns_ie_ip4_elem *ip4_remote;
146 unsigned int num_ip4_remote;
147
148 /* remote configuration as received */
149 struct gprs_ns_ie_ip6_elem *ip6_remote;
150 unsigned int num_ip6_remote;
151};
152
153static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi)
154{
155 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
156 return gss->nse;
157}
158
159/* helper function to compute the sum of all (data or signaling) weights */
160static int ip4_weight_sum(const struct gprs_ns_ie_ip4_elem *ip4, unsigned int num,
161 bool data_weight)
162{
163 unsigned int i;
164 int weight_sum = 0;
165
166 for (i = 0; i < num; i++) {
167 if (data_weight)
168 weight_sum += ip4[i].data_weight;
169 else
170 weight_sum += ip4[i].sig_weight;
171 }
172 return weight_sum;
173}
174#define ip4_weight_sum_data(x,y) ip4_weight_sum(x, y, true)
175#define ip4_weight_sum_sig(x,y) ip4_weight_sum(x, y, false)
176
177/* helper function to compute the sum of all (data or signaling) weights */
178static int ip6_weight_sum(const struct gprs_ns_ie_ip6_elem *ip6, unsigned int num,
179 bool data_weight)
180{
181 unsigned int i;
182 int weight_sum = 0;
183
184 for (i = 0; i < num; i++) {
185 if (data_weight)
186 weight_sum += ip6[i].data_weight;
187 else
188 weight_sum += ip6[i].sig_weight;
189 }
190 return weight_sum;
191}
192#define ip6_weight_sum_data(x,y) ip6_weight_sum(x, y, true)
193#define ip6_weight_sum_sig(x,y) ip6_weight_sum(x, y, false)
194
195static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
196 const struct gprs_ns_ie_ip4_elem *ip4)
197{
198 struct osmo_sockaddr sa;
199 /* copy over. Both data structures use network byte order */
200 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
201 sa.u.sin.sin_port = ip4->udp_port;
202 sa.u.sin.sin_family = AF_INET;
203
Alexander Couzens38b19e82020-09-23 23:56:37 +0200204 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200205}
206
207static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
208 const struct gprs_ns_ie_ip6_elem *ip6)
209{
210 struct osmo_sockaddr sa;
211 /* copy over. Both data structures use network byte order */
212 sa.u.sin6.sin6_addr = ip6->ip_addr;
213 sa.u.sin6.sin6_port = ip6->udp_port;
214 sa.u.sin6.sin6_family = AF_INET;
215
Alexander Couzens38b19e82020-09-23 23:56:37 +0200216 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200217}
218
Alexander Couzens125298f2020-10-11 21:22:42 +0200219/*! Return the initial SNS remote socket address
220 * \param nse NS Entity
221 * \return address of the initial SNS connection; NULL in case of error
222 */
223const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
224{
225 struct ns2_sns_state *gss;
226
227 if (!nse->bss_sns_fi)
228 return NULL;
229
230 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100231 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200232}
233
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100234/*! called when a nsvc is beeing freed or the nsvc became dead */
235void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200236{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100237 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200238 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100239 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200240 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200241
242 if (!fi)
243 return;
244
245 gss = (struct ns2_sns_state *) fi->priv;
246 if (nsvc != gss->sns_nsvc)
247 return;
248
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100249 gss->sns_nsvc = NULL;
250 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200251 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100252 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200253 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100254 return;
255 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200256 }
257 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100258 /* the SNS is waiting for its first NS-VC to come up
259 * choose any other nsvc */
260 llist_for_each_entry(tmp, &nse->nsvc, list) {
261 if (nsvc != tmp) {
262 gss->sns_nsvc = tmp;
263 return;
264 }
265 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200266 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100267
268 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200269}
270
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100271static void ns2_clear_ipv46_entries(struct ns2_sns_state *gss)
272{
273 TALLOC_FREE(gss->ip4_local);
274 TALLOC_FREE(gss->ip4_remote);
275 TALLOC_FREE(gss->ip6_local);
276 TALLOC_FREE(gss->ip6_remote);
277
278 gss->num_ip4_local = 0;
279 gss->num_ip4_remote = 0;
280 gss->num_ip6_local = 0;
281 gss->num_ip6_remote = 0;
282}
283
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100284static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
285 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200286{
287 struct gprs_ns2_inst *nsi = nse->nsi;
288 struct gprs_ns2_vc *nsvc;
289 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100290
291 /* for every bind, create a connection if bind type == IP */
292 llist_for_each_entry(bind, &nsi->binding, list) {
293 if (bind->ll != GPRS_NS2_LL_UDP)
294 continue;
295 /* ignore failed connection */
296 nsvc = gprs_ns2_ip_connect_inactive(bind,
297 remote,
298 nse, 0);
299 if (!nsvc) {
300 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
301 continue;
302 }
303
304 nsvc->sig_weight = sig_weight;
305 nsvc->data_weight = data_weight;
306 }
307}
308
309static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
310 struct gprs_ns2_nse *nse,
311 const struct gprs_ns_ie_ip4_elem *ip4)
312{
Alexander Couzensc068d862020-10-12 04:11:51 +0200313 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200314 /* copy over. Both data structures use network byte order */
315 remote.u.sin.sin_family = AF_INET;
316 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
317 remote.u.sin.sin_port = ip4->udp_port;
318
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100319 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200320}
321
322static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
323 struct gprs_ns2_nse *nse,
324 const struct gprs_ns_ie_ip6_elem *ip6)
325{
Alexander Couzens6a161492020-07-12 13:45:50 +0200326 struct osmo_sockaddr remote = {};
327 /* copy over. Both data structures use network byte order */
328 remote.u.sin6.sin6_family = AF_INET6;
329 remote.u.sin6.sin6_addr = ip6->ip_addr;
330 remote.u.sin6.sin6_port = ip6->udp_port;
331
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100332 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200333}
334
335
336static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
337{
338 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
339 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
340 struct gprs_ns2_vc *nsvc;
341 struct gprs_ns2_vc_bind *bind;
342 struct osmo_sockaddr remote = { };
343 unsigned int i;
344
345 for (i = 0; i < gss->num_ip4_remote; i++) {
346 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->ip4_remote[i];
347
348 remote.u.sin.sin_family = AF_INET;
349 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
350 remote.u.sin.sin_port = ip4->udp_port;
351
352 llist_for_each_entry(bind, &nse->nsi->binding, list) {
353 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100354 if (bind->ll != GPRS_NS2_LL_UDP)
355 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200356
357 llist_for_each_entry(nsvc, &nse->nsvc, list) {
358 if (nsvc->bind != bind)
359 continue;
360
Alexander Couzensc4229a42020-10-11 20:58:04 +0200361 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200362 found = true;
363 break;
364 }
365 }
366
367 if (!found) {
368 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
369 if (!nsvc) {
370 /* TODO: add to a list to send back a NS-STATUS */
371 continue;
372 }
373 }
374
375 /* update data / signalling weight */
376 nsvc->data_weight = ip4->data_weight;
377 nsvc->sig_weight = ip4->sig_weight;
378 nsvc->sns_only = false;
379 }
380 }
381
382 for (i = 0; i < gss->num_ip6_remote; i++) {
383 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->ip6_remote[i];
384
385 remote.u.sin6.sin6_family = AF_INET6;
386 remote.u.sin6.sin6_addr = ip6->ip_addr;
387 remote.u.sin6.sin6_port = ip6->udp_port;
388
389 llist_for_each_entry(bind, &nse->nsi->binding, list) {
390 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100391 if (bind->ll != GPRS_NS2_LL_UDP)
392 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200393
394 llist_for_each_entry(nsvc, &nse->nsvc, list) {
395 if (nsvc->bind != bind)
396 continue;
397
Alexander Couzensc4229a42020-10-11 20:58:04 +0200398 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200399 found = true;
400 break;
401 }
402 }
403
404 if (!found) {
405 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
406 if (!nsvc) {
407 /* TODO: add to a list to send back a NS-STATUS */
408 continue;
409 }
410 }
411
412 /* update data / signalling weight */
413 nsvc->data_weight = ip6->data_weight;
414 nsvc->sig_weight = ip6->sig_weight;
415 nsvc->sns_only = false;
416 }
417 }
418
419
420 return 0;
421}
422
423/* Add a given remote IPv4 element to gprs_sns_state */
424static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
425{
426 unsigned int i;
427
428 if (gss->num_ip4_remote >= gss->num_max_ip4_remote)
429 return -NS_CAUSE_INVAL_NR_NS_VC;
430
431 /* check for duplicates */
432 for (i = 0; i < gss->num_ip4_remote; i++) {
433 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
434 continue;
435 /* TODO: log message duplicate */
436 /* TODO: check if this is the correct cause code */
437 return -NS_CAUSE_PROTO_ERR_UNSPEC;
438 }
439
440 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem,
441 gss->num_ip4_remote+1);
442 gss->ip4_remote[gss->num_ip4_remote] = *ip4;
443 gss->num_ip4_remote += 1;
444 return 0;
445}
446
447/* Remove a given remote IPv4 element from gprs_sns_state */
448static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
449{
450 unsigned int i;
451
452 for (i = 0; i < gss->num_ip4_remote; i++) {
453 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
454 continue;
455 /* all array elements < i remain as they are; all > i are shifted left by one */
456 memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1);
457 gss->num_ip4_remote -= 1;
458 return 0;
459 }
460 return -1;
461}
462
463/* update the weights for specified remote IPv4 */
464static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
465{
466 unsigned int i;
467
468 for (i = 0; i < gss->num_ip4_remote; i++) {
469 if (gss->ip4_remote[i].ip_addr != ip4->ip_addr ||
470 gss->ip4_remote[i].udp_port != ip4->udp_port)
471 continue;
472
473 gss->ip4_remote[i].sig_weight = ip4->sig_weight;
474 gss->ip4_remote[i].data_weight = ip4->data_weight;
475 return 0;
476 }
477 return -1;
478}
479
480/* Add a given remote IPv6 element to gprs_sns_state */
481static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
482{
483 if (gss->num_ip6_remote >= gss->num_max_ip6_remote)
484 return -NS_CAUSE_INVAL_NR_NS_VC;
485
486 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, struct gprs_ns_ie_ip6_elem,
487 gss->num_ip6_remote+1);
488 gss->ip6_remote[gss->num_ip6_remote] = *ip6;
489 gss->num_ip6_remote += 1;
490 return 0;
491}
492
493/* Remove a given remote IPv6 element from gprs_sns_state */
494static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
495{
496 unsigned int i;
497
498 for (i = 0; i < gss->num_ip6_remote; i++) {
499 if (memcmp(&gss->ip6_remote[i], ip6, sizeof(*ip6)))
500 continue;
501 /* all array elements < i remain as they are; all > i are shifted left by one */
502 memmove(&gss->ip6_remote[i], &gss->ip6_remote[i+1], gss->num_ip6_remote-i-1);
503 gss->num_ip6_remote -= 1;
504 return 0;
505 }
506 return -1;
507}
508
509/* update the weights for specified remote IPv6 */
510static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
511{
512 unsigned int i;
513
514 for (i = 0; i < gss->num_ip6_remote; i++) {
515 if (memcmp(&gss->ip6_remote[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
516 gss->ip6_remote[i].udp_port != ip6->udp_port)
517 continue;
518 gss->ip6_remote[i].sig_weight = ip6->sig_weight;
519 gss->ip6_remote[i].data_weight = ip6->data_weight;
520 return 0;
521 }
522 return -1;
523}
524
525static 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)
526{
527 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
528 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
529 struct gprs_ns2_vc *nsvc;
530 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200531 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200532 uint8_t new_signal;
533 uint8_t new_data;
534
535 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
536 * signalling weights of all the peer IP endpoints configured for this NSE is
537 * equal to zero or if the resulting sum of the data weights of all the peer IP
538 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
539 * SNS-ACK PDU with a cause code of "Invalid weights". */
540
541 if (ip4) {
542 if (update_remote_ip4_elem(gss, ip4))
543 return -NS_CAUSE_UNKN_IP_EP;
544
545 /* copy over. Both data structures use network byte order */
546 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
547 sa.u.sin.sin_port = ip4->udp_port;
548 sa.u.sin.sin_family = AF_INET;
549 new_signal = ip4->sig_weight;
550 new_data = ip4->data_weight;
551 } else if (ip6) {
552 if (update_remote_ip6_elem(gss, ip6))
553 return -NS_CAUSE_UNKN_IP_EP;
554
555 /* copy over. Both data structures use network byte order */
556 sa.u.sin6.sin6_addr = ip6->ip_addr;
557 sa.u.sin6.sin6_port = ip6->udp_port;
558 sa.u.sin6.sin6_family = AF_INET6;
559 new_signal = ip6->sig_weight;
560 new_data = ip6->data_weight;
561 } else {
562 OSMO_ASSERT(false);
563 }
564
565 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200566 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200567 /* all nsvc in NSE should be IP/UDP nsvc */
568 OSMO_ASSERT(remote);
569
570 if (osmo_sockaddr_cmp(&sa, remote))
571 continue;
572
573 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
574 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
575 nsvc->sig_weight, new_signal);
576
577 nsvc->data_weight = new_data;
578 nsvc->sig_weight = new_signal;
579 }
580
581 return 0;
582}
583
584static int do_sns_delete(struct osmo_fsm_inst *fi,
585 const struct gprs_ns_ie_ip4_elem *ip4,
586 const struct gprs_ns_ie_ip6_elem *ip6)
587{
588 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
589 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
590 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200591 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200592 struct osmo_sockaddr sa = {};
593
594 if (ip4) {
595 if (remove_remote_ip4_elem(gss, ip4) < 0)
596 return -NS_CAUSE_UNKN_IP_EP;
597 /* copy over. Both data structures use network byte order */
598 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
599 sa.u.sin.sin_port = ip4->udp_port;
600 sa.u.sin.sin_family = AF_INET;
601 } else if (ip6) {
602 if (remove_remote_ip6_elem(gss, ip6))
603 return -NS_CAUSE_UNKN_IP_EP;
604
605 /* copy over. Both data structures use network byte order */
606 sa.u.sin6.sin6_addr = ip6->ip_addr;
607 sa.u.sin6.sin6_port = ip6->udp_port;
608 sa.u.sin6.sin6_family = AF_INET6;
609 } else {
610 OSMO_ASSERT(false);
611 }
612
613 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200614 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200615 /* all nsvc in NSE should be IP/UDP nsvc */
616 OSMO_ASSERT(remote);
617 if (osmo_sockaddr_cmp(&sa, remote))
618 continue;
619
620 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
621 gprs_ns2_free_nsvc(nsvc);
622 }
623
624 return 0;
625}
626
627static int do_sns_add(struct osmo_fsm_inst *fi,
628 const struct gprs_ns_ie_ip4_elem *ip4,
629 const struct gprs_ns_ie_ip6_elem *ip6)
630{
631 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
632 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
633 struct gprs_ns2_vc *nsvc;
634 int rc = 0;
635
636 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
637 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
638 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
639 switch (gss->ip) {
640 case IPv4:
641 rc = add_remote_ip4_elem(gss, ip4);
642 break;
643 case IPv6:
644 rc = add_remote_ip6_elem(gss, ip6);
645 break;
646 default:
647 /* the gss->ip is initialized with the bss */
648 OSMO_ASSERT(false);
649 }
650
651 if (rc)
652 return rc;
653
654 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
655 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
656 * unspecified" */
657 switch (gss->ip) {
658 case IPv4:
659 nsvc = nsvc_by_ip4_elem(nse, ip4);
660 if (nsvc) {
661 /* the nsvc should be already in sync with the ip4 / ip6 elements */
662 return -NS_CAUSE_PROTO_ERR_UNSPEC;
663 }
664
665 /* TODO: failure case */
666 ns2_nsvc_create_ip4(fi, nse, ip4);
667 break;
668 case IPv6:
669 nsvc = nsvc_by_ip6_elem(nse, ip6);
670 if (nsvc) {
671 /* the nsvc should be already in sync with the ip4 / ip6 elements */
672 return -NS_CAUSE_PROTO_ERR_UNSPEC;
673 }
674
675 /* TODO: failure case */
676 ns2_nsvc_create_ip6(fi, nse, ip6);
677 break;
678 }
679
680 gprs_ns2_start_alive_all_nsvcs(nse);
681
682 return 0;
683}
684
685
686static void ns2_sns_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
687{
Alexander Couzense769f522020-12-07 07:37:07 +0100688 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200689}
690
691static void ns2_sns_st_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
692{
693 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
694 struct gprs_ns2_inst *nsi = nse->nsi;
695 struct tlv_parsed *tp = NULL;
696
697 switch (event) {
698 case GPRS_SNS_EV_SIZE_ACK:
699 tp = data;
700 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
701 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
702 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
703 /* TODO: What to do? */
704 } else {
705 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_BSS,
706 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
707 }
708 break;
709 default:
710 OSMO_ASSERT(0);
711 }
712}
713
Alexander Couzense769f522020-12-07 07:37:07 +0100714/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Alexander Couzens6a161492020-07-12 13:45:50 +0200715static void ns2_sns_st_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
716{
717 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100718 struct gprs_ns_ie_ip4_elem *ip4_elems;
719 struct gprs_ns_ie_ip6_elem *ip6_elems;
720 struct gprs_ns2_vc_bind *bind;
721 struct gprs_ns2_inst *nsi = gss->nse->nsi;
722 struct osmo_sockaddr *remote;
723 const struct osmo_sockaddr *sa;
724 struct osmo_sockaddr local;
725 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200726
Alexander Couzense769f522020-12-07 07:37:07 +0100727 /* on a generic failure, the timer callback will recover */
Alexander Couzens6a161492020-07-12 13:45:50 +0200728 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
Alexander Couzens138b96f2021-01-25 16:23:29 +0100729 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200730
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100731 gss->alive = false;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100732 ns2_clear_ipv46_entries(gss);
733
Alexander Couzense769f522020-12-07 07:37:07 +0100734 /* no initial available */
735 if (!gss->initial)
736 return;
737
738 remote = &gss->initial->saddr;
739
740 /* count how many bindings are available (only UDP binds) */
741 count = ns2_ip_count_bind(nsi, remote);
742 if (count == 0) {
743 /* TODO: logging */
744 return;
745 }
746
Alexander Couzens81ae0aa2020-12-07 05:49:43 +0100747 bind = ns2_ip_get_bind_by_index(nsi, remote, gss->bind_offset);
Alexander Couzense769f522020-12-07 07:37:07 +0100748 if (!bind) {
Alexander Couzens81ae0aa2020-12-07 05:49:43 +0100749 if (gss->bind_offset) {
750 gss->bind_offset = 0;
751 bind = ns2_ip_get_bind_by_index(nsi, remote, gss->bind_offset);
752 }
753
754 if (!bind)
755 return;
Alexander Couzense769f522020-12-07 07:37:07 +0100756 }
757
758 /* setup the NSVC */
759 if (!gss->sns_nsvc) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100760 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
Alexander Couzense769f522020-12-07 07:37:07 +0100761 if (!gss->sns_nsvc)
762 return;
763 gss->sns_nsvc->sns_only = true;
764 }
765
766 switch (gss->ip) {
767 case IPv4:
768 ip4_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip4_elem) * count);
769 if (!ip4_elems)
770 return;
771
772 gss->ip4_local = ip4_elems;
773
774 llist_for_each_entry(bind, &nsi->binding, list) {
775 if (!gprs_ns2_is_ip_bind(bind))
776 continue;
777
778 sa = gprs_ns2_ip_bind_sockaddr(bind);
779 if (!sa)
780 continue;
781
782 if (sa->u.sas.ss_family != AF_INET)
783 continue;
784
785 /* check if this is an specific bind */
786 if (sa->u.sin.sin_addr.s_addr == 0) {
787 if (osmo_sockaddr_local_ip(&local, remote))
788 continue;
789
790 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
791 } else {
792 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
793 }
794
795 ip4_elems->udp_port = sa->u.sin.sin_port;
796 ip4_elems->sig_weight = 2;
797 ip4_elems->data_weight = 1;
798 ip4_elems++;
799 }
800
801 gss->num_ip4_local = count;
802 gss->num_max_ip4_remote = 4;
803 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->num_ip4_local, 8);
804 break;
805 case IPv6:
806 /* IPv6 */
807 ip6_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip6_elem) * count);
808 if (!ip6_elems)
809 return;
810
811 gss->ip6_local = ip6_elems;
812
813 llist_for_each_entry(bind, &nsi->binding, list) {
814 if (!gprs_ns2_is_ip_bind(bind))
815 continue;
816
817 sa = gprs_ns2_ip_bind_sockaddr(bind);
818 if (!sa)
819 continue;
820
821 if (sa->u.sas.ss_family != AF_INET6)
822 continue;
823
824 /* check if this is an specific bind */
825 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
826 if (osmo_sockaddr_local_ip(&local, remote))
827 continue;
828
829 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
830 } else {
831 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
832 }
833
834 ip6_elems->udp_port = sa->u.sin.sin_port;
835 ip6_elems->sig_weight = 2;
836 ip6_elems->data_weight = 1;
837
838 ip6_elems++;
839 }
840 gss->num_ip6_local = count;
841 gss->num_max_ip6_remote = 4;
842 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->num_ip6_local, 8);
843 break;
844 }
845
Alexander Couzens6a161492020-07-12 13:45:50 +0200846 if (gss->num_max_ip4_remote > 0)
847 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->num_max_ip4_remote, -1);
848 else
849 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 +0200850}
851
852static void ns2_sns_st_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
853{
854 struct tlv_parsed *tp = NULL;
855
856 switch (event) {
857 case GPRS_SNS_EV_CONFIG_ACK:
858 tp = (struct tlv_parsed *) data;
859 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
860 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
861 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
862 /* TODO: What to do? */
863 } else {
864 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_SGSN, 0, 0);
865 }
866 break;
867 default:
868 OSMO_ASSERT(0);
869 }
870}
871
872static void ns2_sns_st_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
873{
874 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
875 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200876 switch (gss->ip) {
877 case IPv4:
878 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100879 gss->ip4_local, gss->num_ip4_local,
880 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200881 break;
882 case IPv6:
883 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100884 NULL, 0,
885 gss->ip6_local, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200886 break;
887 }
888}
889
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100890/* calculate the timeout of the configured state. the configured
891 * state will fail if not at least one NS-VC is alive within X second.
892 */
893static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
894{
895 int secs;
896 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
897 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
898 secs += nsi->timeout[NS_TOUT_TNS_TEST];
899
900 return secs;
901}
Alexander Couzens6a161492020-07-12 13:45:50 +0200902
903static void ns_sns_st_config_sgsn_ip4(struct osmo_fsm_inst *fi, uint32_t event, void *data)
904{
905 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
906 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
907 const struct gprs_ns_ie_ip4_elem *v4_list;
908 unsigned int num_v4;
909 struct tlv_parsed *tp = NULL;
910
911 uint8_t cause;
912
913 tp = (struct tlv_parsed *) data;
914
915 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
916 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
917 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
918 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
919 return;
920 }
921 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
922 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
923 /* realloc to the new size */
924 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote,
925 struct gprs_ns_ie_ip4_elem,
926 gss->num_ip4_remote+num_v4);
927 /* append the new entries to the end of the list */
928 memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list));
929 gss->num_ip4_remote += num_v4;
930
931 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
932 gss->num_ip4_remote);
933 if (event == GPRS_SNS_EV_CONFIG_END) {
934 /* check if sum of data / sig weights == 0 */
935 if (ip4_weight_sum_data(gss->ip4_remote, gss->num_ip4_remote) == 0 ||
936 ip4_weight_sum_sig(gss->ip4_remote, gss->num_ip4_remote) == 0) {
937 cause = NS_CAUSE_INVAL_WEIGH;
938 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
939 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
940 return;
941 }
942 create_missing_nsvcs(fi);
943 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
944 /* start the test procedure on ALL NSVCs! */
945 gprs_ns2_start_alive_all_nsvcs(nse);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100946 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 3);
Alexander Couzens6a161492020-07-12 13:45:50 +0200947 } else {
948 /* just send CONFIG-ACK */
949 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
950 }
951}
952
953static void ns_sns_st_config_sgsn_ip6(struct osmo_fsm_inst *fi, uint32_t event, void *data)
954{
955 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
956 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
957 const struct gprs_ns_ie_ip6_elem *v6_list;
958 unsigned int num_v6;
959 struct tlv_parsed *tp = NULL;
960
961 uint8_t cause;
962
963 tp = (struct tlv_parsed *) data;
964
965 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
966 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
967 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
968 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
969 return;
970 }
971 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
972 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
973 /* realloc to the new size */
974 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote,
975 struct gprs_ns_ie_ip6_elem,
976 gss->num_ip6_remote+num_v6);
977 /* append the new entries to the end of the list */
978 memcpy(&gss->ip6_remote[gss->num_ip6_remote], v6_list, num_v6*sizeof(*v6_list));
979 gss->num_ip6_remote += num_v6;
980
981 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %u entries\n",
982 gss->num_ip6_remote);
983 if (event == GPRS_SNS_EV_CONFIG_END) {
984 /* check if sum of data / sig weights == 0 */
985 if (ip6_weight_sum_data(gss->ip6_remote, gss->num_ip6_remote) == 0 ||
986 ip6_weight_sum_sig(gss->ip6_remote, gss->num_ip6_remote) == 0) {
987 cause = NS_CAUSE_INVAL_WEIGH;
988 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
989 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
990 return;
991 }
992 create_missing_nsvcs(fi);
993 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
994 /* start the test procedure on ALL NSVCs! */
995 gprs_ns2_start_alive_all_nsvcs(nse);
996 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
997 } else {
998 /* just send CONFIG-ACK */
999 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1000 }
1001}
1002
1003static void ns2_sns_st_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1004{
1005 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1006
1007 switch (event) {
1008 case GPRS_SNS_EV_CONFIG_END:
1009 case GPRS_SNS_EV_CONFIG:
1010
1011#if 0 /* part of incoming SNS-SIZE (doesn't happen on BSS side */
1012 if (TLVP_PRESENT(tp, NS_IE_RESET_FLAG)) {
1013 /* reset all existing config */
1014 if (gss->ip4_remote)
1015 talloc_free(gss->ip4_remote);
1016 gss->num_ip4_remote = 0;
1017 }
1018#endif
1019 /* TODO: reject IPv6 elements on IPv4 mode and vice versa */
1020 switch (gss->ip) {
1021 case IPv4:
1022 ns_sns_st_config_sgsn_ip4(fi, event, data);
1023 break;
1024 case IPv6:
1025 ns_sns_st_config_sgsn_ip6(fi, event, data);
1026 break;
1027 default:
1028 OSMO_ASSERT(0);
1029 }
1030 break;
1031 default:
1032 OSMO_ASSERT(0);
1033 }
1034}
1035
1036/* called when receiving GPRS_SNS_EV_ADD in state configure */
1037static 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) {
1279 case GPRS_SNS_EV_ADD:
1280 ns2_sns_st_configured_add(fi, gss, tp);
1281 break;
1282 case GPRS_SNS_EV_DELETE:
1283 ns2_sns_st_configured_delete(fi, gss, tp);
1284 break;
1285 case GPRS_SNS_EV_CHANGE_WEIGHT:
1286 ns2_sns_st_configured_change(fi, gss, tp);
1287 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001288 case GPRS_SNS_EV_NSVC_ALIVE:
1289 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] = {
1308 .in_event_mask = S(GPRS_SNS_EV_SIZE_ACK),
1309 .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] = {
1317 .in_event_mask = S(GPRS_SNS_EV_CONFIG_ACK),
1318 .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] = {
1327 .in_event_mask = S(GPRS_SNS_EV_CONFIG) |
1328 S(GPRS_SNS_EV_CONFIG_END),
1329 .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,
1335 },
1336 [GPRS_SNS_ST_CONFIGURED] = {
1337 .in_event_mask = S(GPRS_SNS_EV_ADD) |
1338 S(GPRS_SNS_EV_DELETE) |
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001339 S(GPRS_SNS_EV_CHANGE_WEIGHT) |
1340 S(GPRS_SNS_EV_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001341 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1342 S(GPRS_SNS_ST_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001343 .name = "CONFIGURED",
1344 .action = ns2_sns_st_configured,
1345 .onenter = ns2_sns_st_configured_onenter,
1346 },
1347};
1348
1349static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1350{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001351 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001352 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1353 struct gprs_ns2_inst *nsi = nse->nsi;
1354
Alexander Couzens90ee9632020-12-07 06:18:32 +01001355 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001356 switch (fi->T) {
1357 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001358 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1359 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens90ee9632020-12-07 06:18:32 +01001360 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001361 } else {
Alexander Couzenscc65a252020-12-21 14:03:58 +01001362 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nsi->timeout[NS_TOUT_TSNS_PROV], 1);
Alexander Couzensa367d082020-12-21 14:06:24 +01001363 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001364 break;
1365 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001366 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1367 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens90ee9632020-12-07 06:18:32 +01001368 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001369 } else {
Alexander Couzenscc65a252020-12-21 14:03:58 +01001370 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 +01001371 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001372 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001373 case 3:
1374 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online. Selecting next IP-SNS endpoint.\n", nse->nsei);
1375 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_SELECT_ENDPOINT, NULL);
1376 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001377 }
1378 return 0;
1379}
1380
1381static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1382{
Alexander Couzense769f522020-12-07 07:37:07 +01001383 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001384 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1385
1386 /* reset when receiving GPRS_SNS_EV_NO_NSVC */
Alexander Couzense769f522020-12-07 07:37:07 +01001387 switch (event) {
1388 case GPRS_SNS_EV_NO_NSVC:
Alexander Couzens3ad73362020-12-21 13:53:00 +01001389 /* ignore reselection running */
1390 if (gss->reselection_running)
1391 break;
1392
1393 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1394 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001395 break;
1396 case GPRS_SNS_EV_SELECT_ENDPOINT:
1397 /* tear down previous state
1398 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1399 gss->reselection_running = true;
1400 gprs_ns2_free_nsvcs(nse);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +01001401 ns2_clear_ipv46_entries(gss);
Alexander Couzense769f522020-12-07 07:37:07 +01001402
1403 /* Choose the next sns endpoint. */
1404 if (llist_empty(&gss->sns_endpoints)) {
1405 gss->initial = NULL;
Alexander Couzens138b96f2021-01-25 16:23:29 +01001406 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
Alexander Couzense769f522020-12-07 07:37:07 +01001407 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1408 return;
1409 } else if (!gss->initial) {
1410 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
Alexander Couzens81ae0aa2020-12-07 05:49:43 +01001411 gss->bind_offset = 0;
Alexander Couzense769f522020-12-07 07:37:07 +01001412 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1413 /* last entry, continue with first */
1414 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
Alexander Couzens81ae0aa2020-12-07 05:49:43 +01001415 gss->bind_offset++;
1416 gss->bind_offset %= ns2_ip_count_bind(nse->nsi, &gss->initial->saddr);
Alexander Couzense769f522020-12-07 07:37:07 +01001417 } else {
1418 /* next element is an entry */
1419 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1420 }
1421
1422 gss->reselection_running = false;
1423 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 1);
1424 break;
1425 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001426}
1427
1428static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1429 .name = "GPRS-NS2-SNS-BSS",
1430 .states = ns2_sns_bss_states,
1431 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzense769f522020-12-07 07:37:07 +01001432 .allstate_event_mask = S(GPRS_SNS_EV_NO_NSVC) |
1433 S(GPRS_SNS_EV_SELECT_ENDPOINT),
Alexander Couzens6a161492020-07-12 13:45:50 +02001434 .allstate_action = ns2_sns_st_all_action,
1435 .cleanup = NULL,
1436 .timer_cb = ns2_sns_fsm_bss_timer_cb,
1437 /* .log_subsys = DNS, "is not constant" */
1438 .event_names = gprs_sns_event_names,
1439 .pre_term = NULL,
1440 .log_subsys = DLNS,
1441};
1442
Harald Welte5bef2cc2020-09-18 22:33:24 +02001443/*! Allocate an IP-SNS FSM for the BSS side.
1444 * \param[in] nse NS Entity in which the FSM runs
1445 * \param[in] id string identifier
1446 * \retruns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001447struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1448 const char *id)
1449{
1450 struct osmo_fsm_inst *fi;
1451 struct ns2_sns_state *gss;
1452
1453 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1454 if (!fi)
1455 return fi;
1456
1457 gss = talloc_zero(fi, struct ns2_sns_state);
1458 if (!gss)
1459 goto err;
1460
1461 fi->priv = gss;
1462 gss->nse = nse;
Alexander Couzense769f522020-12-07 07:37:07 +01001463 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6a161492020-07-12 13:45:50 +02001464
1465 return fi;
1466err:
1467 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1468 return NULL;
1469}
1470
Harald Welte5bef2cc2020-09-18 22:33:24 +02001471/*! main entry point for receiving SNS messages from the network.
1472 * \param[in] nsvc NS-VC on which the message was received
1473 * \param[in] msg message buffer of the IP-SNS message
1474 * \param[in] tp parsed TLV structure of message
1475 * \retruns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001476int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001477{
1478 struct gprs_ns2_nse *nse = nsvc->nse;
1479 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1480 uint16_t nsei = nsvc->nse->nsei;
1481 struct osmo_fsm_inst *fi;
1482
1483 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001484 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1485 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001486 return -EINVAL;
1487 }
1488
Alexander Couzens6a161492020-07-12 13:45:50 +02001489 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1490 fi = nse->bss_sns_fi;
1491
Harald Weltef2949742021-01-20 14:54:14 +01001492 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1493 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1494
Alexander Couzens6a161492020-07-12 13:45:50 +02001495 switch (nsh->pdu_type) {
1496 case SNS_PDUT_SIZE:
1497 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_SIZE, tp);
1498 break;
1499 case SNS_PDUT_SIZE_ACK:
1500 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_SIZE_ACK, tp);
1501 break;
1502 case SNS_PDUT_CONFIG:
1503 if (nsh->data[0] & 0x01)
1504 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_CONFIG_END, tp);
1505 else
1506 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_CONFIG, tp);
1507 break;
1508 case SNS_PDUT_CONFIG_ACK:
1509 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_CONFIG_ACK, tp);
1510 break;
1511 case SNS_PDUT_ADD:
1512 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_ADD, tp);
1513 break;
1514 case SNS_PDUT_DELETE:
1515 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_DELETE, tp);
1516 break;
1517 case SNS_PDUT_CHANGE_WEIGHT:
1518 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_CHANGE_WEIGHT, tp);
1519 break;
1520 case SNS_PDUT_ACK:
Harald Weltef2949742021-01-20 14:54:14 +01001521 LOGPFSML(fi, LOGL_NOTICE, "NSEI=%u Rx unsupported SNS PDU type %s\n", nsei,
1522 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001523 break;
1524 default:
Harald Weltef2949742021-01-20 14:54:14 +01001525 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1526 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001527 return -EINVAL;
1528 }
1529
1530 return 0;
1531}
1532
1533#include <osmocom/vty/vty.h>
1534#include <osmocom/vty/misc.h>
1535
Harald Welte1262c4f2021-01-19 20:58:33 +01001536static 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 +02001537{
1538 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001539 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001540 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1541}
1542
Harald Welte1262c4f2021-01-19 20:58:33 +01001543static 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 +02001544{
1545 char ip_addr[INET6_ADDRSTRLEN] = {};
1546 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1547 strcpy(ip_addr, "Invalid IPv6");
1548
Harald Welte1262c4f2021-01-19 20:58:33 +01001549 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001550 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1551}
1552
Harald Welte5bef2cc2020-09-18 22:33:24 +02001553/*! Dump the IP-SNS state to a vty.
1554 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001555 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001556 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1557 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001558void 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 +02001559{
1560 struct ns2_sns_state *gss;
1561 unsigned int i;
1562
1563 if (!nse->bss_sns_fi)
1564 return;
1565
Harald Welte1262c4f2021-01-19 20:58:33 +01001566 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001567 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1568
Harald Welte1262c4f2021-01-19 20:58:33 +01001569 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1570 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001571
1572 if (gss->num_ip4_local && gss->num_ip4_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001573 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001574 for (i = 0; i < gss->num_ip4_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001575 vty_dump_sns_ip4(vty, prefix, &gss->ip4_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001576
Harald Welte1262c4f2021-01-19 20:58:33 +01001577 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001578 for (i = 0; i < gss->num_ip4_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001579 vty_dump_sns_ip4(vty, prefix, &gss->ip4_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001580 }
1581
1582 if (gss->num_ip6_local && gss->num_ip6_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001583 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001584 for (i = 0; i < gss->num_ip6_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001585 vty_dump_sns_ip6(vty, prefix, &gss->ip6_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001586
Harald Welte1262c4f2021-01-19 20:58:33 +01001587 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001588 for (i = 0; i < gss->num_ip6_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001589 vty_dump_sns_ip6(vty, prefix, &gss->ip6_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001590 }
1591}
1592
Alexander Couzens412bc342020-11-19 05:24:37 +01001593/*! write IP-SNS to a vty
1594 * \param[in] vty VTY to which the state shall be printed
1595 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001596void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001597{
1598 struct ns2_sns_state *gss;
1599 struct osmo_sockaddr_str addr_str;
1600 struct sns_endpoint *endpoint;
1601
1602 if (!nse->bss_sns_fi)
1603 return;
1604
1605 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1606 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001607 /* It's unlikely that an error happens, but let's better be safe. */
1608 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1609 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens412bc342020-11-19 05:24:37 +01001610 vty_out(vty, " ip-sns %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
1611 }
1612}
1613
Alexander Couzense769f522020-12-07 07:37:07 +01001614static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1615 const struct osmo_sockaddr *saddr)
1616{
1617 struct sns_endpoint *endpoint;
1618
1619 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1620 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1621 return endpoint;
1622 }
1623
1624 return NULL;
1625}
1626
1627/*! gprs_ns2_sns_add_endpoint
1628 * \param[in] nse
1629 * \param[in] sockaddr
1630 * \return
1631 */
1632int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1633 const struct osmo_sockaddr *saddr)
1634{
1635 struct ns2_sns_state *gss;
1636 struct sns_endpoint *endpoint;
1637 bool do_selection = false;
1638
1639 if (nse->ll != GPRS_NS2_LL_UDP) {
1640 return -EINVAL;
1641 }
1642
Alexander Couzens138b96f2021-01-25 16:23:29 +01001643 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001644 return -EINVAL;
1645 }
1646
1647 gss = nse->bss_sns_fi->priv;
1648
1649 if (ns2_get_sns_endpoint(gss, saddr))
1650 return -EADDRINUSE;
1651
1652 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1653 if (!endpoint)
1654 return -ENOMEM;
1655
1656 endpoint->saddr = *saddr;
1657 if (llist_empty(&gss->sns_endpoints))
1658 do_selection = true;
1659
1660 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1661 if (do_selection)
1662 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_SELECT_ENDPOINT, NULL);
1663
1664 return 0;
1665}
1666
1667/*! gprs_ns2_sns_del_endpoint
1668 * \param[in] nse
1669 * \param[in] sockaddr
1670 * \return 0 on success, otherwise < 0
1671 */
1672int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1673 const struct osmo_sockaddr *saddr)
1674{
1675 struct ns2_sns_state *gss;
1676 struct sns_endpoint *endpoint;
1677
1678 if (nse->ll != GPRS_NS2_LL_UDP) {
1679 return -EINVAL;
1680 }
1681
Alexander Couzens138b96f2021-01-25 16:23:29 +01001682 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001683 return -EINVAL;
1684 }
1685
1686 gss = nse->bss_sns_fi->priv;
1687 endpoint = ns2_get_sns_endpoint(gss, saddr);
1688 if (!endpoint)
1689 return -ENOENT;
1690
1691 /* if this is an unused SNS endpoint it's done */
1692 if (gss->initial != endpoint) {
1693 llist_del(&endpoint->list);
1694 talloc_free(endpoint);
1695 return 0;
1696 }
1697
1698 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_NO_NSVC on the last NS-VC
1699 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001700 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001701 "Closing all NS-VC and restart SNS-SIZE procedure"
1702 "with a remaining SNS endpoint.\n");
1703
1704 /* Continue with the next endpoint in the list.
1705 * Special case if the endpoint is at the start or end of the list */
1706 if (endpoint->list.prev == &gss->sns_endpoints ||
1707 endpoint->list.next == &gss->sns_endpoints)
1708 gss->initial = NULL;
1709 else
1710 gss->initial = llist_entry(endpoint->list.next->prev,
1711 struct sns_endpoint,
1712 list);
1713
1714 llist_del(&endpoint->list);
1715 gprs_ns2_free_nsvcs(nse);
1716 talloc_free(endpoint);
1717
1718 return 0;
1719}
1720
1721/*! gprs_ns2_sns_count
1722 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1723 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1724 */
1725int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1726{
1727 struct ns2_sns_state *gss;
1728 struct sns_endpoint *endpoint;
1729 int count = 0;
1730
1731 if (nse->ll != GPRS_NS2_LL_UDP) {
1732 return -EINVAL;
1733 }
1734
Alexander Couzens138b96f2021-01-25 16:23:29 +01001735 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001736 return -EINVAL;
1737 }
1738
1739 gss = nse->bss_sns_fi->priv;
1740 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1741 count++;
1742
1743 return count;
1744}
1745
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001746void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1747{
1748 struct ns2_sns_state *gss;
1749 struct gprs_ns2_vc *tmp;
1750
1751 if (!nse->bss_sns_fi)
1752 return;
1753
1754 gss = nse->bss_sns_fi->priv;
1755 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1756 return;
1757
1758 if (alive == gss->alive)
1759 return;
1760
1761 /* check if this is the current SNS NS-VC */
1762 if (nsvc == gss->sns_nsvc) {
1763 /* only replace the SNS NS-VC if there are other alive NS-VC.
1764 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1765 * and couldn't confirm yet if the NS-VC comes up */
1766 if (gss->alive && !alive)
1767 ns2_sns_replace_nsvc(nsvc);
1768 }
1769
1770 if (alive) {
1771 gss->alive = true;
1772 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_NSVC_ALIVE, NULL);
1773 } else {
1774 /* is there at least another alive nsvc? */
1775 llist_for_each_entry(tmp, &nse->nsvc, list) {
1776 if (ns2_vc_is_unblocked(tmp))
1777 return;
1778 }
1779
1780 /* all NS-VC have failed */
1781 gss->alive = false;
1782 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_NO_NSVC, NULL);
1783 }
1784}
1785
Alexander Couzens6a161492020-07-12 13:45:50 +02001786/* initialize osmo_ctx on main tread */
1787static __attribute__((constructor)) void on_dso_load_ctx(void)
1788{
1789 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
1790}