blob: 1af96319394849eb31f35184e152ddb1e30a31ca [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \file gprs_ns2_sns.c
2 * NS Sub-Network Service Protocol implementation
3 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
4 * as well as its successor 3GPP TS 48.016 */
5
Harald Weltec1c7e4a2021-03-02 20:47:29 +01006/* (C) 2018-2021 by Harald Welte <laforge@gnumonks.org>
Alexander Couzens6a161492020-07-12 13:45:50 +02007 * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
8 * Author: Alexander Couzens <lynxis@fe80.eu>
9 *
10 * All Rights Reserved
11 *
12 * SPDX-License-Identifier: GPL-2.0+
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 *
27 */
28
29/* The BSS NSE only has one SGSN IP address configured, and it will use the SNS procedures
30 * to communicated its local IPs/ports as well as all the SGSN side IPs/ports and
31 * associated weights. The BSS then uses this to establish a full mesh
32 * of NSVCs between all BSS-side IPs/ports and SGSN-side IPs/ports.
33 *
34 * Known limitation/expectation/bugs:
35 * - No concurrent dual stack. It supports either IPv4 or IPv6, but not both at the same time.
36 * - SNS Add/Change/Delete: Doesn't answer on the same NSVC as received SNS ADD/CHANGE/DELETE PDUs.
37 * - SNS Add/Change/Delete: Doesn't communicated the failed IPv4/IPv6 entries on the SNS_ACK.
38 */
39
40#include <errno.h>
41
42#include <netinet/in.h>
43#include <arpa/inet.h>
44#include <stdint.h>
45
46#include <osmocom/core/fsm.h>
47#include <osmocom/core/msgb.h>
48#include <osmocom/core/socket.h>
Alexander Couzens412bc342020-11-19 05:24:37 +010049#include <osmocom/core/sockaddr_str.h>
Alexander Couzens6a161492020-07-12 13:45:50 +020050#include <osmocom/gsm/tlv.h>
51#include <osmocom/gprs/gprs_msgb.h>
52#include <osmocom/gprs/gprs_ns2.h>
53#include <osmocom/gprs/protocol/gsm_08_16.h>
54
55#include "gprs_ns2_internal.h"
56
57#define S(x) (1 << (x))
58
59enum ns2_sns_type {
60 IPv4,
61 IPv6,
62};
63
Harald Welte4f127462021-03-02 20:49:10 +010064enum ns2_sns_role {
65 GPRS_SNS_ROLE_BSS,
66 GPRS_SNS_ROLE_SGSN,
67};
68
Harald Welte694dad52021-03-23 15:22:16 +010069/* BSS-side-only states _ST_BSS_; SGSN-side only states _ST_SGSN_; others shared */
Alexander Couzens6a161492020-07-12 13:45:50 +020070enum gprs_sns_bss_state {
71 GPRS_SNS_ST_UNCONFIGURED,
Harald Welte694dad52021-03-23 15:22:16 +010072 GPRS_SNS_ST_BSS_SIZE, /*!< SNS-SIZE procedure ongoing */
73 GPRS_SNS_ST_BSS_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */
74 GPRS_SNS_ST_BSS_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */
Alexander Couzens6a161492020-07-12 13:45:50 +020075 GPRS_SNS_ST_CONFIGURED,
Harald Welte4f127462021-03-02 20:49:10 +010076 GPRS_SNS_ST_SGSN_WAIT_CONFIG, /* !< SGSN role: Wait for CONFIG from BSS */
77 GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, /* !< SGSN role: Wait for CONFIG-ACK from BSS */
Alexander Couzens6a161492020-07-12 13:45:50 +020078};
79
80enum gprs_sns_event {
Alexander Couzens67725e22021-02-15 02:37:03 +010081 GPRS_SNS_EV_REQ_SELECT_ENDPOINT, /*!< Select a SNS endpoint from the list */
82 GPRS_SNS_EV_RX_SIZE,
83 GPRS_SNS_EV_RX_SIZE_ACK,
84 GPRS_SNS_EV_RX_CONFIG,
85 GPRS_SNS_EV_RX_CONFIG_END, /*!< SNS-CONFIG with end flag received */
86 GPRS_SNS_EV_RX_CONFIG_ACK,
87 GPRS_SNS_EV_RX_ADD,
88 GPRS_SNS_EV_RX_DELETE,
89 GPRS_SNS_EV_RX_CHANGE_WEIGHT,
Harald Welteb9f23872021-03-02 20:48:54 +010090 GPRS_SNS_EV_RX_ACK, /*!< Rx of SNS-ACK (response to ADD/DELETE/CHG_WEIGHT */
Harald Welte04647e12021-03-02 18:50:40 +010091 GPRS_SNS_EV_REQ_NO_NSVC, /*!< no more NS-VC remaining (all dead) */
Alexander Couzens67725e22021-02-15 02:37:03 +010092 GPRS_SNS_EV_REQ_NSVC_ALIVE, /*!< a NS-VC became alive */
Harald Welte04647e12021-03-02 18:50:40 +010093 GPRS_SNS_EV_REQ_ADD_BIND, /*!< add a new local bind to this NSE */
94 GPRS_SNS_EV_REQ_DELETE_BIND, /*!< remove a local bind from this NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +020095};
96
97static const struct value_string gprs_sns_event_names[] = {
Alexander Couzens67725e22021-02-15 02:37:03 +010098 { GPRS_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" },
99 { GPRS_SNS_EV_RX_SIZE, "RX_SIZE" },
100 { GPRS_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" },
101 { GPRS_SNS_EV_RX_CONFIG, "RX_CONFIG" },
102 { GPRS_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" },
103 { GPRS_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" },
104 { GPRS_SNS_EV_RX_ADD, "RX_ADD" },
105 { GPRS_SNS_EV_RX_DELETE, "RX_DELETE" },
Harald Welteb9f23872021-03-02 20:48:54 +0100106 { GPRS_SNS_EV_RX_ACK, "RX_ACK" },
Alexander Couzens67725e22021-02-15 02:37:03 +0100107 { GPRS_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" },
108 { GPRS_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" },
109 { GPRS_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"},
110 { GPRS_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"},
111 { GPRS_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200112 { 0, NULL }
113};
114
Alexander Couzense769f522020-12-07 07:37:07 +0100115struct sns_endpoint {
116 struct llist_head list;
117 struct osmo_sockaddr saddr;
118};
119
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100120struct ns2_sns_bind {
121 struct llist_head list;
122 struct gprs_ns2_vc_bind *bind;
123};
124
Alexander Couzens6a161492020-07-12 13:45:50 +0200125struct ns2_sns_state {
126 struct gprs_ns2_nse *nse;
127
128 enum ns2_sns_type ip;
Harald Welte4f127462021-03-02 20:49:10 +0100129 enum ns2_sns_role role; /* local role: BSS or SGSN */
Alexander Couzens6a161492020-07-12 13:45:50 +0200130
Alexander Couzense769f522020-12-07 07:37:07 +0100131 /* holds the list of initial SNS endpoints */
132 struct llist_head sns_endpoints;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100133 /* list of used struct ns2_sns_bind */
134 struct llist_head binds;
135 /* pointer to the bind which was used to initiate the SNS connection */
136 struct ns2_sns_bind *initial_bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100137 /* prevent recursive reselection */
138 bool reselection_running;
139
140 /* The current initial SNS endpoints.
141 * The initial connection will be moved into the NSE
142 * if configured via SNS. Otherwise it will be removed
143 * in configured state. */
144 struct sns_endpoint *initial;
Alexander Couzens6a161492020-07-12 13:45:50 +0200145 /* all SNS PDU will be sent over this nsvc */
146 struct gprs_ns2_vc *sns_nsvc;
Alexander Couzens90ee9632020-12-07 06:18:32 +0100147 /* timer N */
148 int N;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100149 /* true if at least one nsvc is alive */
150 bool alive;
Alexander Couzens6a161492020-07-12 13:45:50 +0200151
152 /* local configuration to send to the remote end */
153 struct gprs_ns_ie_ip4_elem *ip4_local;
154 size_t num_ip4_local;
155
156 /* local configuration to send to the remote end */
157 struct gprs_ns_ie_ip6_elem *ip6_local;
158 size_t num_ip6_local;
159
160 /* local configuration about our capabilities in terms of connections to
161 * remote (SGSN) side */
162 size_t num_max_nsvcs;
163 size_t num_max_ip4_remote;
164 size_t num_max_ip6_remote;
165
166 /* remote configuration as received */
167 struct gprs_ns_ie_ip4_elem *ip4_remote;
168 unsigned int num_ip4_remote;
169
170 /* remote configuration as received */
171 struct gprs_ns_ie_ip6_elem *ip6_remote;
172 unsigned int num_ip6_remote;
173};
174
175static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi)
176{
177 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
178 return gss->nse;
179}
180
181/* helper function to compute the sum of all (data or signaling) weights */
182static int ip4_weight_sum(const struct gprs_ns_ie_ip4_elem *ip4, unsigned int num,
183 bool data_weight)
184{
185 unsigned int i;
186 int weight_sum = 0;
187
188 for (i = 0; i < num; i++) {
189 if (data_weight)
190 weight_sum += ip4[i].data_weight;
191 else
192 weight_sum += ip4[i].sig_weight;
193 }
194 return weight_sum;
195}
196#define ip4_weight_sum_data(x,y) ip4_weight_sum(x, y, true)
197#define ip4_weight_sum_sig(x,y) ip4_weight_sum(x, y, false)
198
199/* helper function to compute the sum of all (data or signaling) weights */
200static int ip6_weight_sum(const struct gprs_ns_ie_ip6_elem *ip6, unsigned int num,
201 bool data_weight)
202{
203 unsigned int i;
204 int weight_sum = 0;
205
206 for (i = 0; i < num; i++) {
207 if (data_weight)
208 weight_sum += ip6[i].data_weight;
209 else
210 weight_sum += ip6[i].sig_weight;
211 }
212 return weight_sum;
213}
214#define ip6_weight_sum_data(x,y) ip6_weight_sum(x, y, true)
215#define ip6_weight_sum_sig(x,y) ip6_weight_sum(x, y, false)
216
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100217static int nss_weight_sum(const struct ns2_sns_state *nss, bool data_weight)
218{
219 return ip4_weight_sum(nss->ip4_remote, nss->num_ip4_remote, data_weight) +
220 ip6_weight_sum(nss->ip6_remote, nss->num_ip6_remote, data_weight);
221}
222#define nss_weight_sum_data(nss) nss_weight_sum(nss, true)
223#define nss_weight_sum_sig(nss) nss_weight_sum(nss, false)
224
Alexander Couzens6a161492020-07-12 13:45:50 +0200225static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
226 const struct gprs_ns_ie_ip4_elem *ip4)
227{
228 struct osmo_sockaddr sa;
229 /* copy over. Both data structures use network byte order */
230 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
231 sa.u.sin.sin_port = ip4->udp_port;
232 sa.u.sin.sin_family = AF_INET;
233
Alexander Couzens38b19e82020-09-23 23:56:37 +0200234 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200235}
236
237static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
238 const struct gprs_ns_ie_ip6_elem *ip6)
239{
240 struct osmo_sockaddr sa;
241 /* copy over. Both data structures use network byte order */
242 sa.u.sin6.sin6_addr = ip6->ip_addr;
243 sa.u.sin6.sin6_port = ip6->udp_port;
244 sa.u.sin6.sin6_family = AF_INET;
245
Alexander Couzens38b19e82020-09-23 23:56:37 +0200246 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200247}
248
Alexander Couzens125298f2020-10-11 21:22:42 +0200249/*! Return the initial SNS remote socket address
250 * \param nse NS Entity
251 * \return address of the initial SNS connection; NULL in case of error
252 */
253const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
254{
255 struct ns2_sns_state *gss;
256
257 if (!nse->bss_sns_fi)
258 return NULL;
259
260 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100261 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200262}
263
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100264/*! called when a nsvc is beeing freed or the nsvc became dead */
265void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200266{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100267 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200268 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100269 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200270 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200271
272 if (!fi)
273 return;
274
275 gss = (struct ns2_sns_state *) fi->priv;
276 if (nsvc != gss->sns_nsvc)
277 return;
278
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100279 gss->sns_nsvc = NULL;
280 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200281 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100282 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200283 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100284 return;
285 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200286 }
287 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100288 /* the SNS is waiting for its first NS-VC to come up
289 * choose any other nsvc */
290 llist_for_each_entry(tmp, &nse->nsvc, list) {
291 if (nsvc != tmp) {
292 gss->sns_nsvc = tmp;
293 return;
294 }
295 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200296 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100297
Alexander Couzens67725e22021-02-15 02:37:03 +0100298 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200299}
300
Harald Welte46eb7642021-03-04 17:49:59 +0100301static void ns2_clear_ipv46_entries_local(struct ns2_sns_state *gss)
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100302{
303 TALLOC_FREE(gss->ip4_local);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100304 TALLOC_FREE(gss->ip6_local);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100305
306 gss->num_ip4_local = 0;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100307 gss->num_ip6_local = 0;
Harald Welte46eb7642021-03-04 17:49:59 +0100308}
309
310static void ns2_clear_ipv46_entries_remote(struct ns2_sns_state *gss)
311{
312 TALLOC_FREE(gss->ip4_remote);
313 TALLOC_FREE(gss->ip6_remote);
314
315 gss->num_ip4_remote = 0;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100316 gss->num_ip6_remote = 0;
317}
318
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100319static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
320 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200321{
322 struct gprs_ns2_inst *nsi = nse->nsi;
323 struct gprs_ns2_vc *nsvc;
324 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100325
326 /* for every bind, create a connection if bind type == IP */
327 llist_for_each_entry(bind, &nsi->binding, list) {
328 if (bind->ll != GPRS_NS2_LL_UDP)
329 continue;
330 /* ignore failed connection */
331 nsvc = gprs_ns2_ip_connect_inactive(bind,
332 remote,
333 nse, 0);
334 if (!nsvc) {
335 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
336 continue;
337 }
338
339 nsvc->sig_weight = sig_weight;
340 nsvc->data_weight = data_weight;
341 }
342}
343
344static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
345 struct gprs_ns2_nse *nse,
346 const struct gprs_ns_ie_ip4_elem *ip4)
347{
Alexander Couzensc068d862020-10-12 04:11:51 +0200348 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200349 /* copy over. Both data structures use network byte order */
350 remote.u.sin.sin_family = AF_INET;
351 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
352 remote.u.sin.sin_port = ip4->udp_port;
353
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100354 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200355}
356
357static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
358 struct gprs_ns2_nse *nse,
359 const struct gprs_ns_ie_ip6_elem *ip6)
360{
Alexander Couzens6a161492020-07-12 13:45:50 +0200361 struct osmo_sockaddr remote = {};
362 /* copy over. Both data structures use network byte order */
363 remote.u.sin6.sin6_family = AF_INET6;
364 remote.u.sin6.sin6_addr = ip6->ip_addr;
365 remote.u.sin6.sin6_port = ip6->udp_port;
366
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100367 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200368}
369
370
371static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
372{
373 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
374 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
375 struct gprs_ns2_vc *nsvc;
376 struct gprs_ns2_vc_bind *bind;
377 struct osmo_sockaddr remote = { };
378 unsigned int i;
379
380 for (i = 0; i < gss->num_ip4_remote; i++) {
381 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->ip4_remote[i];
382
383 remote.u.sin.sin_family = AF_INET;
384 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
385 remote.u.sin.sin_port = ip4->udp_port;
386
387 llist_for_each_entry(bind, &nse->nsi->binding, list) {
388 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100389 if (bind->ll != GPRS_NS2_LL_UDP)
390 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200391
392 llist_for_each_entry(nsvc, &nse->nsvc, list) {
393 if (nsvc->bind != bind)
394 continue;
395
Alexander Couzensc4229a42020-10-11 20:58:04 +0200396 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200397 found = true;
398 break;
399 }
400 }
401
402 if (!found) {
403 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
404 if (!nsvc) {
405 /* TODO: add to a list to send back a NS-STATUS */
406 continue;
407 }
408 }
409
410 /* update data / signalling weight */
411 nsvc->data_weight = ip4->data_weight;
412 nsvc->sig_weight = ip4->sig_weight;
413 nsvc->sns_only = false;
414 }
415 }
416
417 for (i = 0; i < gss->num_ip6_remote; i++) {
418 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->ip6_remote[i];
419
420 remote.u.sin6.sin6_family = AF_INET6;
421 remote.u.sin6.sin6_addr = ip6->ip_addr;
422 remote.u.sin6.sin6_port = ip6->udp_port;
423
424 llist_for_each_entry(bind, &nse->nsi->binding, list) {
425 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100426 if (bind->ll != GPRS_NS2_LL_UDP)
427 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200428
429 llist_for_each_entry(nsvc, &nse->nsvc, list) {
430 if (nsvc->bind != bind)
431 continue;
432
Alexander Couzensc4229a42020-10-11 20:58:04 +0200433 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200434 found = true;
435 break;
436 }
437 }
438
439 if (!found) {
440 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
441 if (!nsvc) {
442 /* TODO: add to a list to send back a NS-STATUS */
443 continue;
444 }
445 }
446
447 /* update data / signalling weight */
448 nsvc->data_weight = ip6->data_weight;
449 nsvc->sig_weight = ip6->sig_weight;
450 nsvc->sns_only = false;
451 }
452 }
453
454
455 return 0;
456}
457
458/* Add a given remote IPv4 element to gprs_sns_state */
459static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
460{
461 unsigned int i;
462
463 if (gss->num_ip4_remote >= gss->num_max_ip4_remote)
464 return -NS_CAUSE_INVAL_NR_NS_VC;
465
466 /* check for duplicates */
467 for (i = 0; i < gss->num_ip4_remote; i++) {
468 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
469 continue;
470 /* TODO: log message duplicate */
Alexander Couzens6a161492020-07-12 13:45:50 +0200471 return -NS_CAUSE_PROTO_ERR_UNSPEC;
472 }
473
474 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem,
475 gss->num_ip4_remote+1);
476 gss->ip4_remote[gss->num_ip4_remote] = *ip4;
477 gss->num_ip4_remote += 1;
478 return 0;
479}
480
481/* Remove a given remote IPv4 element from gprs_sns_state */
482static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
483{
484 unsigned int i;
485
486 for (i = 0; i < gss->num_ip4_remote; i++) {
487 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
488 continue;
489 /* all array elements < i remain as they are; all > i are shifted left by one */
490 memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1);
491 gss->num_ip4_remote -= 1;
492 return 0;
493 }
494 return -1;
495}
496
497/* update the weights for specified remote IPv4 */
498static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
499{
500 unsigned int i;
501
502 for (i = 0; i < gss->num_ip4_remote; i++) {
503 if (gss->ip4_remote[i].ip_addr != ip4->ip_addr ||
504 gss->ip4_remote[i].udp_port != ip4->udp_port)
505 continue;
506
507 gss->ip4_remote[i].sig_weight = ip4->sig_weight;
508 gss->ip4_remote[i].data_weight = ip4->data_weight;
509 return 0;
510 }
511 return -1;
512}
513
514/* Add a given remote IPv6 element to gprs_sns_state */
515static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
516{
517 if (gss->num_ip6_remote >= gss->num_max_ip6_remote)
518 return -NS_CAUSE_INVAL_NR_NS_VC;
519
520 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, struct gprs_ns_ie_ip6_elem,
521 gss->num_ip6_remote+1);
522 gss->ip6_remote[gss->num_ip6_remote] = *ip6;
523 gss->num_ip6_remote += 1;
524 return 0;
525}
526
527/* Remove a given remote IPv6 element from gprs_sns_state */
528static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
529{
530 unsigned int i;
531
532 for (i = 0; i < gss->num_ip6_remote; i++) {
533 if (memcmp(&gss->ip6_remote[i], ip6, sizeof(*ip6)))
534 continue;
535 /* all array elements < i remain as they are; all > i are shifted left by one */
536 memmove(&gss->ip6_remote[i], &gss->ip6_remote[i+1], gss->num_ip6_remote-i-1);
537 gss->num_ip6_remote -= 1;
538 return 0;
539 }
540 return -1;
541}
542
543/* update the weights for specified remote IPv6 */
544static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
545{
546 unsigned int i;
547
548 for (i = 0; i < gss->num_ip6_remote; i++) {
549 if (memcmp(&gss->ip6_remote[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
550 gss->ip6_remote[i].udp_port != ip6->udp_port)
551 continue;
552 gss->ip6_remote[i].sig_weight = ip6->sig_weight;
553 gss->ip6_remote[i].data_weight = ip6->data_weight;
554 return 0;
555 }
556 return -1;
557}
558
559static 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)
560{
561 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
562 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
563 struct gprs_ns2_vc *nsvc;
564 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200565 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200566 uint8_t new_signal;
567 uint8_t new_data;
568
569 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
570 * signalling weights of all the peer IP endpoints configured for this NSE is
571 * equal to zero or if the resulting sum of the data weights of all the peer IP
572 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
573 * SNS-ACK PDU with a cause code of "Invalid weights". */
574
575 if (ip4) {
576 if (update_remote_ip4_elem(gss, ip4))
577 return -NS_CAUSE_UNKN_IP_EP;
578
579 /* copy over. Both data structures use network byte order */
580 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
581 sa.u.sin.sin_port = ip4->udp_port;
582 sa.u.sin.sin_family = AF_INET;
583 new_signal = ip4->sig_weight;
584 new_data = ip4->data_weight;
585 } else if (ip6) {
586 if (update_remote_ip6_elem(gss, ip6))
587 return -NS_CAUSE_UNKN_IP_EP;
588
589 /* copy over. Both data structures use network byte order */
590 sa.u.sin6.sin6_addr = ip6->ip_addr;
591 sa.u.sin6.sin6_port = ip6->udp_port;
592 sa.u.sin6.sin6_family = AF_INET6;
593 new_signal = ip6->sig_weight;
594 new_data = ip6->data_weight;
595 } else {
596 OSMO_ASSERT(false);
597 }
598
599 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200600 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200601 /* all nsvc in NSE should be IP/UDP nsvc */
602 OSMO_ASSERT(remote);
603
604 if (osmo_sockaddr_cmp(&sa, remote))
605 continue;
606
607 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
608 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
609 nsvc->sig_weight, new_signal);
610
611 nsvc->data_weight = new_data;
612 nsvc->sig_weight = new_signal;
613 }
614
615 return 0;
616}
617
618static int do_sns_delete(struct osmo_fsm_inst *fi,
619 const struct gprs_ns_ie_ip4_elem *ip4,
620 const struct gprs_ns_ie_ip6_elem *ip6)
621{
622 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
623 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
624 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200625 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200626 struct osmo_sockaddr sa = {};
627
628 if (ip4) {
629 if (remove_remote_ip4_elem(gss, ip4) < 0)
630 return -NS_CAUSE_UNKN_IP_EP;
631 /* copy over. Both data structures use network byte order */
632 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
633 sa.u.sin.sin_port = ip4->udp_port;
634 sa.u.sin.sin_family = AF_INET;
635 } else if (ip6) {
636 if (remove_remote_ip6_elem(gss, ip6))
637 return -NS_CAUSE_UNKN_IP_EP;
638
639 /* copy over. Both data structures use network byte order */
640 sa.u.sin6.sin6_addr = ip6->ip_addr;
641 sa.u.sin6.sin6_port = ip6->udp_port;
642 sa.u.sin6.sin6_family = AF_INET6;
643 } else {
644 OSMO_ASSERT(false);
645 }
646
647 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200648 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200649 /* all nsvc in NSE should be IP/UDP nsvc */
650 OSMO_ASSERT(remote);
651 if (osmo_sockaddr_cmp(&sa, remote))
652 continue;
653
654 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
655 gprs_ns2_free_nsvc(nsvc);
656 }
657
658 return 0;
659}
660
661static int do_sns_add(struct osmo_fsm_inst *fi,
662 const struct gprs_ns_ie_ip4_elem *ip4,
663 const struct gprs_ns_ie_ip6_elem *ip6)
664{
665 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
666 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
667 struct gprs_ns2_vc *nsvc;
668 int rc = 0;
669
670 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
671 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
672 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
673 switch (gss->ip) {
674 case IPv4:
675 rc = add_remote_ip4_elem(gss, ip4);
676 break;
677 case IPv6:
678 rc = add_remote_ip6_elem(gss, ip6);
679 break;
680 default:
681 /* the gss->ip is initialized with the bss */
682 OSMO_ASSERT(false);
683 }
684
685 if (rc)
686 return rc;
687
688 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
689 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
690 * unspecified" */
691 switch (gss->ip) {
692 case IPv4:
693 nsvc = nsvc_by_ip4_elem(nse, ip4);
694 if (nsvc) {
695 /* the nsvc should be already in sync with the ip4 / ip6 elements */
696 return -NS_CAUSE_PROTO_ERR_UNSPEC;
697 }
698
699 /* TODO: failure case */
700 ns2_nsvc_create_ip4(fi, nse, ip4);
701 break;
702 case IPv6:
703 nsvc = nsvc_by_ip6_elem(nse, ip6);
704 if (nsvc) {
705 /* the nsvc should be already in sync with the ip4 / ip6 elements */
706 return -NS_CAUSE_PROTO_ERR_UNSPEC;
707 }
708
709 /* TODO: failure case */
710 ns2_nsvc_create_ip6(fi, nse, ip6);
711 break;
712 }
713
714 gprs_ns2_start_alive_all_nsvcs(nse);
715
716 return 0;
717}
718
719
Harald Welte694dad52021-03-23 15:22:16 +0100720static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200721{
Harald Weltef61a9152021-03-02 22:20:17 +0100722 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
723 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzense769f522020-12-07 07:37:07 +0100724 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200725}
726
Harald Welte694dad52021-03-23 15:22:16 +0100727static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200728{
Harald Weltef61a9152021-03-02 22:20:17 +0100729 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200730 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
731 struct gprs_ns2_inst *nsi = nse->nsi;
732 struct tlv_parsed *tp = NULL;
733
Harald Weltef61a9152021-03-02 22:20:17 +0100734 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
735
Alexander Couzens6a161492020-07-12 13:45:50 +0200736 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100737 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200738 tp = data;
739 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
740 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
741 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
742 /* TODO: What to do? */
743 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100744 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200745 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
746 }
747 break;
748 default:
749 OSMO_ASSERT(0);
750 }
751}
752
Harald Welte24920e22021-03-04 13:03:27 +0100753static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200754{
755 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100756 struct gprs_ns_ie_ip4_elem *ip4_elems;
757 struct gprs_ns_ie_ip6_elem *ip6_elems;
758 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100759 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100760 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100761 const struct osmo_sockaddr *sa;
762 struct osmo_sockaddr local;
763 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200764
Harald Welte46eb7642021-03-04 17:49:59 +0100765 ns2_clear_ipv46_entries_local(gss);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100766
Alexander Couzense769f522020-12-07 07:37:07 +0100767 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100768 if (gss->role == GPRS_SNS_ROLE_BSS) {
769 if (!gss->initial)
770 return;
771 remote = &gss->initial->saddr;
772 } else
773 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100774
775 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100776 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100777 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100778 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100779 return;
780 }
781
Alexander Couzense769f522020-12-07 07:37:07 +0100782 switch (gss->ip) {
783 case IPv4:
784 ip4_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip4_elem) * count);
785 if (!ip4_elems)
786 return;
787
788 gss->ip4_local = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100789 llist_for_each_entry(sbind, &gss->binds, list) {
790 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100791 sa = gprs_ns2_ip_bind_sockaddr(bind);
792 if (!sa)
793 continue;
794
795 if (sa->u.sas.ss_family != AF_INET)
796 continue;
797
798 /* check if this is an specific bind */
799 if (sa->u.sin.sin_addr.s_addr == 0) {
800 if (osmo_sockaddr_local_ip(&local, remote))
801 continue;
802
803 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
804 } else {
805 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
806 }
807
808 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100809 ip4_elems->sig_weight = bind->sns_sig_weight;
810 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100811 ip4_elems++;
812 }
813
814 gss->num_ip4_local = count;
Alexander Couzense769f522020-12-07 07:37:07 +0100815 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->num_ip4_local, 8);
816 break;
817 case IPv6:
818 /* IPv6 */
819 ip6_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip6_elem) * count);
820 if (!ip6_elems)
821 return;
822
823 gss->ip6_local = ip6_elems;
824
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100825 llist_for_each_entry(sbind, &gss->binds, list) {
826 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100827 sa = gprs_ns2_ip_bind_sockaddr(bind);
828 if (!sa)
829 continue;
830
831 if (sa->u.sas.ss_family != AF_INET6)
832 continue;
833
834 /* check if this is an specific bind */
835 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
836 if (osmo_sockaddr_local_ip(&local, remote))
837 continue;
838
839 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
840 } else {
841 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
842 }
843
844 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100845 ip6_elems->sig_weight = bind->sns_sig_weight;
846 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100847
848 ip6_elems++;
849 }
850 gss->num_ip6_local = count;
Alexander Couzense769f522020-12-07 07:37:07 +0100851 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->num_ip6_local, 8);
852 break;
853 }
Harald Welte24920e22021-03-04 13:03:27 +0100854}
855
856/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Harald Welte694dad52021-03-23 15:22:16 +0100857static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Harald Welte24920e22021-03-04 13:03:27 +0100858{
859 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
860
Harald Weltef61a9152021-03-02 22:20:17 +0100861 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
862
Harald Welte24920e22021-03-04 13:03:27 +0100863 /* on a generic failure, the timer callback will recover */
864 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
865 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Harald Welte694dad52021-03-23 15:22:16 +0100866 if (old_state != GPRS_SNS_ST_BSS_SIZE)
Harald Welte24920e22021-03-04 13:03:27 +0100867 gss->N = 0;
868
869 gss->alive = false;
870
871 ns2_sns_compute_local_ep_from_binds(fi);
872
873 /* take the first bind or take the next bind */
874 if (!gss->initial_bind) {
875 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
876 } else {
877 if (gss->initial_bind->list.next != &gss->binds) {
878 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
879 } else {
880 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
881 }
882 }
883
884
885 /* setup the NSVC */
886 if (!gss->sns_nsvc) {
887 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
888 struct osmo_sockaddr *remote = &gss->initial->saddr;
889 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
890 if (!gss->sns_nsvc)
891 return;
892 gss->sns_nsvc->sns_only = true;
893 }
894
Alexander Couzense769f522020-12-07 07:37:07 +0100895
Alexander Couzens6a161492020-07-12 13:45:50 +0200896 if (gss->num_max_ip4_remote > 0)
Harald Welte4e41acc2021-03-23 11:55:25 +0100897 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->num_ip4_local, -1);
Alexander Couzens6a161492020-07-12 13:45:50 +0200898 else
Harald Welte4e41acc2021-03-23 11:55:25 +0100899 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200900}
901
Harald Welte694dad52021-03-23 15:22:16 +0100902static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200903{
Harald Weltef61a9152021-03-02 22:20:17 +0100904 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens3df58862021-02-05 17:18:08 +0100905 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Harald Weltef61a9152021-03-02 22:20:17 +0100906 struct tlv_parsed *tp = NULL;
907
908 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzens6a161492020-07-12 13:45:50 +0200909
910 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100911 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200912 tp = (struct tlv_parsed *) data;
913 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
914 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
915 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
916 /* TODO: What to do? */
917 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100918 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_SGSN, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 3);
Alexander Couzens6a161492020-07-12 13:45:50 +0200919 }
920 break;
921 default:
922 OSMO_ASSERT(0);
923 }
924}
925
Harald Welte694dad52021-03-23 15:22:16 +0100926static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200927{
928 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100929
Harald Weltef61a9152021-03-02 22:20:17 +0100930 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
931
Harald Welte694dad52021-03-23 15:22:16 +0100932 if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS)
Alexander Couzens790a9632021-02-05 17:18:39 +0100933 gss->N = 0;
934
Alexander Couzens6a161492020-07-12 13:45:50 +0200935 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200936 switch (gss->ip) {
937 case IPv4:
938 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100939 gss->ip4_local, gss->num_ip4_local,
940 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200941 break;
942 case IPv6:
943 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100944 NULL, 0,
945 gss->ip6_local, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200946 break;
947 }
948}
949
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100950/* calculate the timeout of the configured state. the configured
951 * state will fail if not at least one NS-VC is alive within X second.
952 */
953static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
954{
955 int secs;
956 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
957 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
958 secs += nsi->timeout[NS_TOUT_TNS_TEST];
959
960 return secs;
961}
Alexander Couzens6a161492020-07-12 13:45:50 +0200962
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100963/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
964static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200965{
966 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200967
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100968 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
969 const struct gprs_ns_ie_ip4_elem *v4_list;
970 unsigned int num_v4;
971 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
972 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +0200973
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100974 if (num_v4 && gss->ip6_remote)
975 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +0200976
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100977 /* realloc to the new size */
978 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote,
979 struct gprs_ns_ie_ip4_elem,
980 gss->num_ip4_remote + num_v4);
981 /* append the new entries to the end of the list */
982 memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list));
983 gss->num_ip4_remote += num_v4;
984
985 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
986 gss->num_ip4_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200987 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200988
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100989 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
990 const struct gprs_ns_ie_ip6_elem *v6_list;
991 unsigned int num_v6;
992 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
993 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
994
995 if (num_v6 && gss->ip4_remote)
996 return -NS_CAUSE_INVAL_NR_IPv6_EP;
997
998 /* realloc to the new size */
999 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote,
1000 struct gprs_ns_ie_ip6_elem,
1001 gss->num_ip6_remote + num_v6);
1002 /* append the new entries to the end of the list */
1003 memcpy(&gss->ip6_remote[gss->num_ip6_remote], v6_list, num_v6*sizeof(*v6_list));
1004 gss->num_ip6_remote += num_v6;
1005
1006 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %u entries\n",
1007 gss->num_ip6_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +02001008 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001009
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001010 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001011}
1012
Harald Welte694dad52021-03-23 15:22:16 +01001013static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens790a9632021-02-05 17:18:39 +01001014{
1015 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1016
Harald Weltef61a9152021-03-02 22:20:17 +01001017 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1018
Harald Welte694dad52021-03-23 15:22:16 +01001019 if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN)
Alexander Couzens790a9632021-02-05 17:18:39 +01001020 gss->N = 0;
1021}
1022
Harald Welte694dad52021-03-23 15:22:16 +01001023static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +02001024{
1025 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001026 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1027 uint8_t cause;
1028 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001029
Harald Weltef61a9152021-03-02 22:20:17 +01001030 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1031
Alexander Couzens6a161492020-07-12 13:45:50 +02001032 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001033 case GPRS_SNS_EV_RX_CONFIG_END:
1034 case GPRS_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001035 rc = ns_sns_append_remote_eps(fi, data);
1036 if (rc < 0) {
1037 cause = -rc;
1038 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1039 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1040 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001041 }
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001042 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1043 /* check if sum of data / sig weights == 0 */
1044 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1045 cause = NS_CAUSE_INVAL_WEIGH;
1046 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1047 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1048 return;
1049 }
1050 create_missing_nsvcs(fi);
1051 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1052 /* start the test procedure on ALL NSVCs! */
1053 gprs_ns2_start_alive_all_nsvcs(nse);
1054 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1055 } else {
1056 /* just send CONFIG-ACK */
1057 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1058 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001059 }
1060 break;
1061 default:
1062 OSMO_ASSERT(0);
1063 }
1064}
1065
Alexander Couzens67725e22021-02-15 02:37:03 +01001066/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001067static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1068 struct ns2_sns_state *gss,
1069 struct tlv_parsed *tp)
1070{
1071 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1072 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1073 int num_v4 = 0, num_v6 = 0;
1074 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001075 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001076 int rc = 0;
1077
1078 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1079 * check uniqueness within the lists (no doublicate entries)
1080 * check not-known-by-us and sent back a list of unknown/known values
1081 * (abnormal behaviour according to 48.016)
1082 */
1083
1084 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1085 if (gss->ip == IPv4) {
1086 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1087 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1088 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1089 return;
1090 }
1091
1092 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1093 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001094 for (i = 0; i < num_v4; i++) {
1095 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001096 rc = do_sns_add(fi, &v4_list[i], NULL);
1097 if (rc < 0) {
1098 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001099 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001100 do_sns_delete(fi, &v4_list[j], NULL);
1101 cause = -rc;
1102 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1103 break;
1104 }
1105 }
1106 } else { /* IPv6 */
1107 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1108 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1109 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1110 return;
1111 }
1112
1113 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1114 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001115 for (i = 0; i < num_v6; i++) {
1116 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001117 rc = do_sns_add(fi, NULL, &v6_list[i]);
1118 if (rc < 0) {
1119 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001120 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001121 do_sns_delete(fi, NULL, &v6_list[j]);
1122 cause = -rc;
1123 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1124 break;
1125 }
1126 }
1127 }
1128
1129 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1130 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1131}
1132
1133static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1134 struct ns2_sns_state *gss,
1135 struct tlv_parsed *tp)
1136{
1137 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1138 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1139 int num_v4 = 0, num_v6 = 0;
1140 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001141 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001142 int rc = 0;
1143
1144 /* TODO: split up delete into v4 + v6
1145 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1146 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1147 */
1148 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1149 if (gss->ip == IPv4) {
1150 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1151 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1152 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001153 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001154 rc = do_sns_delete(fi, &v4_list[i], NULL);
1155 if (rc < 0) {
1156 cause = -rc;
1157 /* continue to delete others */
1158 }
1159 }
1160 if (cause != 0xff) {
1161 /* TODO: create list of not-deleted and return it */
1162 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1163 return;
1164 }
1165
1166 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1167 /* delete all NS-VCs for given IPv4 address */
1168 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1169 struct gprs_ns_ie_ip4_elem *ip4_remote;
1170 uint32_t ip_addr = *(uint32_t *)(ie+1);
1171 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1172 cause = NS_CAUSE_UNKN_IP_ADDR;
1173 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1174 return;
1175 }
1176 /* make a copy as do_sns_delete() will change the array underneath us */
1177 ip4_remote = talloc_memdup(fi, gss->ip4_remote,
1178 gss->num_ip4_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001179 for (i = 0; i < gss->num_ip4_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001180 if (ip4_remote[i].ip_addr == ip_addr) {
1181 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1182 if (rc < 0) {
1183 cause = -rc;
1184 /* continue to delete others */
1185 }
1186 }
1187 }
1188 talloc_free(ip4_remote);
1189 if (cause != 0xff) {
1190 /* TODO: create list of not-deleted and return it */
1191 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1192 return;
1193 }
1194 } else {
1195 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1196 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1197 return;
1198 }
1199 } else { /* IPv6 */
1200 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1201 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1202 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001203 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001204 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1205 if (rc < 0) {
1206 cause = -rc;
1207 /* continue to delete others */
1208 }
1209 }
1210 if (cause != 0xff) {
1211 /* TODO: create list of not-deleted and return it */
1212 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1213 return;
1214 }
1215 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1216 /* delete all NS-VCs for given IPv4 address */
1217 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1218 struct gprs_ns_ie_ip6_elem *ip6_remote;
1219 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001220 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001221 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1222 cause = NS_CAUSE_UNKN_IP_ADDR;
1223 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1224 return;
1225 }
1226 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1227 /* make a copy as do_sns_delete() will change the array underneath us */
1228 ip6_remote = talloc_memdup(fi, gss->ip6_remote,
1229 gss->num_ip6_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001230 for (i = 0; i < gss->num_ip6_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001231 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1232 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1233 if (rc < 0) {
1234 cause = -rc;
1235 /* continue to delete others */
1236 }
1237 }
1238 }
1239
1240 talloc_free(ip6_remote);
1241 if (cause != 0xff) {
1242 /* TODO: create list of not-deleted and return it */
1243 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1244 return;
1245 }
1246 } else {
1247 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1248 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1249 return;
1250 }
1251 }
1252 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1253}
1254
1255static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1256 struct ns2_sns_state *gss,
1257 struct tlv_parsed *tp)
1258{
1259 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1260 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1261 int num_v4 = 0, num_v6 = 0;
1262 uint8_t trans_id, cause = 0xff;
1263 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001264 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001265
1266 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1267 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1268 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1269 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001270 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001271 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1272 if (rc < 0) {
1273 cause = -rc;
1274 /* continue to others */
1275 }
1276 }
1277 if (cause != 0xff) {
1278 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1279 return;
1280 }
1281 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1282 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1283 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001284 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001285 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1286 if (rc < 0) {
1287 cause = -rc;
1288 /* continue to others */
1289 }
1290 }
1291 if (cause != 0xff) {
1292 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1293 return;
1294 }
1295 } else {
1296 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1297 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1298 return;
1299 }
1300 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1301}
1302
1303static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1304{
1305 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1306 struct tlv_parsed *tp = data;
1307
1308 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001309 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001310 ns2_sns_st_configured_add(fi, gss, tp);
1311 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001312 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001313 ns2_sns_st_configured_delete(fi, gss, tp);
1314 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001315 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001316 ns2_sns_st_configured_change(fi, gss, tp);
1317 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001318 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001319 osmo_timer_del(&fi->timer);
1320 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001321 }
1322}
1323
1324static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1325{
1326 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens138b96f2021-01-25 16:23:29 +01001327 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001328}
1329
1330static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1331 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001332 .in_event_mask = 0, /* handled by all_state_action */
Harald Welte694dad52021-03-23 15:22:16 +01001333 .out_state_mask = S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001334 .name = "UNCONFIGURED",
Harald Welte694dad52021-03-23 15:22:16 +01001335 .action = ns2_sns_st_bss_unconfigured,
Alexander Couzens6a161492020-07-12 13:45:50 +02001336 },
Harald Welte694dad52021-03-23 15:22:16 +01001337 [GPRS_SNS_ST_BSS_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001338 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001339 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001340 S(GPRS_SNS_ST_BSS_SIZE) |
1341 S(GPRS_SNS_ST_BSS_CONFIG_BSS),
1342 .name = "BSS_SIZE",
1343 .action = ns2_sns_st_bss_size,
1344 .onenter = ns2_sns_st_bss_size_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001345 },
Harald Welte694dad52021-03-23 15:22:16 +01001346 [GPRS_SNS_ST_BSS_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001347 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001348 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001349 S(GPRS_SNS_ST_BSS_CONFIG_BSS) |
1350 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
1351 S(GPRS_SNS_ST_BSS_SIZE),
1352 .name = "BSS_CONFIG_BSS",
1353 .action = ns2_sns_st_bss_config_bss,
1354 .onenter = ns2_sns_st_bss_config_bss_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001355 },
Harald Welte694dad52021-03-23 15:22:16 +01001356 [GPRS_SNS_ST_BSS_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001357 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1358 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001359 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001360 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens6a161492020-07-12 13:45:50 +02001361 S(GPRS_SNS_ST_CONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001362 S(GPRS_SNS_ST_BSS_SIZE),
1363 .name = "BSS_CONFIG_SGSN",
1364 .action = ns2_sns_st_bss_config_sgsn,
1365 .onenter = ns2_sns_st_bss_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001366 },
1367 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001368 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1369 S(GPRS_SNS_EV_RX_DELETE) |
1370 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1371 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001372 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001373 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001374 .name = "CONFIGURED",
1375 .action = ns2_sns_st_configured,
1376 .onenter = ns2_sns_st_configured_onenter,
1377 },
1378};
1379
1380static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1381{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001382 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001383 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1384 struct gprs_ns2_inst *nsi = nse->nsi;
1385
Alexander Couzens90ee9632020-12-07 06:18:32 +01001386 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001387 switch (fi->T) {
1388 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001389 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1390 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001391 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001392 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001393 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_SIZE, nsi->timeout[NS_TOUT_TSNS_PROV], 1);
Alexander Couzensa367d082020-12-21 14:06:24 +01001394 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001395 break;
1396 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001397 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001398 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 +01001399 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001400 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001401 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS, nsi->timeout[NS_TOUT_TSNS_PROV], 2);
Alexander Couzensa367d082020-12-21 14:06:24 +01001402 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001403 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001404 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001405 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1406 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 +01001407 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001408 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001409 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_SGSN, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
Alexander Couzens3df58862021-02-05 17:18:08 +01001410 }
1411 break;
1412 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001413 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 +01001414 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001415 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001416 }
1417 return 0;
1418}
1419
Harald Welte9e37bf42021-03-02 20:48:31 +01001420/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001421static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1422{
1423 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001424 struct ns2_sns_bind *sbind;
1425 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001426
Alexander Couzense769f522020-12-07 07:37:07 +01001427 switch (event) {
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001428 case GPRS_SNS_EV_REQ_ADD_BIND:
1429 sbind = data;
1430 switch (fi->state) {
1431 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001432 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001433 break;
Harald Welte694dad52021-03-23 15:22:16 +01001434 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001435 /* TODO: add the ip4 element to the list */
1436 break;
Harald Welte694dad52021-03-23 15:22:16 +01001437 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1438 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001439 case GPRS_SNS_ST_CONFIGURED:
1440 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1441 break;
1442 }
1443 break;
1444 case GPRS_SNS_EV_REQ_DELETE_BIND:
1445 sbind = data;
1446 switch (fi->state) {
1447 case GPRS_SNS_ST_UNCONFIGURED:
1448 break;
Harald Welte694dad52021-03-23 15:22:16 +01001449 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001450 /* TODO: remove the ip4 element from the list */
1451 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1452 if (nsvc->bind == sbind->bind) {
1453 gprs_ns2_free_nsvc(nsvc);
1454 }
1455 }
1456 break;
Harald Welte694dad52021-03-23 15:22:16 +01001457 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1458 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001459 case GPRS_SNS_ST_CONFIGURED:
1460 /* TODO: do an delete SNS-IP procedure */
1461 /* TODO: remove the ip4 element to the list */
1462 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1463 if (nsvc->bind == sbind->bind) {
1464 gprs_ns2_free_nsvc(nsvc);
1465 }
1466 }
1467 break;
1468 }
1469 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1470 talloc_free(sbind);
1471 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001472 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001473}
1474
Harald Welte9e37bf42021-03-02 20:48:31 +01001475/* allstate-action for BSS role */
1476static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1477{
1478 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1479 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1480
1481 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
1482 switch (event) {
1483 case GPRS_SNS_EV_REQ_NO_NSVC:
1484 /* ignore reselection running */
1485 if (gss->reselection_running)
1486 break;
1487
1488 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1489 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1490 break;
1491 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
1492 /* tear down previous state
1493 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1494 gss->reselection_running = true;
1495 gprs_ns2_free_nsvcs(nse);
Harald Welte46eb7642021-03-04 17:49:59 +01001496 ns2_clear_ipv46_entries_local(gss);
1497 ns2_clear_ipv46_entries_remote(gss);
Harald Welte9e37bf42021-03-02 20:48:31 +01001498
1499 /* Choose the next sns endpoint. */
1500 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds)) {
1501 gss->initial = NULL;
1502 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1503 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1504 return;
1505 } else if (!gss->initial) {
1506 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1507 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1508 /* last entry, continue with first */
1509 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1510 } else {
1511 /* next element is an entry */
1512 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1513 }
1514
1515 gss->reselection_running = false;
Harald Welte694dad52021-03-23 15:22:16 +01001516 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_SIZE, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 1);
Harald Welte9e37bf42021-03-02 20:48:31 +01001517 break;
1518 default:
1519 ns2_sns_st_all_action(fi, event, data);
1520 break;
1521 }
1522}
1523
Alexander Couzens6a161492020-07-12 13:45:50 +02001524static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1525 .name = "GPRS-NS2-SNS-BSS",
1526 .states = ns2_sns_bss_states,
1527 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001528 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1529 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001530 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1531 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001532 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001533 .cleanup = NULL,
1534 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001535 .event_names = gprs_sns_event_names,
1536 .pre_term = NULL,
1537 .log_subsys = DLNS,
1538};
1539
Harald Welte5bef2cc2020-09-18 22:33:24 +02001540/*! Allocate an IP-SNS FSM for the BSS side.
1541 * \param[in] nse NS Entity in which the FSM runs
1542 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001543 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001544struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1545 const char *id)
1546{
1547 struct osmo_fsm_inst *fi;
1548 struct ns2_sns_state *gss;
1549
1550 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1551 if (!fi)
1552 return fi;
1553
1554 gss = talloc_zero(fi, struct ns2_sns_state);
1555 if (!gss)
1556 goto err;
1557
1558 fi->priv = gss;
1559 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001560 gss->role = GPRS_SNS_ROLE_BSS;
Harald Welte24f4df52021-03-04 18:02:54 +01001561 /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */
1562 gss->num_max_ip4_remote = 8192;
1563 gss->num_max_ip6_remote = 8192;
Alexander Couzense769f522020-12-07 07:37:07 +01001564 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001565 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001566
1567 return fi;
1568err:
1569 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1570 return NULL;
1571}
1572
Harald Welte5bef2cc2020-09-18 22:33:24 +02001573/*! main entry point for receiving SNS messages from the network.
1574 * \param[in] nsvc NS-VC on which the message was received
1575 * \param[in] msg message buffer of the IP-SNS message
1576 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001577 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001578int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001579{
1580 struct gprs_ns2_nse *nse = nsvc->nse;
1581 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1582 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001583 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001584 struct osmo_fsm_inst *fi;
1585
1586 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001587 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1588 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001589 return -EINVAL;
1590 }
1591
Alexander Couzens6a161492020-07-12 13:45:50 +02001592 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1593 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001594 gss = (struct ns2_sns_state *) fi->priv;
1595 if (!gss->sns_nsvc)
1596 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001597
Harald Weltef2949742021-01-20 14:54:14 +01001598 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1599 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1600
Alexander Couzens6a161492020-07-12 13:45:50 +02001601 switch (nsh->pdu_type) {
1602 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001603 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001604 break;
1605 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001606 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001607 break;
1608 case SNS_PDUT_CONFIG:
1609 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001610 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001611 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001612 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001613 break;
1614 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001615 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001616 break;
1617 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001618 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001619 break;
1620 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001621 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001622 break;
1623 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001624 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001625 break;
1626 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001627 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001628 break;
1629 default:
Harald Weltef2949742021-01-20 14:54:14 +01001630 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1631 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001632 return -EINVAL;
1633 }
1634
1635 return 0;
1636}
1637
1638#include <osmocom/vty/vty.h>
1639#include <osmocom/vty/misc.h>
1640
Harald Welte1262c4f2021-01-19 20:58:33 +01001641static 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 +02001642{
1643 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001644 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001645 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1646}
1647
Harald Welte1262c4f2021-01-19 20:58:33 +01001648static 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 +02001649{
1650 char ip_addr[INET6_ADDRSTRLEN] = {};
1651 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1652 strcpy(ip_addr, "Invalid IPv6");
1653
Harald Welte1262c4f2021-01-19 20:58:33 +01001654 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001655 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1656}
1657
Harald Welte5bef2cc2020-09-18 22:33:24 +02001658/*! Dump the IP-SNS state to a vty.
1659 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001660 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001661 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1662 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001663void 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 +02001664{
1665 struct ns2_sns_state *gss;
1666 unsigned int i;
1667
1668 if (!nse->bss_sns_fi)
1669 return;
1670
Harald Welte1262c4f2021-01-19 20:58:33 +01001671 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001672 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1673
Harald Welte1262c4f2021-01-19 20:58:33 +01001674 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1675 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001676
1677 if (gss->num_ip4_local && gss->num_ip4_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001678 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001679 for (i = 0; i < gss->num_ip4_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001680 vty_dump_sns_ip4(vty, prefix, &gss->ip4_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001681
Harald Welte1262c4f2021-01-19 20:58:33 +01001682 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001683 for (i = 0; i < gss->num_ip4_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001684 vty_dump_sns_ip4(vty, prefix, &gss->ip4_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001685 }
1686
1687 if (gss->num_ip6_local && gss->num_ip6_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001688 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001689 for (i = 0; i < gss->num_ip6_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001690 vty_dump_sns_ip6(vty, prefix, &gss->ip6_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001691
Harald Welte1262c4f2021-01-19 20:58:33 +01001692 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001693 for (i = 0; i < gss->num_ip6_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001694 vty_dump_sns_ip6(vty, prefix, &gss->ip6_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001695 }
1696}
1697
Alexander Couzens412bc342020-11-19 05:24:37 +01001698/*! write IP-SNS to a vty
1699 * \param[in] vty VTY to which the state shall be printed
1700 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001701void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001702{
1703 struct ns2_sns_state *gss;
1704 struct osmo_sockaddr_str addr_str;
1705 struct sns_endpoint *endpoint;
1706
1707 if (!nse->bss_sns_fi)
1708 return;
1709
1710 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1711 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001712 /* It's unlikely that an error happens, but let's better be safe. */
1713 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1714 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001715 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001716 }
1717}
1718
Alexander Couzense769f522020-12-07 07:37:07 +01001719static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1720 const struct osmo_sockaddr *saddr)
1721{
1722 struct sns_endpoint *endpoint;
1723
1724 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1725 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1726 return endpoint;
1727 }
1728
1729 return NULL;
1730}
1731
1732/*! gprs_ns2_sns_add_endpoint
1733 * \param[in] nse
1734 * \param[in] sockaddr
1735 * \return
1736 */
1737int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1738 const struct osmo_sockaddr *saddr)
1739{
1740 struct ns2_sns_state *gss;
1741 struct sns_endpoint *endpoint;
1742 bool do_selection = false;
1743
1744 if (nse->ll != GPRS_NS2_LL_UDP) {
1745 return -EINVAL;
1746 }
1747
Alexander Couzens138b96f2021-01-25 16:23:29 +01001748 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001749 return -EINVAL;
1750 }
1751
1752 gss = nse->bss_sns_fi->priv;
1753
1754 if (ns2_get_sns_endpoint(gss, saddr))
1755 return -EADDRINUSE;
1756
1757 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1758 if (!endpoint)
1759 return -ENOMEM;
1760
1761 endpoint->saddr = *saddr;
1762 if (llist_empty(&gss->sns_endpoints))
1763 do_selection = true;
1764
1765 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1766 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001767 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001768
1769 return 0;
1770}
1771
1772/*! gprs_ns2_sns_del_endpoint
1773 * \param[in] nse
1774 * \param[in] sockaddr
1775 * \return 0 on success, otherwise < 0
1776 */
1777int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1778 const struct osmo_sockaddr *saddr)
1779{
1780 struct ns2_sns_state *gss;
1781 struct sns_endpoint *endpoint;
1782
1783 if (nse->ll != GPRS_NS2_LL_UDP) {
1784 return -EINVAL;
1785 }
1786
Alexander Couzens138b96f2021-01-25 16:23:29 +01001787 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001788 return -EINVAL;
1789 }
1790
1791 gss = nse->bss_sns_fi->priv;
1792 endpoint = ns2_get_sns_endpoint(gss, saddr);
1793 if (!endpoint)
1794 return -ENOENT;
1795
1796 /* if this is an unused SNS endpoint it's done */
1797 if (gss->initial != endpoint) {
1798 llist_del(&endpoint->list);
1799 talloc_free(endpoint);
1800 return 0;
1801 }
1802
Alexander Couzens67725e22021-02-15 02:37:03 +01001803 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001804 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001805 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001806 "Closing all NS-VC and restart SNS-SIZE procedure"
1807 "with a remaining SNS endpoint.\n");
1808
1809 /* Continue with the next endpoint in the list.
1810 * Special case if the endpoint is at the start or end of the list */
1811 if (endpoint->list.prev == &gss->sns_endpoints ||
1812 endpoint->list.next == &gss->sns_endpoints)
1813 gss->initial = NULL;
1814 else
1815 gss->initial = llist_entry(endpoint->list.next->prev,
1816 struct sns_endpoint,
1817 list);
1818
1819 llist_del(&endpoint->list);
1820 gprs_ns2_free_nsvcs(nse);
1821 talloc_free(endpoint);
1822
1823 return 0;
1824}
1825
1826/*! gprs_ns2_sns_count
1827 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1828 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1829 */
1830int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1831{
1832 struct ns2_sns_state *gss;
1833 struct sns_endpoint *endpoint;
1834 int count = 0;
1835
1836 if (nse->ll != GPRS_NS2_LL_UDP) {
1837 return -EINVAL;
1838 }
1839
Alexander Couzens138b96f2021-01-25 16:23:29 +01001840 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001841 return -EINVAL;
1842 }
1843
1844 gss = nse->bss_sns_fi->priv;
1845 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1846 count++;
1847
1848 return count;
1849}
1850
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001851void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1852{
1853 struct ns2_sns_state *gss;
1854 struct gprs_ns2_vc *tmp;
1855
1856 if (!nse->bss_sns_fi)
1857 return;
1858
1859 gss = nse->bss_sns_fi->priv;
1860 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1861 return;
1862
1863 if (alive == gss->alive)
1864 return;
1865
1866 /* check if this is the current SNS NS-VC */
1867 if (nsvc == gss->sns_nsvc) {
1868 /* only replace the SNS NS-VC if there are other alive NS-VC.
1869 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1870 * and couldn't confirm yet if the NS-VC comes up */
1871 if (gss->alive && !alive)
1872 ns2_sns_replace_nsvc(nsvc);
1873 }
1874
1875 if (alive) {
1876 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001877 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001878 } else {
1879 /* is there at least another alive nsvc? */
1880 llist_for_each_entry(tmp, &nse->nsvc, list) {
1881 if (ns2_vc_is_unblocked(tmp))
1882 return;
1883 }
1884
1885 /* all NS-VC have failed */
1886 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001887 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001888 }
1889}
1890
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001891int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1892 struct gprs_ns2_vc_bind *bind)
1893{
1894 struct ns2_sns_state *gss;
1895 struct ns2_sns_bind *tmp;
1896
1897 OSMO_ASSERT(nse->bss_sns_fi);
1898 gss = nse->bss_sns_fi->priv;
1899
1900 if (!gprs_ns2_is_ip_bind(bind)) {
1901 return -EINVAL;
1902 }
1903
1904 if (!llist_empty(&gss->binds)) {
1905 llist_for_each_entry(tmp, &gss->binds, list) {
1906 if (tmp->bind == bind)
1907 return -EALREADY;
1908 }
1909 }
1910
1911 tmp = talloc_zero(gss, struct ns2_sns_bind);
1912 if (!tmp)
1913 return -ENOMEM;
1914 tmp->bind = bind;
1915 llist_add_tail(&tmp->list, &gss->binds);
1916
1917 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
1918 return 0;
1919}
1920
1921/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
1922int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
1923 struct gprs_ns2_vc_bind *bind)
1924{
1925 struct ns2_sns_state *gss;
1926 struct ns2_sns_bind *tmp, *tmp2;
1927 bool found = false;
1928
1929 if (!nse->bss_sns_fi)
1930 return -EINVAL;
1931
1932 gss = nse->bss_sns_fi->priv;
1933 if (gss->initial_bind && gss->initial_bind->bind == bind) {
1934 if (gss->initial_bind->list.prev == &gss->binds)
1935 gss->initial_bind = NULL;
1936 else
1937 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
1938 }
1939
1940 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
1941 if (tmp->bind == bind) {
1942 llist_del(&tmp->list);
1943 found = true;
1944 }
1945 }
1946
1947 if (!found)
1948 return -ENOENT;
1949
1950 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
1951 return 0;
1952}
1953
Alexander Couzensc4704762021-02-08 23:13:12 +01001954/* Update SNS weights
1955 * \param[in] nsvc the NSVC which should be updated
1956 */
1957void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
1958{
1959 /* TODO: implement weights after binds per sns implemented */
1960}
1961
Harald Welte4f127462021-03-02 20:49:10 +01001962
1963
1964
1965/***********************************************************************
1966 * SGSN role
1967 ***********************************************************************/
1968
1969static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1970{
1971 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1972 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
1973 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
1974}
1975
1976/* We're waiting for inbound SNS-CONFIG from the BSS */
1977static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1978{
1979 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1980 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1981 struct gprs_ns2_inst *nsi = nse->nsi;
1982 uint8_t cause;
1983 int rc;
1984
1985 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
1986
1987 switch (event) {
1988 case GPRS_SNS_EV_RX_CONFIG:
1989 case GPRS_SNS_EV_RX_CONFIG_END:
1990 rc = ns_sns_append_remote_eps(fi, data);
1991 if (rc < 0) {
1992 cause = -rc;
1993 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1994 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1995 return;
1996 }
1997 /* only change state if last CONFIG was received */
1998 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1999 /* ensure sum of data weight / sig weights is > 0 */
2000 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
2001 cause = NS_CAUSE_INVAL_WEIGH;
2002 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2003 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2004 break;
2005 }
2006 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2007 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2008 } else {
2009 /* just send CONFIG-ACK */
2010 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2011 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2012 }
2013 break;
2014 }
2015}
2016
2017static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2018{
2019 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2020 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2021
2022 ns2_sns_compute_local_ep_from_binds(fi);
2023 /* transmit SGSN-oriented SNS-CONFIG */
2024 ns2_tx_sns_config(gss->sns_nsvc, true, gss->ip4_local, gss->num_ip4_local,
2025 gss->ip6_local, gss->num_ip6_local);
2026}
2027
2028/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2029static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2030{
2031 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2032 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2033 struct tlv_parsed *tp = NULL;
2034
2035 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2036
2037 switch (event) {
2038 case GPRS_SNS_EV_RX_CONFIG_ACK:
2039 tp = data;
2040 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2041 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2042 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2043 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2044 break;
2045 }
2046 /* we currently only send one SNS-CONFIG with END FLAG */
2047 if (true) {
2048 create_missing_nsvcs(fi);
2049 /* start the test procedure on ALL NSVCs! */
2050 gprs_ns2_start_alive_all_nsvcs(nse);
2051 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2052 }
2053 break;
2054 }
2055}
2056
2057/* SGSN-side SNS state machine */
2058static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2059 [GPRS_SNS_ST_UNCONFIGURED] = {
2060 .in_event_mask = 0, /* handled by all_state_action */
2061 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2062 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2063 .name = "UNCONFIGURED",
2064 .action = ns2_sns_st_sgsn_unconfigured,
2065 },
2066 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
2067 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
2068 S(GPRS_SNS_EV_RX_CONFIG_END),
2069 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2070 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2071 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2072 .name = "SGSN_WAIT_CONFIG",
2073 .action = ns2_sns_st_sgsn_wait_config,
2074 },
2075 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
2076 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
2077 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2078 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2079 S(GPRS_SNS_ST_CONFIGURED),
2080 .name = "SGSN_WAIT_CONFIG_ACK",
2081 .action = ns2_sns_st_sgsn_wait_config_ack,
2082 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2083 },
2084 [GPRS_SNS_ST_CONFIGURED] = {
2085 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
2086 S(GPRS_SNS_EV_RX_DELETE) |
2087 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
2088 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
2089 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2090 .name = "CONFIGURED",
2091 /* shared with BSS side; once configured there's no difference */
2092 .action = ns2_sns_st_configured,
2093 .onenter = ns2_sns_st_configured_onenter,
2094 },
2095};
2096
2097static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2098{
2099 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2100 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2101 struct gprs_ns2_inst *nsi = nse->nsi;
2102
2103 gss->N++;
2104 switch (fi->T) {
2105 case 3:
2106 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2107 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2108 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2109 } else {
2110 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2111 }
2112 break;
2113 case 4:
2114 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2115 break;
2116 }
2117 return 0;
2118}
2119
2120
2121/* allstate-action for SGSN role */
2122static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2123{
2124 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2125 struct tlv_parsed *tp = NULL;
2126 uint8_t flag;
Harald Weltea2c5af52021-03-04 17:59:35 +01002127 uint8_t cause;
Harald Welte4f127462021-03-02 20:49:10 +01002128
2129 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2130
2131 switch (event) {
2132 case GPRS_SNS_EV_RX_SIZE:
2133 tp = (struct tlv_parsed *) data;
Harald Weltea2c5af52021-03-04 17:59:35 +01002134 /* check for mandatory / conditional IEs */
2135 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) ||
2136 !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) {
2137 cause = NS_CAUSE_MISSING_ESSENT_IE;
2138 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2139 break;
2140 }
2141 if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) &&
2142 !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) {
2143 cause = NS_CAUSE_MISSING_ESSENT_IE;
Harald Welte4f127462021-03-02 20:49:10 +01002144 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2145 break;
2146 }
2147 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2148 if (flag & 1) {
2149 struct gprs_ns2_vc *nsvc, *nsvc2;
2150 /* clear all state */
Harald Welte46eb7642021-03-04 17:49:59 +01002151 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
Harald Welte4f127462021-03-02 20:49:10 +01002152 gss->N = 0;
Harald Welte46eb7642021-03-04 17:49:59 +01002153 ns2_clear_ipv46_entries_local(gss);
2154 ns2_clear_ipv46_entries_remote(gss);
Harald Welte4f127462021-03-02 20:49:10 +01002155 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2156 if (nsvc == gss->sns_nsvc) {
2157 /* keep the NSVC we need for SNS, but unconfigure it */
2158 nsvc->sig_weight = 0;
2159 nsvc->data_weight = 0;
2160 ns2_vc_force_unconfigured(nsvc);
2161 } else {
2162 /* free all other NS-VCs */
2163 gprs_ns2_free_nsvc(nsvc);
2164 }
2165 }
2166 }
2167 /* send SIZE_ACK */
2168 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2169 /* only wait for SNS-CONFIG in case of Reset flag */
2170 if (flag & 1)
2171 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2172 break;
2173 default:
2174 ns2_sns_st_all_action(fi, event, data);
2175 break;
2176 }
2177}
2178
2179static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2180 .name = "GPRS-NS2-SNS-SGSN",
2181 .states = ns2_sns_sgsn_states,
2182 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
2183 .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) |
2184 S(GPRS_SNS_EV_REQ_NO_NSVC) |
2185 S(GPRS_SNS_EV_REQ_ADD_BIND) |
2186 S(GPRS_SNS_EV_REQ_DELETE_BIND),
2187 .allstate_action = ns2_sns_st_all_action_sgsn,
2188 .cleanup = NULL,
2189 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2190 .event_names = gprs_sns_event_names,
2191 .pre_term = NULL,
2192 .log_subsys = DLNS,
2193};
2194
2195/*! Allocate an IP-SNS FSM for the SGSN side.
2196 * \param[in] nse NS Entity in which the FSM runs
2197 * \param[in] id string identifier
2198 * \returns FSM instance on success; NULL on error */
2199struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2200{
2201 struct osmo_fsm_inst *fi;
2202 struct ns2_sns_state *gss;
2203
2204 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2205 if (!fi)
2206 return fi;
2207
2208 gss = talloc_zero(fi, struct ns2_sns_state);
2209 if (!gss)
2210 goto err;
2211
2212 fi->priv = gss;
2213 gss->nse = nse;
2214 gss->role = GPRS_SNS_ROLE_SGSN;
2215 INIT_LLIST_HEAD(&gss->sns_endpoints);
2216 INIT_LLIST_HEAD(&gss->binds);
2217
2218 return fi;
2219err:
2220 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2221 return NULL;
2222}
2223
2224
2225
2226
Alexander Couzens6a161492020-07-12 13:45:50 +02002227/* initialize osmo_ctx on main tread */
2228static __attribute__((constructor)) void on_dso_load_ctx(void)
2229{
2230 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002231 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002232}