blob: 1d4d069281566afe1054e672b3b650143c757a62 [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 */
Alexander Couzens6a161492020-07-12 13:45:50 +0200449 return -NS_CAUSE_PROTO_ERR_UNSPEC;
450 }
451
452 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem,
453 gss->num_ip4_remote+1);
454 gss->ip4_remote[gss->num_ip4_remote] = *ip4;
455 gss->num_ip4_remote += 1;
456 return 0;
457}
458
459/* Remove a given remote IPv4 element from gprs_sns_state */
460static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
461{
462 unsigned int i;
463
464 for (i = 0; i < gss->num_ip4_remote; i++) {
465 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
466 continue;
467 /* all array elements < i remain as they are; all > i are shifted left by one */
468 memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1);
469 gss->num_ip4_remote -= 1;
470 return 0;
471 }
472 return -1;
473}
474
475/* update the weights for specified remote IPv4 */
476static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
477{
478 unsigned int i;
479
480 for (i = 0; i < gss->num_ip4_remote; i++) {
481 if (gss->ip4_remote[i].ip_addr != ip4->ip_addr ||
482 gss->ip4_remote[i].udp_port != ip4->udp_port)
483 continue;
484
485 gss->ip4_remote[i].sig_weight = ip4->sig_weight;
486 gss->ip4_remote[i].data_weight = ip4->data_weight;
487 return 0;
488 }
489 return -1;
490}
491
492/* Add a given remote IPv6 element to gprs_sns_state */
493static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
494{
495 if (gss->num_ip6_remote >= gss->num_max_ip6_remote)
496 return -NS_CAUSE_INVAL_NR_NS_VC;
497
498 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, struct gprs_ns_ie_ip6_elem,
499 gss->num_ip6_remote+1);
500 gss->ip6_remote[gss->num_ip6_remote] = *ip6;
501 gss->num_ip6_remote += 1;
502 return 0;
503}
504
505/* Remove a given remote IPv6 element from gprs_sns_state */
506static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
507{
508 unsigned int i;
509
510 for (i = 0; i < gss->num_ip6_remote; i++) {
511 if (memcmp(&gss->ip6_remote[i], ip6, sizeof(*ip6)))
512 continue;
513 /* all array elements < i remain as they are; all > i are shifted left by one */
514 memmove(&gss->ip6_remote[i], &gss->ip6_remote[i+1], gss->num_ip6_remote-i-1);
515 gss->num_ip6_remote -= 1;
516 return 0;
517 }
518 return -1;
519}
520
521/* update the weights for specified remote IPv6 */
522static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
523{
524 unsigned int i;
525
526 for (i = 0; i < gss->num_ip6_remote; i++) {
527 if (memcmp(&gss->ip6_remote[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
528 gss->ip6_remote[i].udp_port != ip6->udp_port)
529 continue;
530 gss->ip6_remote[i].sig_weight = ip6->sig_weight;
531 gss->ip6_remote[i].data_weight = ip6->data_weight;
532 return 0;
533 }
534 return -1;
535}
536
537static 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)
538{
539 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
540 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
541 struct gprs_ns2_vc *nsvc;
542 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200543 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200544 uint8_t new_signal;
545 uint8_t new_data;
546
547 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
548 * signalling weights of all the peer IP endpoints configured for this NSE is
549 * equal to zero or if the resulting sum of the data weights of all the peer IP
550 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
551 * SNS-ACK PDU with a cause code of "Invalid weights". */
552
553 if (ip4) {
554 if (update_remote_ip4_elem(gss, ip4))
555 return -NS_CAUSE_UNKN_IP_EP;
556
557 /* copy over. Both data structures use network byte order */
558 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
559 sa.u.sin.sin_port = ip4->udp_port;
560 sa.u.sin.sin_family = AF_INET;
561 new_signal = ip4->sig_weight;
562 new_data = ip4->data_weight;
563 } else if (ip6) {
564 if (update_remote_ip6_elem(gss, ip6))
565 return -NS_CAUSE_UNKN_IP_EP;
566
567 /* copy over. Both data structures use network byte order */
568 sa.u.sin6.sin6_addr = ip6->ip_addr;
569 sa.u.sin6.sin6_port = ip6->udp_port;
570 sa.u.sin6.sin6_family = AF_INET6;
571 new_signal = ip6->sig_weight;
572 new_data = ip6->data_weight;
573 } else {
574 OSMO_ASSERT(false);
575 }
576
577 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200578 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200579 /* all nsvc in NSE should be IP/UDP nsvc */
580 OSMO_ASSERT(remote);
581
582 if (osmo_sockaddr_cmp(&sa, remote))
583 continue;
584
585 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
586 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
587 nsvc->sig_weight, new_signal);
588
589 nsvc->data_weight = new_data;
590 nsvc->sig_weight = new_signal;
591 }
592
593 return 0;
594}
595
596static int do_sns_delete(struct osmo_fsm_inst *fi,
597 const struct gprs_ns_ie_ip4_elem *ip4,
598 const struct gprs_ns_ie_ip6_elem *ip6)
599{
600 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
601 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
602 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200603 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200604 struct osmo_sockaddr sa = {};
605
606 if (ip4) {
607 if (remove_remote_ip4_elem(gss, ip4) < 0)
608 return -NS_CAUSE_UNKN_IP_EP;
609 /* copy over. Both data structures use network byte order */
610 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
611 sa.u.sin.sin_port = ip4->udp_port;
612 sa.u.sin.sin_family = AF_INET;
613 } else if (ip6) {
614 if (remove_remote_ip6_elem(gss, ip6))
615 return -NS_CAUSE_UNKN_IP_EP;
616
617 /* copy over. Both data structures use network byte order */
618 sa.u.sin6.sin6_addr = ip6->ip_addr;
619 sa.u.sin6.sin6_port = ip6->udp_port;
620 sa.u.sin6.sin6_family = AF_INET6;
621 } else {
622 OSMO_ASSERT(false);
623 }
624
625 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200626 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200627 /* all nsvc in NSE should be IP/UDP nsvc */
628 OSMO_ASSERT(remote);
629 if (osmo_sockaddr_cmp(&sa, remote))
630 continue;
631
632 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
633 gprs_ns2_free_nsvc(nsvc);
634 }
635
636 return 0;
637}
638
639static int do_sns_add(struct osmo_fsm_inst *fi,
640 const struct gprs_ns_ie_ip4_elem *ip4,
641 const struct gprs_ns_ie_ip6_elem *ip6)
642{
643 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
644 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
645 struct gprs_ns2_vc *nsvc;
646 int rc = 0;
647
648 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
649 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
650 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
651 switch (gss->ip) {
652 case IPv4:
653 rc = add_remote_ip4_elem(gss, ip4);
654 break;
655 case IPv6:
656 rc = add_remote_ip6_elem(gss, ip6);
657 break;
658 default:
659 /* the gss->ip is initialized with the bss */
660 OSMO_ASSERT(false);
661 }
662
663 if (rc)
664 return rc;
665
666 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
667 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
668 * unspecified" */
669 switch (gss->ip) {
670 case IPv4:
671 nsvc = nsvc_by_ip4_elem(nse, ip4);
672 if (nsvc) {
673 /* the nsvc should be already in sync with the ip4 / ip6 elements */
674 return -NS_CAUSE_PROTO_ERR_UNSPEC;
675 }
676
677 /* TODO: failure case */
678 ns2_nsvc_create_ip4(fi, nse, ip4);
679 break;
680 case IPv6:
681 nsvc = nsvc_by_ip6_elem(nse, ip6);
682 if (nsvc) {
683 /* the nsvc should be already in sync with the ip4 / ip6 elements */
684 return -NS_CAUSE_PROTO_ERR_UNSPEC;
685 }
686
687 /* TODO: failure case */
688 ns2_nsvc_create_ip6(fi, nse, ip6);
689 break;
690 }
691
692 gprs_ns2_start_alive_all_nsvcs(nse);
693
694 return 0;
695}
696
697
698static void ns2_sns_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
699{
Alexander Couzense769f522020-12-07 07:37:07 +0100700 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200701}
702
703static void ns2_sns_st_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
704{
705 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
706 struct gprs_ns2_inst *nsi = nse->nsi;
707 struct tlv_parsed *tp = NULL;
708
709 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100710 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200711 tp = data;
712 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
713 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
714 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
715 /* TODO: What to do? */
716 } else {
717 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_BSS,
718 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
719 }
720 break;
721 default:
722 OSMO_ASSERT(0);
723 }
724}
725
Harald Welte24920e22021-03-04 13:03:27 +0100726static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200727{
728 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100729 struct gprs_ns_ie_ip4_elem *ip4_elems;
730 struct gprs_ns_ie_ip6_elem *ip6_elems;
731 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100732 struct ns2_sns_bind *sbind;
Alexander Couzense769f522020-12-07 07:37:07 +0100733 struct osmo_sockaddr *remote;
734 const struct osmo_sockaddr *sa;
735 struct osmo_sockaddr local;
736 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200737
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100738 ns2_clear_ipv46_entries(gss);
739
Alexander Couzense769f522020-12-07 07:37:07 +0100740 /* no initial available */
741 if (!gss->initial)
742 return;
743
744 remote = &gss->initial->saddr;
745
746 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100747 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100748 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100749 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100750 return;
751 }
752
Alexander Couzense769f522020-12-07 07:37:07 +0100753 switch (gss->ip) {
754 case IPv4:
755 ip4_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip4_elem) * count);
756 if (!ip4_elems)
757 return;
758
759 gss->ip4_local = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100760 llist_for_each_entry(sbind, &gss->binds, list) {
761 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100762 sa = gprs_ns2_ip_bind_sockaddr(bind);
763 if (!sa)
764 continue;
765
766 if (sa->u.sas.ss_family != AF_INET)
767 continue;
768
769 /* check if this is an specific bind */
770 if (sa->u.sin.sin_addr.s_addr == 0) {
771 if (osmo_sockaddr_local_ip(&local, remote))
772 continue;
773
774 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
775 } else {
776 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
777 }
778
779 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100780 ip4_elems->sig_weight = bind->sns_sig_weight;
781 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100782 ip4_elems++;
783 }
784
785 gss->num_ip4_local = count;
786 gss->num_max_ip4_remote = 4;
787 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->num_ip4_local, 8);
788 break;
789 case IPv6:
790 /* IPv6 */
791 ip6_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip6_elem) * count);
792 if (!ip6_elems)
793 return;
794
795 gss->ip6_local = ip6_elems;
796
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100797 llist_for_each_entry(sbind, &gss->binds, list) {
798 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100799 sa = gprs_ns2_ip_bind_sockaddr(bind);
800 if (!sa)
801 continue;
802
803 if (sa->u.sas.ss_family != AF_INET6)
804 continue;
805
806 /* check if this is an specific bind */
807 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
808 if (osmo_sockaddr_local_ip(&local, remote))
809 continue;
810
811 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
812 } else {
813 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
814 }
815
816 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100817 ip6_elems->sig_weight = bind->sns_sig_weight;
818 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100819
820 ip6_elems++;
821 }
822 gss->num_ip6_local = count;
823 gss->num_max_ip6_remote = 4;
824 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->num_ip6_local, 8);
825 break;
826 }
Harald Welte24920e22021-03-04 13:03:27 +0100827}
828
829/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
830static void ns2_sns_st_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
831{
832 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
833
834 /* on a generic failure, the timer callback will recover */
835 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
836 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
837 if (old_state != GPRS_SNS_ST_SIZE)
838 gss->N = 0;
839
840 gss->alive = false;
841
842 ns2_sns_compute_local_ep_from_binds(fi);
843
844 /* take the first bind or take the next bind */
845 if (!gss->initial_bind) {
846 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
847 } else {
848 if (gss->initial_bind->list.next != &gss->binds) {
849 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
850 } else {
851 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
852 }
853 }
854
855
856 /* setup the NSVC */
857 if (!gss->sns_nsvc) {
858 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
859 struct osmo_sockaddr *remote = &gss->initial->saddr;
860 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
861 if (!gss->sns_nsvc)
862 return;
863 gss->sns_nsvc->sns_only = true;
864 }
865
Alexander Couzense769f522020-12-07 07:37:07 +0100866
Alexander Couzens6a161492020-07-12 13:45:50 +0200867 if (gss->num_max_ip4_remote > 0)
868 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->num_max_ip4_remote, -1);
869 else
870 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 +0200871}
872
873static void ns2_sns_st_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
874{
875 struct tlv_parsed *tp = NULL;
Alexander Couzens3df58862021-02-05 17:18:08 +0100876 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6a161492020-07-12 13:45:50 +0200877
878 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100879 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200880 tp = (struct tlv_parsed *) data;
881 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
882 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
883 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
884 /* TODO: What to do? */
885 } else {
Alexander Couzens3df58862021-02-05 17:18:08 +0100886 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 +0200887 }
888 break;
889 default:
890 OSMO_ASSERT(0);
891 }
892}
893
894static void ns2_sns_st_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
895{
896 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100897
898 if (old_state != GPRS_SNS_ST_CONFIG_BSS)
899 gss->N = 0;
900
Alexander Couzens6a161492020-07-12 13:45:50 +0200901 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200902 switch (gss->ip) {
903 case IPv4:
904 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100905 gss->ip4_local, gss->num_ip4_local,
906 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200907 break;
908 case IPv6:
909 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100910 NULL, 0,
911 gss->ip6_local, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200912 break;
913 }
914}
915
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100916/* calculate the timeout of the configured state. the configured
917 * state will fail if not at least one NS-VC is alive within X second.
918 */
919static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
920{
921 int secs;
922 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
923 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
924 secs += nsi->timeout[NS_TOUT_TNS_TEST];
925
926 return secs;
927}
Alexander Couzens6a161492020-07-12 13:45:50 +0200928
929static void ns_sns_st_config_sgsn_ip4(struct osmo_fsm_inst *fi, uint32_t event, void *data)
930{
931 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
932 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
933 const struct gprs_ns_ie_ip4_elem *v4_list;
934 unsigned int num_v4;
935 struct tlv_parsed *tp = NULL;
936
937 uint8_t cause;
938
939 tp = (struct tlv_parsed *) data;
940
941 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
942 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
943 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
944 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
945 return;
946 }
947 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
948 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
949 /* realloc to the new size */
950 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote,
951 struct gprs_ns_ie_ip4_elem,
952 gss->num_ip4_remote+num_v4);
953 /* append the new entries to the end of the list */
954 memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list));
955 gss->num_ip4_remote += num_v4;
956
957 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
958 gss->num_ip4_remote);
Alexander Couzens67725e22021-02-15 02:37:03 +0100959 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200960 /* check if sum of data / sig weights == 0 */
961 if (ip4_weight_sum_data(gss->ip4_remote, gss->num_ip4_remote) == 0 ||
962 ip4_weight_sum_sig(gss->ip4_remote, gss->num_ip4_remote) == 0) {
963 cause = NS_CAUSE_INVAL_WEIGH;
964 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
965 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
966 return;
967 }
968 create_missing_nsvcs(fi);
969 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
970 /* start the test procedure on ALL NSVCs! */
971 gprs_ns2_start_alive_all_nsvcs(nse);
Alexander Couzens3df58862021-02-05 17:18:08 +0100972 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
Alexander Couzens6a161492020-07-12 13:45:50 +0200973 } else {
974 /* just send CONFIG-ACK */
975 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +0100976 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200977 }
978}
979
980static void ns_sns_st_config_sgsn_ip6(struct osmo_fsm_inst *fi, uint32_t event, void *data)
981{
982 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
983 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
984 const struct gprs_ns_ie_ip6_elem *v6_list;
985 unsigned int num_v6;
986 struct tlv_parsed *tp = NULL;
987
988 uint8_t cause;
989
990 tp = (struct tlv_parsed *) data;
991
992 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
993 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
994 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
995 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
996 return;
997 }
998 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
999 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
1000 /* realloc to the new size */
1001 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote,
1002 struct gprs_ns_ie_ip6_elem,
1003 gss->num_ip6_remote+num_v6);
1004 /* append the new entries to the end of the list */
1005 memcpy(&gss->ip6_remote[gss->num_ip6_remote], v6_list, num_v6*sizeof(*v6_list));
1006 gss->num_ip6_remote += num_v6;
1007
1008 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %u entries\n",
1009 gss->num_ip6_remote);
Alexander Couzens67725e22021-02-15 02:37:03 +01001010 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001011 /* check if sum of data / sig weights == 0 */
1012 if (ip6_weight_sum_data(gss->ip6_remote, gss->num_ip6_remote) == 0 ||
1013 ip6_weight_sum_sig(gss->ip6_remote, gss->num_ip6_remote) == 0) {
1014 cause = NS_CAUSE_INVAL_WEIGH;
1015 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1016 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1017 return;
1018 }
1019 create_missing_nsvcs(fi);
1020 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1021 /* start the test procedure on ALL NSVCs! */
1022 gprs_ns2_start_alive_all_nsvcs(nse);
1023 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1024 } else {
1025 /* just send CONFIG-ACK */
1026 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
Alexander Couzens790a9632021-02-05 17:18:39 +01001027 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001028 }
1029}
1030
Alexander Couzens790a9632021-02-05 17:18:39 +01001031static void ns2_sns_st_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1032{
1033 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1034
1035 if (old_state != GPRS_SNS_ST_CONFIG_SGSN)
1036 gss->N = 0;
1037}
1038
Alexander Couzens6a161492020-07-12 13:45:50 +02001039static void ns2_sns_st_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1040{
1041 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1042
1043 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001044 case GPRS_SNS_EV_RX_CONFIG_END:
1045 case GPRS_SNS_EV_RX_CONFIG:
Alexander Couzens6a161492020-07-12 13:45:50 +02001046
1047#if 0 /* part of incoming SNS-SIZE (doesn't happen on BSS side */
1048 if (TLVP_PRESENT(tp, NS_IE_RESET_FLAG)) {
1049 /* reset all existing config */
1050 if (gss->ip4_remote)
1051 talloc_free(gss->ip4_remote);
1052 gss->num_ip4_remote = 0;
1053 }
1054#endif
1055 /* TODO: reject IPv6 elements on IPv4 mode and vice versa */
1056 switch (gss->ip) {
1057 case IPv4:
1058 ns_sns_st_config_sgsn_ip4(fi, event, data);
1059 break;
1060 case IPv6:
1061 ns_sns_st_config_sgsn_ip6(fi, event, data);
1062 break;
1063 default:
1064 OSMO_ASSERT(0);
1065 }
1066 break;
1067 default:
1068 OSMO_ASSERT(0);
1069 }
1070}
1071
Alexander Couzens67725e22021-02-15 02:37:03 +01001072/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001073static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1074 struct ns2_sns_state *gss,
1075 struct tlv_parsed *tp)
1076{
1077 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1078 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1079 int num_v4 = 0, num_v6 = 0;
1080 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001081 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001082 int rc = 0;
1083
1084 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1085 * check uniqueness within the lists (no doublicate entries)
1086 * check not-known-by-us and sent back a list of unknown/known values
1087 * (abnormal behaviour according to 48.016)
1088 */
1089
1090 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1091 if (gss->ip == IPv4) {
1092 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1093 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1094 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1095 return;
1096 }
1097
1098 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1099 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001100 for (i = 0; i < num_v4; i++) {
1101 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001102 rc = do_sns_add(fi, &v4_list[i], NULL);
1103 if (rc < 0) {
1104 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001105 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001106 do_sns_delete(fi, &v4_list[j], NULL);
1107 cause = -rc;
1108 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1109 break;
1110 }
1111 }
1112 } else { /* IPv6 */
1113 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1114 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1115 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1116 return;
1117 }
1118
1119 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1120 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001121 for (i = 0; i < num_v6; i++) {
1122 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001123 rc = do_sns_add(fi, NULL, &v6_list[i]);
1124 if (rc < 0) {
1125 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001126 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001127 do_sns_delete(fi, NULL, &v6_list[j]);
1128 cause = -rc;
1129 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1130 break;
1131 }
1132 }
1133 }
1134
1135 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1136 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1137}
1138
1139static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1140 struct ns2_sns_state *gss,
1141 struct tlv_parsed *tp)
1142{
1143 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1144 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1145 int num_v4 = 0, num_v6 = 0;
1146 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001147 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001148 int rc = 0;
1149
1150 /* TODO: split up delete into v4 + v6
1151 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1152 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1153 */
1154 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1155 if (gss->ip == IPv4) {
1156 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1157 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1158 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001159 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001160 rc = do_sns_delete(fi, &v4_list[i], NULL);
1161 if (rc < 0) {
1162 cause = -rc;
1163 /* continue to delete others */
1164 }
1165 }
1166 if (cause != 0xff) {
1167 /* TODO: create list of not-deleted and return it */
1168 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1169 return;
1170 }
1171
1172 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1173 /* delete all NS-VCs for given IPv4 address */
1174 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1175 struct gprs_ns_ie_ip4_elem *ip4_remote;
1176 uint32_t ip_addr = *(uint32_t *)(ie+1);
1177 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1178 cause = NS_CAUSE_UNKN_IP_ADDR;
1179 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1180 return;
1181 }
1182 /* make a copy as do_sns_delete() will change the array underneath us */
1183 ip4_remote = talloc_memdup(fi, gss->ip4_remote,
1184 gss->num_ip4_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001185 for (i = 0; i < gss->num_ip4_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001186 if (ip4_remote[i].ip_addr == ip_addr) {
1187 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1188 if (rc < 0) {
1189 cause = -rc;
1190 /* continue to delete others */
1191 }
1192 }
1193 }
1194 talloc_free(ip4_remote);
1195 if (cause != 0xff) {
1196 /* TODO: create list of not-deleted and return it */
1197 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1198 return;
1199 }
1200 } else {
1201 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1202 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1203 return;
1204 }
1205 } else { /* IPv6 */
1206 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1207 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1208 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001209 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001210 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1211 if (rc < 0) {
1212 cause = -rc;
1213 /* continue to delete others */
1214 }
1215 }
1216 if (cause != 0xff) {
1217 /* TODO: create list of not-deleted and return it */
1218 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1219 return;
1220 }
1221 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1222 /* delete all NS-VCs for given IPv4 address */
1223 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1224 struct gprs_ns_ie_ip6_elem *ip6_remote;
1225 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001226 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001227 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1228 cause = NS_CAUSE_UNKN_IP_ADDR;
1229 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1230 return;
1231 }
1232 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1233 /* make a copy as do_sns_delete() will change the array underneath us */
1234 ip6_remote = talloc_memdup(fi, gss->ip6_remote,
1235 gss->num_ip6_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001236 for (i = 0; i < gss->num_ip6_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001237 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1238 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1239 if (rc < 0) {
1240 cause = -rc;
1241 /* continue to delete others */
1242 }
1243 }
1244 }
1245
1246 talloc_free(ip6_remote);
1247 if (cause != 0xff) {
1248 /* TODO: create list of not-deleted and return it */
1249 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1250 return;
1251 }
1252 } else {
1253 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1254 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1255 return;
1256 }
1257 }
1258 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1259}
1260
1261static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1262 struct ns2_sns_state *gss,
1263 struct tlv_parsed *tp)
1264{
1265 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1266 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1267 int num_v4 = 0, num_v6 = 0;
1268 uint8_t trans_id, cause = 0xff;
1269 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001270 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001271
1272 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1273 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1274 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1275 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001276 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001277 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1278 if (rc < 0) {
1279 cause = -rc;
1280 /* continue to others */
1281 }
1282 }
1283 if (cause != 0xff) {
1284 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1285 return;
1286 }
1287 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1288 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1289 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001290 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001291 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1292 if (rc < 0) {
1293 cause = -rc;
1294 /* continue to others */
1295 }
1296 }
1297 if (cause != 0xff) {
1298 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1299 return;
1300 }
1301 } else {
1302 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1303 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1304 return;
1305 }
1306 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1307}
1308
1309static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1310{
1311 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1312 struct tlv_parsed *tp = data;
1313
1314 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001315 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001316 ns2_sns_st_configured_add(fi, gss, tp);
1317 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001318 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001319 ns2_sns_st_configured_delete(fi, gss, tp);
1320 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001321 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001322 ns2_sns_st_configured_change(fi, gss, tp);
1323 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001324 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001325 osmo_timer_del(&fi->timer);
1326 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001327 }
1328}
1329
1330static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1331{
1332 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens138b96f2021-01-25 16:23:29 +01001333 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001334}
1335
1336static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1337 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001338 .in_event_mask = 0, /* handled by all_state_action */
Alexander Couzens6a161492020-07-12 13:45:50 +02001339 .out_state_mask = S(GPRS_SNS_ST_SIZE),
1340 .name = "UNCONFIGURED",
1341 .action = ns2_sns_st_unconfigured,
1342 },
1343 [GPRS_SNS_ST_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001344 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001345 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1346 S(GPRS_SNS_ST_SIZE) |
1347 S(GPRS_SNS_ST_CONFIG_BSS),
1348 .name = "SIZE",
1349 .action = ns2_sns_st_size,
1350 .onenter = ns2_sns_st_size_onenter,
1351 },
1352 [GPRS_SNS_ST_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001353 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001354 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1355 S(GPRS_SNS_ST_CONFIG_BSS) |
1356 S(GPRS_SNS_ST_CONFIG_SGSN) |
1357 S(GPRS_SNS_ST_SIZE),
1358 .name = "CONFIG_BSS",
1359 .action = ns2_sns_st_config_bss,
1360 .onenter = ns2_sns_st_config_bss_onenter,
1361 },
1362 [GPRS_SNS_ST_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001363 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1364 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001365 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1366 S(GPRS_SNS_ST_CONFIG_SGSN) |
1367 S(GPRS_SNS_ST_CONFIGURED) |
1368 S(GPRS_SNS_ST_SIZE),
1369 .name = "CONFIG_SGSN",
1370 .action = ns2_sns_st_config_sgsn,
Alexander Couzens790a9632021-02-05 17:18:39 +01001371 .onenter = ns2_sns_st_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001372 },
1373 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001374 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1375 S(GPRS_SNS_EV_RX_DELETE) |
1376 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1377 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001378 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1379 S(GPRS_SNS_ST_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001380 .name = "CONFIGURED",
1381 .action = ns2_sns_st_configured,
1382 .onenter = ns2_sns_st_configured_onenter,
1383 },
1384};
1385
1386static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1387{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001388 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001389 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1390 struct gprs_ns2_inst *nsi = nse->nsi;
1391
Alexander Couzens90ee9632020-12-07 06:18:32 +01001392 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001393 switch (fi->T) {
1394 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001395 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1396 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size 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_SIZE, nsi->timeout[NS_TOUT_TSNS_PROV], 1);
Alexander Couzensa367d082020-12-21 14:06:24 +01001400 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001401 break;
1402 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001403 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001404 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 +01001405 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001406 } else {
Alexander Couzenscc65a252020-12-21 14:03:58 +01001407 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 +01001408 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001409 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001410 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001411 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1412 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 +01001413 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001414 } else {
1415 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_SGSN, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
1416 }
1417 break;
1418 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001419 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 +01001420 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001421 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001422 }
1423 return 0;
1424}
1425
1426static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1427{
Alexander Couzense769f522020-12-07 07:37:07 +01001428 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001429 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001430 struct ns2_sns_bind *sbind;
1431 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001432
Alexander Couzens67725e22021-02-15 02:37:03 +01001433 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
Alexander Couzense769f522020-12-07 07:37:07 +01001434 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001435 case GPRS_SNS_EV_REQ_NO_NSVC:
Alexander Couzens3ad73362020-12-21 13:53:00 +01001436 /* ignore reselection running */
1437 if (gss->reselection_running)
1438 break;
1439
1440 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001441 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001442 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001443 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
Alexander Couzense769f522020-12-07 07:37:07 +01001444 /* tear down previous state
1445 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1446 gss->reselection_running = true;
1447 gprs_ns2_free_nsvcs(nse);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +01001448 ns2_clear_ipv46_entries(gss);
Alexander Couzense769f522020-12-07 07:37:07 +01001449
1450 /* Choose the next sns endpoint. */
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001451 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds)) {
Alexander Couzense769f522020-12-07 07:37:07 +01001452 gss->initial = NULL;
Alexander Couzens138b96f2021-01-25 16:23:29 +01001453 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
Alexander Couzense769f522020-12-07 07:37:07 +01001454 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1455 return;
1456 } else if (!gss->initial) {
1457 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1458 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1459 /* last entry, continue with first */
1460 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1461 } else {
1462 /* next element is an entry */
1463 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1464 }
1465
1466 gss->reselection_running = false;
1467 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 1);
1468 break;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001469 case GPRS_SNS_EV_REQ_ADD_BIND:
1470 sbind = data;
1471 switch (fi->state) {
1472 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001473 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001474 break;
1475 case GPRS_SNS_ST_SIZE:
1476 /* TODO: add the ip4 element to the list */
1477 break;
1478 case GPRS_SNS_ST_CONFIG_BSS:
1479 case GPRS_SNS_ST_CONFIG_SGSN:
1480 case GPRS_SNS_ST_CONFIGURED:
1481 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1482 break;
1483 }
1484 break;
1485 case GPRS_SNS_EV_REQ_DELETE_BIND:
1486 sbind = data;
1487 switch (fi->state) {
1488 case GPRS_SNS_ST_UNCONFIGURED:
1489 break;
1490 case GPRS_SNS_ST_SIZE:
1491 /* TODO: remove the ip4 element from the list */
1492 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1493 if (nsvc->bind == sbind->bind) {
1494 gprs_ns2_free_nsvc(nsvc);
1495 }
1496 }
1497 break;
1498 case GPRS_SNS_ST_CONFIG_BSS:
1499 case GPRS_SNS_ST_CONFIG_SGSN:
1500 case GPRS_SNS_ST_CONFIGURED:
1501 /* TODO: do an delete SNS-IP procedure */
1502 /* TODO: remove the ip4 element to the list */
1503 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1504 if (nsvc->bind == sbind->bind) {
1505 gprs_ns2_free_nsvc(nsvc);
1506 }
1507 }
1508 break;
1509 }
1510 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1511 talloc_free(sbind);
1512 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001513 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001514}
1515
1516static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1517 .name = "GPRS-NS2-SNS-BSS",
1518 .states = ns2_sns_bss_states,
1519 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001520 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1521 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001522 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1523 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Alexander Couzens6a161492020-07-12 13:45:50 +02001524 .allstate_action = ns2_sns_st_all_action,
1525 .cleanup = NULL,
1526 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001527 .event_names = gprs_sns_event_names,
1528 .pre_term = NULL,
1529 .log_subsys = DLNS,
1530};
1531
Harald Welte5bef2cc2020-09-18 22:33:24 +02001532/*! Allocate an IP-SNS FSM for the BSS side.
1533 * \param[in] nse NS Entity in which the FSM runs
1534 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001535 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001536struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1537 const char *id)
1538{
1539 struct osmo_fsm_inst *fi;
1540 struct ns2_sns_state *gss;
1541
1542 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1543 if (!fi)
1544 return fi;
1545
1546 gss = talloc_zero(fi, struct ns2_sns_state);
1547 if (!gss)
1548 goto err;
1549
1550 fi->priv = gss;
1551 gss->nse = nse;
Alexander Couzense769f522020-12-07 07:37:07 +01001552 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001553 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001554
1555 return fi;
1556err:
1557 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1558 return NULL;
1559}
1560
Harald Welte5bef2cc2020-09-18 22:33:24 +02001561/*! main entry point for receiving SNS messages from the network.
1562 * \param[in] nsvc NS-VC on which the message was received
1563 * \param[in] msg message buffer of the IP-SNS message
1564 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001565 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001566int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001567{
1568 struct gprs_ns2_nse *nse = nsvc->nse;
1569 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1570 uint16_t nsei = nsvc->nse->nsei;
1571 struct osmo_fsm_inst *fi;
1572
1573 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001574 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1575 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001576 return -EINVAL;
1577 }
1578
Alexander Couzens6a161492020-07-12 13:45:50 +02001579 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1580 fi = nse->bss_sns_fi;
1581
Harald Weltef2949742021-01-20 14:54:14 +01001582 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1583 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1584
Alexander Couzens6a161492020-07-12 13:45:50 +02001585 switch (nsh->pdu_type) {
1586 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001587 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001588 break;
1589 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001590 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001591 break;
1592 case SNS_PDUT_CONFIG:
1593 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001594 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001595 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001596 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001597 break;
1598 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001599 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001600 break;
1601 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001602 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001603 break;
1604 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001605 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001606 break;
1607 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001608 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001609 break;
1610 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001611 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001612 break;
1613 default:
Harald Weltef2949742021-01-20 14:54:14 +01001614 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1615 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001616 return -EINVAL;
1617 }
1618
1619 return 0;
1620}
1621
1622#include <osmocom/vty/vty.h>
1623#include <osmocom/vty/misc.h>
1624
Harald Welte1262c4f2021-01-19 20:58:33 +01001625static 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 +02001626{
1627 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001628 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001629 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1630}
1631
Harald Welte1262c4f2021-01-19 20:58:33 +01001632static 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 +02001633{
1634 char ip_addr[INET6_ADDRSTRLEN] = {};
1635 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1636 strcpy(ip_addr, "Invalid IPv6");
1637
Harald Welte1262c4f2021-01-19 20:58:33 +01001638 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001639 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1640}
1641
Harald Welte5bef2cc2020-09-18 22:33:24 +02001642/*! Dump the IP-SNS state to a vty.
1643 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001644 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001645 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1646 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001647void 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 +02001648{
1649 struct ns2_sns_state *gss;
1650 unsigned int i;
1651
1652 if (!nse->bss_sns_fi)
1653 return;
1654
Harald Welte1262c4f2021-01-19 20:58:33 +01001655 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001656 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1657
Harald Welte1262c4f2021-01-19 20:58:33 +01001658 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1659 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001660
1661 if (gss->num_ip4_local && gss->num_ip4_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001662 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001663 for (i = 0; i < gss->num_ip4_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001664 vty_dump_sns_ip4(vty, prefix, &gss->ip4_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001665
Harald Welte1262c4f2021-01-19 20:58:33 +01001666 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001667 for (i = 0; i < gss->num_ip4_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001668 vty_dump_sns_ip4(vty, prefix, &gss->ip4_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001669 }
1670
1671 if (gss->num_ip6_local && gss->num_ip6_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001672 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001673 for (i = 0; i < gss->num_ip6_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001674 vty_dump_sns_ip6(vty, prefix, &gss->ip6_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001675
Harald Welte1262c4f2021-01-19 20:58:33 +01001676 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001677 for (i = 0; i < gss->num_ip6_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001678 vty_dump_sns_ip6(vty, prefix, &gss->ip6_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001679 }
1680}
1681
Alexander Couzens412bc342020-11-19 05:24:37 +01001682/*! write IP-SNS to a vty
1683 * \param[in] vty VTY to which the state shall be printed
1684 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001685void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001686{
1687 struct ns2_sns_state *gss;
1688 struct osmo_sockaddr_str addr_str;
1689 struct sns_endpoint *endpoint;
1690
1691 if (!nse->bss_sns_fi)
1692 return;
1693
1694 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1695 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001696 /* It's unlikely that an error happens, but let's better be safe. */
1697 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1698 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001699 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001700 }
1701}
1702
Alexander Couzense769f522020-12-07 07:37:07 +01001703static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1704 const struct osmo_sockaddr *saddr)
1705{
1706 struct sns_endpoint *endpoint;
1707
1708 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1709 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1710 return endpoint;
1711 }
1712
1713 return NULL;
1714}
1715
1716/*! gprs_ns2_sns_add_endpoint
1717 * \param[in] nse
1718 * \param[in] sockaddr
1719 * \return
1720 */
1721int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1722 const struct osmo_sockaddr *saddr)
1723{
1724 struct ns2_sns_state *gss;
1725 struct sns_endpoint *endpoint;
1726 bool do_selection = false;
1727
1728 if (nse->ll != GPRS_NS2_LL_UDP) {
1729 return -EINVAL;
1730 }
1731
Alexander Couzens138b96f2021-01-25 16:23:29 +01001732 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001733 return -EINVAL;
1734 }
1735
1736 gss = nse->bss_sns_fi->priv;
1737
1738 if (ns2_get_sns_endpoint(gss, saddr))
1739 return -EADDRINUSE;
1740
1741 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1742 if (!endpoint)
1743 return -ENOMEM;
1744
1745 endpoint->saddr = *saddr;
1746 if (llist_empty(&gss->sns_endpoints))
1747 do_selection = true;
1748
1749 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1750 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001751 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001752
1753 return 0;
1754}
1755
1756/*! gprs_ns2_sns_del_endpoint
1757 * \param[in] nse
1758 * \param[in] sockaddr
1759 * \return 0 on success, otherwise < 0
1760 */
1761int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1762 const struct osmo_sockaddr *saddr)
1763{
1764 struct ns2_sns_state *gss;
1765 struct sns_endpoint *endpoint;
1766
1767 if (nse->ll != GPRS_NS2_LL_UDP) {
1768 return -EINVAL;
1769 }
1770
Alexander Couzens138b96f2021-01-25 16:23:29 +01001771 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001772 return -EINVAL;
1773 }
1774
1775 gss = nse->bss_sns_fi->priv;
1776 endpoint = ns2_get_sns_endpoint(gss, saddr);
1777 if (!endpoint)
1778 return -ENOENT;
1779
1780 /* if this is an unused SNS endpoint it's done */
1781 if (gss->initial != endpoint) {
1782 llist_del(&endpoint->list);
1783 talloc_free(endpoint);
1784 return 0;
1785 }
1786
Alexander Couzens67725e22021-02-15 02:37:03 +01001787 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001788 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001789 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001790 "Closing all NS-VC and restart SNS-SIZE procedure"
1791 "with a remaining SNS endpoint.\n");
1792
1793 /* Continue with the next endpoint in the list.
1794 * Special case if the endpoint is at the start or end of the list */
1795 if (endpoint->list.prev == &gss->sns_endpoints ||
1796 endpoint->list.next == &gss->sns_endpoints)
1797 gss->initial = NULL;
1798 else
1799 gss->initial = llist_entry(endpoint->list.next->prev,
1800 struct sns_endpoint,
1801 list);
1802
1803 llist_del(&endpoint->list);
1804 gprs_ns2_free_nsvcs(nse);
1805 talloc_free(endpoint);
1806
1807 return 0;
1808}
1809
1810/*! gprs_ns2_sns_count
1811 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1812 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1813 */
1814int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1815{
1816 struct ns2_sns_state *gss;
1817 struct sns_endpoint *endpoint;
1818 int count = 0;
1819
1820 if (nse->ll != GPRS_NS2_LL_UDP) {
1821 return -EINVAL;
1822 }
1823
Alexander Couzens138b96f2021-01-25 16:23:29 +01001824 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001825 return -EINVAL;
1826 }
1827
1828 gss = nse->bss_sns_fi->priv;
1829 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1830 count++;
1831
1832 return count;
1833}
1834
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001835void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1836{
1837 struct ns2_sns_state *gss;
1838 struct gprs_ns2_vc *tmp;
1839
1840 if (!nse->bss_sns_fi)
1841 return;
1842
1843 gss = nse->bss_sns_fi->priv;
1844 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1845 return;
1846
1847 if (alive == gss->alive)
1848 return;
1849
1850 /* check if this is the current SNS NS-VC */
1851 if (nsvc == gss->sns_nsvc) {
1852 /* only replace the SNS NS-VC if there are other alive NS-VC.
1853 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1854 * and couldn't confirm yet if the NS-VC comes up */
1855 if (gss->alive && !alive)
1856 ns2_sns_replace_nsvc(nsvc);
1857 }
1858
1859 if (alive) {
1860 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001861 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001862 } else {
1863 /* is there at least another alive nsvc? */
1864 llist_for_each_entry(tmp, &nse->nsvc, list) {
1865 if (ns2_vc_is_unblocked(tmp))
1866 return;
1867 }
1868
1869 /* all NS-VC have failed */
1870 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001871 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001872 }
1873}
1874
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001875int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1876 struct gprs_ns2_vc_bind *bind)
1877{
1878 struct ns2_sns_state *gss;
1879 struct ns2_sns_bind *tmp;
1880
1881 OSMO_ASSERT(nse->bss_sns_fi);
1882 gss = nse->bss_sns_fi->priv;
1883
1884 if (!gprs_ns2_is_ip_bind(bind)) {
1885 return -EINVAL;
1886 }
1887
1888 if (!llist_empty(&gss->binds)) {
1889 llist_for_each_entry(tmp, &gss->binds, list) {
1890 if (tmp->bind == bind)
1891 return -EALREADY;
1892 }
1893 }
1894
1895 tmp = talloc_zero(gss, struct ns2_sns_bind);
1896 if (!tmp)
1897 return -ENOMEM;
1898 tmp->bind = bind;
1899 llist_add_tail(&tmp->list, &gss->binds);
1900
1901 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
1902 return 0;
1903}
1904
1905/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
1906int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
1907 struct gprs_ns2_vc_bind *bind)
1908{
1909 struct ns2_sns_state *gss;
1910 struct ns2_sns_bind *tmp, *tmp2;
1911 bool found = false;
1912
1913 if (!nse->bss_sns_fi)
1914 return -EINVAL;
1915
1916 gss = nse->bss_sns_fi->priv;
1917 if (gss->initial_bind && gss->initial_bind->bind == bind) {
1918 if (gss->initial_bind->list.prev == &gss->binds)
1919 gss->initial_bind = NULL;
1920 else
1921 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
1922 }
1923
1924 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
1925 if (tmp->bind == bind) {
1926 llist_del(&tmp->list);
1927 found = true;
1928 }
1929 }
1930
1931 if (!found)
1932 return -ENOENT;
1933
1934 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
1935 return 0;
1936}
1937
Alexander Couzensc4704762021-02-08 23:13:12 +01001938/* Update SNS weights
1939 * \param[in] nsvc the NSVC which should be updated
1940 */
1941void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
1942{
1943 /* TODO: implement weights after binds per sns implemented */
1944}
1945
Alexander Couzens6a161492020-07-12 13:45:50 +02001946/* initialize osmo_ctx on main tread */
1947static __attribute__((constructor)) void on_dso_load_ctx(void)
1948{
1949 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
1950}