blob: 9865acd2b0b055258beb3bdadc6b032773ddb9da [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
Harald Weltee8c61062021-03-24 13:16:27 +0100370static struct gprs_ns2_vc *nsvc_for_bind_and_remote(struct gprs_ns2_nse *nse,
371 struct gprs_ns2_vc_bind *bind,
372 const struct osmo_sockaddr *remote)
373{
374 struct gprs_ns2_vc *nsvc;
375
376 llist_for_each_entry(nsvc, &nse->nsvc, list) {
377 if (nsvc->bind != bind)
378 continue;
379
380 if (!osmo_sockaddr_cmp(remote, gprs_ns2_ip_vc_remote(nsvc)))
381 return nsvc;
382 }
383 return NULL;
384}
Alexander Couzens6a161492020-07-12 13:45:50 +0200385
386static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
387{
388 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
389 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
390 struct gprs_ns2_vc *nsvc;
Harald Welte3053bbb2021-03-24 13:22:18 +0100391 struct ns2_sns_bind *sbind;
Alexander Couzens6a161492020-07-12 13:45:50 +0200392 struct osmo_sockaddr remote = { };
393 unsigned int i;
394
Harald Weltee8c61062021-03-24 13:16:27 +0100395 /* iterate over all remote IPv4 endpoints */
Alexander Couzens6a161492020-07-12 13:45:50 +0200396 for (i = 0; i < gss->num_ip4_remote; i++) {
397 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->ip4_remote[i];
398
399 remote.u.sin.sin_family = AF_INET;
400 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
401 remote.u.sin.sin_port = ip4->udp_port;
402
Harald Welte3053bbb2021-03-24 13:22:18 +0100403 /* iterate over all local binds within this SNS */
404 llist_for_each_entry(sbind, &gss->binds, list) {
405 struct gprs_ns2_vc_bind *bind = sbind->bind;
406
Harald Weltee8c61062021-03-24 13:16:27 +0100407 /* we only care about UDP binds */
Daniel Willmann967e2c12021-01-14 16:58:17 +0100408 if (bind->ll != GPRS_NS2_LL_UDP)
409 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200410
Harald Weltee8c61062021-03-24 13:16:27 +0100411 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
412 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200413 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
414 if (!nsvc) {
415 /* TODO: add to a list to send back a NS-STATUS */
416 continue;
417 }
418 }
419
420 /* update data / signalling weight */
421 nsvc->data_weight = ip4->data_weight;
422 nsvc->sig_weight = ip4->sig_weight;
423 nsvc->sns_only = false;
424 }
425 }
426
Harald Weltee8c61062021-03-24 13:16:27 +0100427 /* iterate over all remote IPv4 endpoints */
Alexander Couzens6a161492020-07-12 13:45:50 +0200428 for (i = 0; i < gss->num_ip6_remote; i++) {
429 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->ip6_remote[i];
430
431 remote.u.sin6.sin6_family = AF_INET6;
432 remote.u.sin6.sin6_addr = ip6->ip_addr;
433 remote.u.sin6.sin6_port = ip6->udp_port;
434
Harald Welte3053bbb2021-03-24 13:22:18 +0100435 /* iterate over all local binds within this SNS */
436 llist_for_each_entry(sbind, &gss->binds, list) {
437 struct gprs_ns2_vc_bind *bind = sbind->bind;
438
Daniel Willmann967e2c12021-01-14 16:58:17 +0100439 if (bind->ll != GPRS_NS2_LL_UDP)
440 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200441
Harald Weltee8c61062021-03-24 13:16:27 +0100442 /* we only care about UDP binds */
443 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
444 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200445 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
446 if (!nsvc) {
447 /* TODO: add to a list to send back a NS-STATUS */
448 continue;
449 }
450 }
451
452 /* update data / signalling weight */
453 nsvc->data_weight = ip6->data_weight;
454 nsvc->sig_weight = ip6->sig_weight;
455 nsvc->sns_only = false;
456 }
457 }
458
459
460 return 0;
461}
462
463/* Add a given remote IPv4 element to gprs_sns_state */
464static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
465{
466 unsigned int i;
467
468 if (gss->num_ip4_remote >= gss->num_max_ip4_remote)
469 return -NS_CAUSE_INVAL_NR_NS_VC;
470
471 /* check for duplicates */
472 for (i = 0; i < gss->num_ip4_remote; i++) {
473 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
474 continue;
475 /* TODO: log message duplicate */
Alexander Couzens6a161492020-07-12 13:45:50 +0200476 return -NS_CAUSE_PROTO_ERR_UNSPEC;
477 }
478
479 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem,
480 gss->num_ip4_remote+1);
481 gss->ip4_remote[gss->num_ip4_remote] = *ip4;
482 gss->num_ip4_remote += 1;
483 return 0;
484}
485
486/* Remove a given remote IPv4 element from gprs_sns_state */
487static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
488{
489 unsigned int i;
490
491 for (i = 0; i < gss->num_ip4_remote; i++) {
492 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
493 continue;
494 /* all array elements < i remain as they are; all > i are shifted left by one */
495 memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1);
496 gss->num_ip4_remote -= 1;
497 return 0;
498 }
499 return -1;
500}
501
502/* update the weights for specified remote IPv4 */
503static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
504{
505 unsigned int i;
506
507 for (i = 0; i < gss->num_ip4_remote; i++) {
508 if (gss->ip4_remote[i].ip_addr != ip4->ip_addr ||
509 gss->ip4_remote[i].udp_port != ip4->udp_port)
510 continue;
511
512 gss->ip4_remote[i].sig_weight = ip4->sig_weight;
513 gss->ip4_remote[i].data_weight = ip4->data_weight;
514 return 0;
515 }
516 return -1;
517}
518
519/* Add a given remote IPv6 element to gprs_sns_state */
520static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
521{
522 if (gss->num_ip6_remote >= gss->num_max_ip6_remote)
523 return -NS_CAUSE_INVAL_NR_NS_VC;
524
525 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, struct gprs_ns_ie_ip6_elem,
526 gss->num_ip6_remote+1);
527 gss->ip6_remote[gss->num_ip6_remote] = *ip6;
528 gss->num_ip6_remote += 1;
529 return 0;
530}
531
532/* Remove a given remote IPv6 element from gprs_sns_state */
533static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
534{
535 unsigned int i;
536
537 for (i = 0; i < gss->num_ip6_remote; i++) {
538 if (memcmp(&gss->ip6_remote[i], ip6, sizeof(*ip6)))
539 continue;
540 /* all array elements < i remain as they are; all > i are shifted left by one */
541 memmove(&gss->ip6_remote[i], &gss->ip6_remote[i+1], gss->num_ip6_remote-i-1);
542 gss->num_ip6_remote -= 1;
543 return 0;
544 }
545 return -1;
546}
547
548/* update the weights for specified remote IPv6 */
549static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
550{
551 unsigned int i;
552
553 for (i = 0; i < gss->num_ip6_remote; i++) {
554 if (memcmp(&gss->ip6_remote[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
555 gss->ip6_remote[i].udp_port != ip6->udp_port)
556 continue;
557 gss->ip6_remote[i].sig_weight = ip6->sig_weight;
558 gss->ip6_remote[i].data_weight = ip6->data_weight;
559 return 0;
560 }
561 return -1;
562}
563
564static 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)
565{
566 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
567 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
568 struct gprs_ns2_vc *nsvc;
569 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200570 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200571 uint8_t new_signal;
572 uint8_t new_data;
573
574 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
575 * signalling weights of all the peer IP endpoints configured for this NSE is
576 * equal to zero or if the resulting sum of the data weights of all the peer IP
577 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
578 * SNS-ACK PDU with a cause code of "Invalid weights". */
579
580 if (ip4) {
581 if (update_remote_ip4_elem(gss, ip4))
582 return -NS_CAUSE_UNKN_IP_EP;
583
584 /* copy over. Both data structures use network byte order */
585 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
586 sa.u.sin.sin_port = ip4->udp_port;
587 sa.u.sin.sin_family = AF_INET;
588 new_signal = ip4->sig_weight;
589 new_data = ip4->data_weight;
590 } else if (ip6) {
591 if (update_remote_ip6_elem(gss, ip6))
592 return -NS_CAUSE_UNKN_IP_EP;
593
594 /* copy over. Both data structures use network byte order */
595 sa.u.sin6.sin6_addr = ip6->ip_addr;
596 sa.u.sin6.sin6_port = ip6->udp_port;
597 sa.u.sin6.sin6_family = AF_INET6;
598 new_signal = ip6->sig_weight;
599 new_data = ip6->data_weight;
600 } else {
601 OSMO_ASSERT(false);
602 }
603
604 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200605 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200606 /* all nsvc in NSE should be IP/UDP nsvc */
607 OSMO_ASSERT(remote);
608
609 if (osmo_sockaddr_cmp(&sa, remote))
610 continue;
611
612 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
613 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
614 nsvc->sig_weight, new_signal);
615
616 nsvc->data_weight = new_data;
617 nsvc->sig_weight = new_signal;
618 }
619
620 return 0;
621}
622
623static int do_sns_delete(struct osmo_fsm_inst *fi,
624 const struct gprs_ns_ie_ip4_elem *ip4,
625 const struct gprs_ns_ie_ip6_elem *ip6)
626{
627 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
628 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
629 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200630 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200631 struct osmo_sockaddr sa = {};
632
633 if (ip4) {
634 if (remove_remote_ip4_elem(gss, ip4) < 0)
635 return -NS_CAUSE_UNKN_IP_EP;
636 /* copy over. Both data structures use network byte order */
637 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
638 sa.u.sin.sin_port = ip4->udp_port;
639 sa.u.sin.sin_family = AF_INET;
640 } else if (ip6) {
641 if (remove_remote_ip6_elem(gss, ip6))
642 return -NS_CAUSE_UNKN_IP_EP;
643
644 /* copy over. Both data structures use network byte order */
645 sa.u.sin6.sin6_addr = ip6->ip_addr;
646 sa.u.sin6.sin6_port = ip6->udp_port;
647 sa.u.sin6.sin6_family = AF_INET6;
648 } else {
649 OSMO_ASSERT(false);
650 }
651
652 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200653 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200654 /* all nsvc in NSE should be IP/UDP nsvc */
655 OSMO_ASSERT(remote);
656 if (osmo_sockaddr_cmp(&sa, remote))
657 continue;
658
659 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
660 gprs_ns2_free_nsvc(nsvc);
661 }
662
663 return 0;
664}
665
666static int do_sns_add(struct osmo_fsm_inst *fi,
667 const struct gprs_ns_ie_ip4_elem *ip4,
668 const struct gprs_ns_ie_ip6_elem *ip6)
669{
670 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
671 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
672 struct gprs_ns2_vc *nsvc;
673 int rc = 0;
674
675 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
676 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
677 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
678 switch (gss->ip) {
679 case IPv4:
680 rc = add_remote_ip4_elem(gss, ip4);
681 break;
682 case IPv6:
683 rc = add_remote_ip6_elem(gss, ip6);
684 break;
685 default:
686 /* the gss->ip is initialized with the bss */
687 OSMO_ASSERT(false);
688 }
689
690 if (rc)
691 return rc;
692
693 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
694 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
695 * unspecified" */
696 switch (gss->ip) {
697 case IPv4:
698 nsvc = nsvc_by_ip4_elem(nse, ip4);
699 if (nsvc) {
700 /* the nsvc should be already in sync with the ip4 / ip6 elements */
701 return -NS_CAUSE_PROTO_ERR_UNSPEC;
702 }
703
704 /* TODO: failure case */
705 ns2_nsvc_create_ip4(fi, nse, ip4);
706 break;
707 case IPv6:
708 nsvc = nsvc_by_ip6_elem(nse, ip6);
709 if (nsvc) {
710 /* the nsvc should be already in sync with the ip4 / ip6 elements */
711 return -NS_CAUSE_PROTO_ERR_UNSPEC;
712 }
713
714 /* TODO: failure case */
715 ns2_nsvc_create_ip6(fi, nse, ip6);
716 break;
717 }
718
719 gprs_ns2_start_alive_all_nsvcs(nse);
720
721 return 0;
722}
723
724
Harald Welte694dad52021-03-23 15:22:16 +0100725static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200726{
Harald Weltef61a9152021-03-02 22:20:17 +0100727 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
728 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzense769f522020-12-07 07:37:07 +0100729 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200730}
731
Harald Welte694dad52021-03-23 15:22:16 +0100732static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200733{
Harald Weltef61a9152021-03-02 22:20:17 +0100734 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200735 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
736 struct gprs_ns2_inst *nsi = nse->nsi;
737 struct tlv_parsed *tp = NULL;
738
Harald Weltef61a9152021-03-02 22:20:17 +0100739 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
740
Alexander Couzens6a161492020-07-12 13:45:50 +0200741 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100742 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200743 tp = data;
744 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
745 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
746 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
747 /* TODO: What to do? */
748 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100749 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200750 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
751 }
752 break;
753 default:
754 OSMO_ASSERT(0);
755 }
756}
757
Harald Welte01fa6a32021-03-04 19:49:38 +0100758static int ns2_sns_count_num_local_ep(struct osmo_fsm_inst *fi, enum ns2_sns_type stype)
759{
760 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
761 struct ns2_sns_bind *sbind;
762 int count = 0;
763
764 llist_for_each_entry(sbind, &gss->binds, list) {
765 const struct osmo_sockaddr *sa = gprs_ns2_ip_bind_sockaddr(sbind->bind);
766 if (!sa)
767 continue;
768
769 switch (stype) {
770 case IPv4:
771 if (sa->u.sas.ss_family == AF_INET)
772 count++;
773 break;
774 case IPv6:
775 if (sa->u.sas.ss_family == AF_INET6)
776 count++;
777 break;
778 }
779 }
780 return count;
781}
782
Harald Welte24920e22021-03-04 13:03:27 +0100783static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200784{
785 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100786 struct gprs_ns_ie_ip4_elem *ip4_elems;
787 struct gprs_ns_ie_ip6_elem *ip6_elems;
788 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100789 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100790 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100791 const struct osmo_sockaddr *sa;
792 struct osmo_sockaddr local;
793 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200794
Harald Welte46eb7642021-03-04 17:49:59 +0100795 ns2_clear_ipv46_entries_local(gss);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100796
Alexander Couzense769f522020-12-07 07:37:07 +0100797 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100798 if (gss->role == GPRS_SNS_ROLE_BSS) {
799 if (!gss->initial)
800 return;
801 remote = &gss->initial->saddr;
802 } else
803 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100804
805 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100806 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100807 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100808 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100809 return;
810 }
811
Alexander Couzense769f522020-12-07 07:37:07 +0100812 switch (gss->ip) {
813 case IPv4:
Harald Weltec2fbbd72021-03-04 13:14:05 +0100814 ip4_elems = talloc_realloc(fi, gss->ip4_local, struct gprs_ns_ie_ip4_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100815 if (!ip4_elems)
816 return;
817
818 gss->ip4_local = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100819 llist_for_each_entry(sbind, &gss->binds, list) {
820 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100821 sa = gprs_ns2_ip_bind_sockaddr(bind);
822 if (!sa)
823 continue;
824
825 if (sa->u.sas.ss_family != AF_INET)
826 continue;
827
828 /* check if this is an specific bind */
829 if (sa->u.sin.sin_addr.s_addr == 0) {
830 if (osmo_sockaddr_local_ip(&local, remote))
831 continue;
832
833 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
834 } else {
835 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
836 }
837
838 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100839 ip4_elems->sig_weight = bind->sns_sig_weight;
840 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100841 ip4_elems++;
842 }
843
844 gss->num_ip4_local = count;
Alexander Couzense769f522020-12-07 07:37:07 +0100845 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->num_ip4_local, 8);
846 break;
847 case IPv6:
848 /* IPv6 */
Harald Weltec2fbbd72021-03-04 13:14:05 +0100849 ip6_elems = talloc_realloc(fi, gss->ip6_local, struct gprs_ns_ie_ip6_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100850 if (!ip6_elems)
851 return;
852
853 gss->ip6_local = ip6_elems;
854
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100855 llist_for_each_entry(sbind, &gss->binds, list) {
856 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100857 sa = gprs_ns2_ip_bind_sockaddr(bind);
858 if (!sa)
859 continue;
860
861 if (sa->u.sas.ss_family != AF_INET6)
862 continue;
863
864 /* check if this is an specific bind */
865 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
866 if (osmo_sockaddr_local_ip(&local, remote))
867 continue;
868
869 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
870 } else {
871 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
872 }
873
874 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100875 ip6_elems->sig_weight = bind->sns_sig_weight;
876 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100877
878 ip6_elems++;
879 }
880 gss->num_ip6_local = count;
Alexander Couzense769f522020-12-07 07:37:07 +0100881 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->num_ip6_local, 8);
882 break;
883 }
Harald Welte24920e22021-03-04 13:03:27 +0100884}
885
Alexander Couzens6608ce92021-04-26 20:39:46 +0200886static void ns2_sns_choose_next_bind(struct ns2_sns_state *gss)
887{
888 /* take the first bind or take the next bind */
889 if (!gss->initial_bind || gss->initial_bind->list.next == &gss->binds)
890 gss->initial_bind = llist_first_entry_or_null(&gss->binds, struct ns2_sns_bind, list);
891 else
892 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
893}
894
Harald Welte24920e22021-03-04 13:03:27 +0100895/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Harald Welte694dad52021-03-23 15:22:16 +0100896static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Harald Welte24920e22021-03-04 13:03:27 +0100897{
898 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
899
Harald Weltef61a9152021-03-02 22:20:17 +0100900 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
901
Harald Welte24920e22021-03-04 13:03:27 +0100902 /* on a generic failure, the timer callback will recover */
903 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
904 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Harald Welte694dad52021-03-23 15:22:16 +0100905 if (old_state != GPRS_SNS_ST_BSS_SIZE)
Harald Welte24920e22021-03-04 13:03:27 +0100906 gss->N = 0;
907
908 gss->alive = false;
909
910 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens6608ce92021-04-26 20:39:46 +0200911 ns2_sns_choose_next_bind(gss);
Harald Welte24920e22021-03-04 13:03:27 +0100912
913 /* setup the NSVC */
914 if (!gss->sns_nsvc) {
915 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
916 struct osmo_sockaddr *remote = &gss->initial->saddr;
917 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
918 if (!gss->sns_nsvc)
919 return;
Harald Weltec962a2e2021-03-05 08:09:08 +0100920 /* A pre-configured endpoint shall not be used for NSE data or signalling traffic
921 * (with the exception of Size and Configuration procedures) unless it is configured
922 * by the SGSN using the auto-configuration procedures */
Harald Welte24920e22021-03-04 13:03:27 +0100923 gss->sns_nsvc->sns_only = true;
924 }
925
Alexander Couzens6a161492020-07-12 13:45:50 +0200926 if (gss->num_max_ip4_remote > 0)
Harald Welte4e41acc2021-03-23 11:55:25 +0100927 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->num_ip4_local, -1);
Alexander Couzens6a161492020-07-12 13:45:50 +0200928 else
Harald Welte4e41acc2021-03-23 11:55:25 +0100929 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200930}
931
Harald Welte694dad52021-03-23 15:22:16 +0100932static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200933{
Harald Weltef61a9152021-03-02 22:20:17 +0100934 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens3df58862021-02-05 17:18:08 +0100935 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Harald Weltef61a9152021-03-02 22:20:17 +0100936 struct tlv_parsed *tp = NULL;
937
938 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzens6a161492020-07-12 13:45:50 +0200939
940 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100941 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200942 tp = (struct tlv_parsed *) data;
943 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
944 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
945 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
946 /* TODO: What to do? */
947 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100948 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 +0200949 }
950 break;
951 default:
952 OSMO_ASSERT(0);
953 }
954}
955
Harald Welte694dad52021-03-23 15:22:16 +0100956static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200957{
958 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100959
Harald Weltef61a9152021-03-02 22:20:17 +0100960 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
961
Harald Welte694dad52021-03-23 15:22:16 +0100962 if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS)
Alexander Couzens790a9632021-02-05 17:18:39 +0100963 gss->N = 0;
964
Alexander Couzens6a161492020-07-12 13:45:50 +0200965 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200966 switch (gss->ip) {
967 case IPv4:
968 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100969 gss->ip4_local, gss->num_ip4_local,
970 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200971 break;
972 case IPv6:
973 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100974 NULL, 0,
975 gss->ip6_local, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200976 break;
977 }
978}
979
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100980/* calculate the timeout of the configured state. the configured
981 * state will fail if not at least one NS-VC is alive within X second.
982 */
983static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
984{
985 int secs;
986 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
987 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
988 secs += nsi->timeout[NS_TOUT_TNS_TEST];
989
990 return secs;
991}
Alexander Couzens6a161492020-07-12 13:45:50 +0200992
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100993/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
994static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200995{
996 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200997
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100998 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
999 const struct gprs_ns_ie_ip4_elem *v4_list;
1000 unsigned int num_v4;
1001 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1002 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +02001003
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001004 if (num_v4 && gss->ip6_remote)
1005 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +02001006
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001007 /* realloc to the new size */
1008 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote,
1009 struct gprs_ns_ie_ip4_elem,
1010 gss->num_ip4_remote + num_v4);
1011 /* append the new entries to the end of the list */
1012 memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list));
1013 gss->num_ip4_remote += num_v4;
1014
1015 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
1016 gss->num_ip4_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +02001017 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001018
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001019 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1020 const struct gprs_ns_ie_ip6_elem *v6_list;
1021 unsigned int num_v6;
1022 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1023 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
1024
1025 if (num_v6 && gss->ip4_remote)
1026 return -NS_CAUSE_INVAL_NR_IPv6_EP;
1027
1028 /* realloc to the new size */
1029 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote,
1030 struct gprs_ns_ie_ip6_elem,
1031 gss->num_ip6_remote + num_v6);
1032 /* append the new entries to the end of the list */
1033 memcpy(&gss->ip6_remote[gss->num_ip6_remote], v6_list, num_v6*sizeof(*v6_list));
1034 gss->num_ip6_remote += num_v6;
1035
1036 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %u entries\n",
1037 gss->num_ip6_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +02001038 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001039
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001040 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001041}
1042
Harald Welte694dad52021-03-23 15:22:16 +01001043static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens790a9632021-02-05 17:18:39 +01001044{
1045 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1046
Harald Weltef61a9152021-03-02 22:20:17 +01001047 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1048
Harald Welte694dad52021-03-23 15:22:16 +01001049 if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN)
Alexander Couzens790a9632021-02-05 17:18:39 +01001050 gss->N = 0;
1051}
1052
Harald Welte694dad52021-03-23 15:22:16 +01001053static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +02001054{
1055 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001056 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1057 uint8_t cause;
1058 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001059
Harald Weltef61a9152021-03-02 22:20:17 +01001060 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1061
Alexander Couzens6a161492020-07-12 13:45:50 +02001062 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001063 case GPRS_SNS_EV_RX_CONFIG_END:
1064 case GPRS_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001065 rc = ns_sns_append_remote_eps(fi, data);
1066 if (rc < 0) {
1067 cause = -rc;
1068 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1069 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1070 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001071 }
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001072 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1073 /* check if sum of data / sig weights == 0 */
1074 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1075 cause = NS_CAUSE_INVAL_WEIGH;
1076 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1077 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1078 return;
1079 }
1080 create_missing_nsvcs(fi);
1081 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1082 /* start the test procedure on ALL NSVCs! */
1083 gprs_ns2_start_alive_all_nsvcs(nse);
1084 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1085 } else {
1086 /* just send CONFIG-ACK */
1087 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1088 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001089 }
1090 break;
1091 default:
1092 OSMO_ASSERT(0);
1093 }
1094}
1095
Alexander Couzens67725e22021-02-15 02:37:03 +01001096/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001097static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1098 struct ns2_sns_state *gss,
1099 struct tlv_parsed *tp)
1100{
1101 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1102 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1103 int num_v4 = 0, num_v6 = 0;
1104 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001105 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001106 int rc = 0;
1107
1108 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1109 * check uniqueness within the lists (no doublicate entries)
1110 * check not-known-by-us and sent back a list of unknown/known values
1111 * (abnormal behaviour according to 48.016)
1112 */
1113
1114 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1115 if (gss->ip == IPv4) {
1116 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1117 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1118 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1119 return;
1120 }
1121
1122 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1123 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001124 for (i = 0; i < num_v4; i++) {
1125 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001126 rc = do_sns_add(fi, &v4_list[i], NULL);
1127 if (rc < 0) {
1128 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001129 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001130 do_sns_delete(fi, &v4_list[j], NULL);
1131 cause = -rc;
1132 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1133 break;
1134 }
1135 }
1136 } else { /* IPv6 */
1137 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1138 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1139 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1140 return;
1141 }
1142
1143 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1144 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001145 for (i = 0; i < num_v6; i++) {
1146 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001147 rc = do_sns_add(fi, NULL, &v6_list[i]);
1148 if (rc < 0) {
1149 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001150 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001151 do_sns_delete(fi, NULL, &v6_list[j]);
1152 cause = -rc;
1153 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1154 break;
1155 }
1156 }
1157 }
1158
1159 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1160 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1161}
1162
1163static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1164 struct ns2_sns_state *gss,
1165 struct tlv_parsed *tp)
1166{
1167 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1168 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1169 int num_v4 = 0, num_v6 = 0;
1170 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001171 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001172 int rc = 0;
1173
1174 /* TODO: split up delete into v4 + v6
1175 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1176 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1177 */
1178 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1179 if (gss->ip == IPv4) {
1180 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1181 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1182 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001183 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001184 rc = do_sns_delete(fi, &v4_list[i], NULL);
1185 if (rc < 0) {
1186 cause = -rc;
1187 /* continue to delete others */
1188 }
1189 }
1190 if (cause != 0xff) {
1191 /* TODO: create list of not-deleted and return it */
1192 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1193 return;
1194 }
1195
1196 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1197 /* delete all NS-VCs for given IPv4 address */
1198 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1199 struct gprs_ns_ie_ip4_elem *ip4_remote;
1200 uint32_t ip_addr = *(uint32_t *)(ie+1);
1201 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1202 cause = NS_CAUSE_UNKN_IP_ADDR;
1203 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1204 return;
1205 }
1206 /* make a copy as do_sns_delete() will change the array underneath us */
1207 ip4_remote = talloc_memdup(fi, gss->ip4_remote,
1208 gss->num_ip4_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001209 for (i = 0; i < gss->num_ip4_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001210 if (ip4_remote[i].ip_addr == ip_addr) {
1211 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1212 if (rc < 0) {
1213 cause = -rc;
1214 /* continue to delete others */
1215 }
1216 }
1217 }
1218 talloc_free(ip4_remote);
1219 if (cause != 0xff) {
1220 /* TODO: create list of not-deleted and return it */
1221 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1222 return;
1223 }
1224 } else {
1225 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1226 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1227 return;
1228 }
1229 } else { /* IPv6 */
1230 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1231 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1232 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001233 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001234 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1235 if (rc < 0) {
1236 cause = -rc;
1237 /* continue to delete others */
1238 }
1239 }
1240 if (cause != 0xff) {
1241 /* TODO: create list of not-deleted and return it */
1242 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1243 return;
1244 }
1245 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1246 /* delete all NS-VCs for given IPv4 address */
1247 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1248 struct gprs_ns_ie_ip6_elem *ip6_remote;
1249 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001250 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001251 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1252 cause = NS_CAUSE_UNKN_IP_ADDR;
1253 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1254 return;
1255 }
1256 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1257 /* make a copy as do_sns_delete() will change the array underneath us */
1258 ip6_remote = talloc_memdup(fi, gss->ip6_remote,
1259 gss->num_ip6_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001260 for (i = 0; i < gss->num_ip6_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001261 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1262 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1263 if (rc < 0) {
1264 cause = -rc;
1265 /* continue to delete others */
1266 }
1267 }
1268 }
1269
1270 talloc_free(ip6_remote);
1271 if (cause != 0xff) {
1272 /* TODO: create list of not-deleted and return it */
1273 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1274 return;
1275 }
1276 } else {
1277 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1278 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1279 return;
1280 }
1281 }
1282 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1283}
1284
1285static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1286 struct ns2_sns_state *gss,
1287 struct tlv_parsed *tp)
1288{
1289 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1290 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1291 int num_v4 = 0, num_v6 = 0;
1292 uint8_t trans_id, cause = 0xff;
1293 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001294 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001295
1296 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1297 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1298 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1299 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001300 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001301 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1302 if (rc < 0) {
1303 cause = -rc;
1304 /* continue to others */
1305 }
1306 }
1307 if (cause != 0xff) {
1308 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1309 return;
1310 }
1311 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1312 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1313 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001314 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001315 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1316 if (rc < 0) {
1317 cause = -rc;
1318 /* continue to others */
1319 }
1320 }
1321 if (cause != 0xff) {
1322 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1323 return;
1324 }
1325 } else {
1326 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1327 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1328 return;
1329 }
1330 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1331}
1332
1333static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1334{
1335 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1336 struct tlv_parsed *tp = data;
1337
1338 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001339 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001340 ns2_sns_st_configured_add(fi, gss, tp);
1341 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001342 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001343 ns2_sns_st_configured_delete(fi, gss, tp);
1344 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001345 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001346 ns2_sns_st_configured_change(fi, gss, tp);
1347 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001348 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001349 osmo_timer_del(&fi->timer);
1350 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001351 }
1352}
1353
1354static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1355{
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001356 struct gprs_ns2_vc *nsvc;
1357 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001358 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001359 /* NS-VC status updates are only parsed in ST_CONFIGURED.
1360 * Do an initial check if there are any nsvc alive atm */
1361 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1362 if (ns2_vc_is_unblocked(nsvc)) {
1363 gss->alive = true;
1364 osmo_timer_del(&fi->timer);
1365 break;
1366 }
1367 }
1368
Alexander Couzens53e70092021-04-06 15:45:47 +02001369 /* remove the initial NSVC if the NSVC isn't part of the configuration */
1370 if (gss->sns_nsvc->sns_only)
1371 gprs_ns2_free_nsvc(gss->sns_nsvc);
1372
Alexander Couzens138b96f2021-01-25 16:23:29 +01001373 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001374}
1375
1376static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1377 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001378 .in_event_mask = 0, /* handled by all_state_action */
Alexander Couzens0a7c5ee2021-04-10 18:20:21 +02001379 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1380 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001381 .name = "UNCONFIGURED",
Harald Welte694dad52021-03-23 15:22:16 +01001382 .action = ns2_sns_st_bss_unconfigured,
Alexander Couzens6a161492020-07-12 13:45:50 +02001383 },
Harald Welte694dad52021-03-23 15:22:16 +01001384 [GPRS_SNS_ST_BSS_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001385 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001386 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001387 S(GPRS_SNS_ST_BSS_SIZE) |
1388 S(GPRS_SNS_ST_BSS_CONFIG_BSS),
1389 .name = "BSS_SIZE",
1390 .action = ns2_sns_st_bss_size,
1391 .onenter = ns2_sns_st_bss_size_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001392 },
Harald Welte694dad52021-03-23 15:22:16 +01001393 [GPRS_SNS_ST_BSS_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001394 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001395 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001396 S(GPRS_SNS_ST_BSS_CONFIG_BSS) |
1397 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
1398 S(GPRS_SNS_ST_BSS_SIZE),
1399 .name = "BSS_CONFIG_BSS",
1400 .action = ns2_sns_st_bss_config_bss,
1401 .onenter = ns2_sns_st_bss_config_bss_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001402 },
Harald Welte694dad52021-03-23 15:22:16 +01001403 [GPRS_SNS_ST_BSS_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001404 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1405 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001406 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001407 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens6a161492020-07-12 13:45:50 +02001408 S(GPRS_SNS_ST_CONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001409 S(GPRS_SNS_ST_BSS_SIZE),
1410 .name = "BSS_CONFIG_SGSN",
1411 .action = ns2_sns_st_bss_config_sgsn,
1412 .onenter = ns2_sns_st_bss_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001413 },
1414 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001415 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1416 S(GPRS_SNS_EV_RX_DELETE) |
1417 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1418 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001419 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001420 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001421 .name = "CONFIGURED",
1422 .action = ns2_sns_st_configured,
1423 .onenter = ns2_sns_st_configured_onenter,
1424 },
1425};
1426
1427static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1428{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001429 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001430 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1431 struct gprs_ns2_inst *nsi = nse->nsi;
1432
Alexander Couzens90ee9632020-12-07 06:18:32 +01001433 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001434 switch (fi->T) {
1435 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001436 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1437 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001438 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001439 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001440 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 +01001441 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001442 break;
1443 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001444 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001445 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 +01001446 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001447 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001448 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 +01001449 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001450 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001451 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001452 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1453 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 +01001454 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001455 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001456 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 +01001457 }
1458 break;
1459 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001460 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 +01001461 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001462 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001463 }
1464 return 0;
1465}
1466
Harald Welte9e37bf42021-03-02 20:48:31 +01001467/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001468static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1469{
1470 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001471 struct ns2_sns_bind *sbind;
1472 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001473
Alexander Couzense769f522020-12-07 07:37:07 +01001474 switch (event) {
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001475 case GPRS_SNS_EV_REQ_ADD_BIND:
1476 sbind = data;
1477 switch (fi->state) {
1478 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001479 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001480 break;
Harald Welte694dad52021-03-23 15:22:16 +01001481 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001482 /* TODO: add the ip4 element to the list */
1483 break;
Harald Welte694dad52021-03-23 15:22:16 +01001484 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1485 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001486 case GPRS_SNS_ST_CONFIGURED:
1487 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1488 break;
1489 }
1490 break;
1491 case GPRS_SNS_EV_REQ_DELETE_BIND:
1492 sbind = data;
1493 switch (fi->state) {
1494 case GPRS_SNS_ST_UNCONFIGURED:
1495 break;
Harald Welte694dad52021-03-23 15:22:16 +01001496 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001497 /* TODO: remove the ip4 element from the list */
1498 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1499 if (nsvc->bind == sbind->bind) {
1500 gprs_ns2_free_nsvc(nsvc);
1501 }
1502 }
1503 break;
Harald Welte694dad52021-03-23 15:22:16 +01001504 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1505 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001506 case GPRS_SNS_ST_CONFIGURED:
1507 /* TODO: do an delete SNS-IP procedure */
1508 /* TODO: remove the ip4 element to the list */
1509 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1510 if (nsvc->bind == sbind->bind) {
1511 gprs_ns2_free_nsvc(nsvc);
1512 }
1513 }
1514 break;
1515 }
1516 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1517 talloc_free(sbind);
1518 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001519 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001520}
1521
Alexander Couzens31d52e12021-06-05 20:04:04 +02001522/* validate the bss configuration (sns endpoint and binds)
1523 * - no endpoints -> invalid
1524 * - no binds -> invalid
1525 * - only v4 sns endpoints, only v6 binds -> invalid
1526 * - only v4 sns endpoints, but v4 sig weights == 0 -> invalid ...
1527 */
1528static int ns2_sns_bss_valid_configuration(struct ns2_sns_state *gss)
1529{
1530 struct ns2_sns_bind *sbind;
1531 struct sns_endpoint *endpoint;
1532 const struct osmo_sockaddr *addr;
1533 int v4_sig = 0, v4_data = 0, v6_sig = 0, v6_data = 0;
1534 bool v4_endpoints = false;
1535 bool v6_endpoints = false;
1536
1537 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds))
1538 return 0;
1539
1540 llist_for_each_entry(sbind, &gss->binds, list) {
1541 addr = gprs_ns2_ip_bind_sockaddr(sbind->bind);
1542 if (!addr)
1543 continue;
1544 switch (addr->u.sa.sa_family) {
1545 case AF_INET:
1546 v4_sig += sbind->bind->sns_sig_weight;
1547 v4_data += sbind->bind->sns_data_weight;
1548 break;
1549 case AF_INET6:
1550 v6_sig += sbind->bind->sns_sig_weight;
1551 v6_data += sbind->bind->sns_data_weight;
1552 break;
1553 }
1554 }
1555
1556 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
1557 switch (endpoint->saddr.u.sa.sa_family) {
1558 case AF_INET:
1559 v4_endpoints = true;
1560 break;
1561 case AF_INET6:
1562 v6_endpoints = true;
1563 break;
1564 }
1565 }
1566
1567 return (v4_endpoints && v4_sig && v4_data) || (v6_endpoints && v6_sig && v6_data);
1568}
1569
Harald Welte9e37bf42021-03-02 20:48:31 +01001570/* allstate-action for BSS role */
1571static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1572{
1573 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1574 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1575
1576 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
1577 switch (event) {
1578 case GPRS_SNS_EV_REQ_NO_NSVC:
1579 /* ignore reselection running */
1580 if (gss->reselection_running)
1581 break;
1582
1583 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1584 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1585 break;
1586 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
1587 /* tear down previous state
1588 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1589 gss->reselection_running = true;
1590 gprs_ns2_free_nsvcs(nse);
Harald Welte46eb7642021-03-04 17:49:59 +01001591 ns2_clear_ipv46_entries_local(gss);
1592 ns2_clear_ipv46_entries_remote(gss);
Harald Welte9e37bf42021-03-02 20:48:31 +01001593
1594 /* Choose the next sns endpoint. */
Alexander Couzens31d52e12021-06-05 20:04:04 +02001595 if (!ns2_sns_bss_valid_configuration(gss)) {
Harald Welte9e37bf42021-03-02 20:48:31 +01001596 gss->initial = NULL;
1597 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1598 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1599 return;
1600 } else if (!gss->initial) {
1601 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1602 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1603 /* last entry, continue with first */
1604 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1605 } else {
1606 /* next element is an entry */
1607 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1608 }
1609
1610 gss->reselection_running = false;
Harald Welte694dad52021-03-23 15:22:16 +01001611 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 +01001612 break;
1613 default:
1614 ns2_sns_st_all_action(fi, event, data);
1615 break;
1616 }
1617}
1618
Alexander Couzens6a161492020-07-12 13:45:50 +02001619static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1620 .name = "GPRS-NS2-SNS-BSS",
1621 .states = ns2_sns_bss_states,
1622 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001623 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1624 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001625 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1626 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001627 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001628 .cleanup = NULL,
1629 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001630 .event_names = gprs_sns_event_names,
1631 .pre_term = NULL,
1632 .log_subsys = DLNS,
1633};
1634
Harald Welte5bef2cc2020-09-18 22:33:24 +02001635/*! Allocate an IP-SNS FSM for the BSS side.
1636 * \param[in] nse NS Entity in which the FSM runs
1637 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001638 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001639struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1640 const char *id)
1641{
1642 struct osmo_fsm_inst *fi;
1643 struct ns2_sns_state *gss;
1644
1645 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1646 if (!fi)
1647 return fi;
1648
1649 gss = talloc_zero(fi, struct ns2_sns_state);
1650 if (!gss)
1651 goto err;
1652
1653 fi->priv = gss;
1654 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001655 gss->role = GPRS_SNS_ROLE_BSS;
Harald Welte24f4df52021-03-04 18:02:54 +01001656 /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */
1657 gss->num_max_ip4_remote = 8192;
1658 gss->num_max_ip6_remote = 8192;
Alexander Couzense769f522020-12-07 07:37:07 +01001659 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001660 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001661
1662 return fi;
1663err:
1664 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1665 return NULL;
1666}
1667
Harald Welte5bef2cc2020-09-18 22:33:24 +02001668/*! main entry point for receiving SNS messages from the network.
1669 * \param[in] nsvc NS-VC on which the message was received
1670 * \param[in] msg message buffer of the IP-SNS message
1671 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001672 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001673int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001674{
1675 struct gprs_ns2_nse *nse = nsvc->nse;
1676 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1677 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001678 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001679 struct osmo_fsm_inst *fi;
Alexander Couzens7619ed42021-03-24 17:44:03 +01001680 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001681
1682 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001683 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1684 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001685 rc = -EINVAL;
1686 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +02001687 }
1688
Alexander Couzens6a161492020-07-12 13:45:50 +02001689 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1690 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001691 gss = (struct ns2_sns_state *) fi->priv;
1692 if (!gss->sns_nsvc)
1693 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001694
Harald Weltef2949742021-01-20 14:54:14 +01001695 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1696 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1697
Alexander Couzens6a161492020-07-12 13:45:50 +02001698 switch (nsh->pdu_type) {
1699 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001700 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001701 break;
1702 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001703 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001704 break;
1705 case SNS_PDUT_CONFIG:
1706 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001707 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001708 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001709 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001710 break;
1711 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001712 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001713 break;
1714 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001715 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001716 break;
1717 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001718 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001719 break;
1720 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001721 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001722 break;
1723 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001724 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001725 break;
1726 default:
Harald Weltef2949742021-01-20 14:54:14 +01001727 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1728 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001729 rc = -EINVAL;
Alexander Couzens6a161492020-07-12 13:45:50 +02001730 }
1731
Alexander Couzens7619ed42021-03-24 17:44:03 +01001732out:
1733 msgb_free(msg);
1734
1735 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001736}
1737
1738#include <osmocom/vty/vty.h>
1739#include <osmocom/vty/misc.h>
1740
Harald Welte1262c4f2021-01-19 20:58:33 +01001741static 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 +02001742{
1743 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001744 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001745 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1746}
1747
Harald Welte1262c4f2021-01-19 20:58:33 +01001748static 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 +02001749{
1750 char ip_addr[INET6_ADDRSTRLEN] = {};
1751 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1752 strcpy(ip_addr, "Invalid IPv6");
1753
Harald Welte1262c4f2021-01-19 20:58:33 +01001754 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001755 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1756}
1757
Harald Welte5bef2cc2020-09-18 22:33:24 +02001758/*! Dump the IP-SNS state to a vty.
1759 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001760 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001761 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1762 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001763void 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 +02001764{
1765 struct ns2_sns_state *gss;
1766 unsigned int i;
1767
1768 if (!nse->bss_sns_fi)
1769 return;
1770
Harald Welte1262c4f2021-01-19 20:58:33 +01001771 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001772 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1773
Harald Welte1262c4f2021-01-19 20:58:33 +01001774 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1775 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001776
1777 if (gss->num_ip4_local && gss->num_ip4_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001778 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001779 for (i = 0; i < gss->num_ip4_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001780 vty_dump_sns_ip4(vty, prefix, &gss->ip4_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001781
Harald Welte1262c4f2021-01-19 20:58:33 +01001782 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001783 for (i = 0; i < gss->num_ip4_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001784 vty_dump_sns_ip4(vty, prefix, &gss->ip4_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001785 }
1786
1787 if (gss->num_ip6_local && gss->num_ip6_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001788 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001789 for (i = 0; i < gss->num_ip6_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001790 vty_dump_sns_ip6(vty, prefix, &gss->ip6_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001791
Harald Welte1262c4f2021-01-19 20:58:33 +01001792 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001793 for (i = 0; i < gss->num_ip6_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001794 vty_dump_sns_ip6(vty, prefix, &gss->ip6_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001795 }
1796}
1797
Alexander Couzens412bc342020-11-19 05:24:37 +01001798/*! write IP-SNS to a vty
1799 * \param[in] vty VTY to which the state shall be printed
1800 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001801void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001802{
1803 struct ns2_sns_state *gss;
1804 struct osmo_sockaddr_str addr_str;
1805 struct sns_endpoint *endpoint;
1806
1807 if (!nse->bss_sns_fi)
1808 return;
1809
1810 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1811 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001812 /* It's unlikely that an error happens, but let's better be safe. */
1813 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1814 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001815 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001816 }
1817}
1818
Alexander Couzense769f522020-12-07 07:37:07 +01001819static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1820 const struct osmo_sockaddr *saddr)
1821{
1822 struct sns_endpoint *endpoint;
1823
1824 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1825 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1826 return endpoint;
1827 }
1828
1829 return NULL;
1830}
1831
1832/*! gprs_ns2_sns_add_endpoint
1833 * \param[in] nse
1834 * \param[in] sockaddr
1835 * \return
1836 */
1837int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1838 const struct osmo_sockaddr *saddr)
1839{
1840 struct ns2_sns_state *gss;
1841 struct sns_endpoint *endpoint;
1842 bool do_selection = false;
1843
1844 if (nse->ll != GPRS_NS2_LL_UDP) {
1845 return -EINVAL;
1846 }
1847
Alexander Couzens138b96f2021-01-25 16:23:29 +01001848 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001849 return -EINVAL;
1850 }
1851
1852 gss = nse->bss_sns_fi->priv;
1853
1854 if (ns2_get_sns_endpoint(gss, saddr))
1855 return -EADDRINUSE;
1856
1857 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1858 if (!endpoint)
1859 return -ENOMEM;
1860
1861 endpoint->saddr = *saddr;
1862 if (llist_empty(&gss->sns_endpoints))
1863 do_selection = true;
1864
1865 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1866 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001867 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001868
1869 return 0;
1870}
1871
1872/*! gprs_ns2_sns_del_endpoint
1873 * \param[in] nse
1874 * \param[in] sockaddr
1875 * \return 0 on success, otherwise < 0
1876 */
1877int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1878 const struct osmo_sockaddr *saddr)
1879{
1880 struct ns2_sns_state *gss;
1881 struct sns_endpoint *endpoint;
1882
1883 if (nse->ll != GPRS_NS2_LL_UDP) {
1884 return -EINVAL;
1885 }
1886
Alexander Couzens138b96f2021-01-25 16:23:29 +01001887 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001888 return -EINVAL;
1889 }
1890
1891 gss = nse->bss_sns_fi->priv;
1892 endpoint = ns2_get_sns_endpoint(gss, saddr);
1893 if (!endpoint)
1894 return -ENOENT;
1895
1896 /* if this is an unused SNS endpoint it's done */
1897 if (gss->initial != endpoint) {
1898 llist_del(&endpoint->list);
1899 talloc_free(endpoint);
1900 return 0;
1901 }
1902
Alexander Couzens67725e22021-02-15 02:37:03 +01001903 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001904 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001905 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001906 "Closing all NS-VC and restart SNS-SIZE procedure"
1907 "with a remaining SNS endpoint.\n");
1908
1909 /* Continue with the next endpoint in the list.
1910 * Special case if the endpoint is at the start or end of the list */
1911 if (endpoint->list.prev == &gss->sns_endpoints ||
1912 endpoint->list.next == &gss->sns_endpoints)
1913 gss->initial = NULL;
1914 else
1915 gss->initial = llist_entry(endpoint->list.next->prev,
1916 struct sns_endpoint,
1917 list);
1918
1919 llist_del(&endpoint->list);
1920 gprs_ns2_free_nsvcs(nse);
1921 talloc_free(endpoint);
1922
1923 return 0;
1924}
1925
1926/*! gprs_ns2_sns_count
1927 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1928 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1929 */
1930int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1931{
1932 struct ns2_sns_state *gss;
1933 struct sns_endpoint *endpoint;
1934 int count = 0;
1935
1936 if (nse->ll != GPRS_NS2_LL_UDP) {
1937 return -EINVAL;
1938 }
1939
Alexander Couzens138b96f2021-01-25 16:23:29 +01001940 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001941 return -EINVAL;
1942 }
1943
1944 gss = nse->bss_sns_fi->priv;
1945 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1946 count++;
1947
1948 return count;
1949}
1950
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001951void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1952{
1953 struct ns2_sns_state *gss;
1954 struct gprs_ns2_vc *tmp;
1955
1956 if (!nse->bss_sns_fi)
1957 return;
1958
1959 gss = nse->bss_sns_fi->priv;
1960 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1961 return;
1962
1963 if (alive == gss->alive)
1964 return;
1965
1966 /* check if this is the current SNS NS-VC */
1967 if (nsvc == gss->sns_nsvc) {
1968 /* only replace the SNS NS-VC if there are other alive NS-VC.
1969 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1970 * and couldn't confirm yet if the NS-VC comes up */
1971 if (gss->alive && !alive)
1972 ns2_sns_replace_nsvc(nsvc);
1973 }
1974
1975 if (alive) {
1976 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001977 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001978 } else {
1979 /* is there at least another alive nsvc? */
1980 llist_for_each_entry(tmp, &nse->nsvc, list) {
1981 if (ns2_vc_is_unblocked(tmp))
1982 return;
1983 }
1984
1985 /* all NS-VC have failed */
1986 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001987 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001988 }
1989}
1990
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001991int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1992 struct gprs_ns2_vc_bind *bind)
1993{
1994 struct ns2_sns_state *gss;
1995 struct ns2_sns_bind *tmp;
1996
1997 OSMO_ASSERT(nse->bss_sns_fi);
1998 gss = nse->bss_sns_fi->priv;
1999
2000 if (!gprs_ns2_is_ip_bind(bind)) {
2001 return -EINVAL;
2002 }
2003
2004 if (!llist_empty(&gss->binds)) {
2005 llist_for_each_entry(tmp, &gss->binds, list) {
2006 if (tmp->bind == bind)
2007 return -EALREADY;
2008 }
2009 }
2010
2011 tmp = talloc_zero(gss, struct ns2_sns_bind);
2012 if (!tmp)
2013 return -ENOMEM;
2014 tmp->bind = bind;
2015 llist_add_tail(&tmp->list, &gss->binds);
2016
2017 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
2018 return 0;
2019}
2020
2021/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
2022int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
2023 struct gprs_ns2_vc_bind *bind)
2024{
2025 struct ns2_sns_state *gss;
2026 struct ns2_sns_bind *tmp, *tmp2;
2027 bool found = false;
2028
2029 if (!nse->bss_sns_fi)
2030 return -EINVAL;
2031
2032 gss = nse->bss_sns_fi->priv;
2033 if (gss->initial_bind && gss->initial_bind->bind == bind) {
2034 if (gss->initial_bind->list.prev == &gss->binds)
2035 gss->initial_bind = NULL;
2036 else
2037 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
2038 }
2039
2040 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
2041 if (tmp->bind == bind) {
2042 llist_del(&tmp->list);
2043 found = true;
Alexander Couzensa35c2962021-04-19 03:30:15 +02002044 break;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002045 }
2046 }
2047
2048 if (!found)
2049 return -ENOENT;
2050
2051 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
2052 return 0;
2053}
2054
Alexander Couzensc4704762021-02-08 23:13:12 +01002055/* Update SNS weights
2056 * \param[in] nsvc the NSVC which should be updated
2057 */
2058void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
2059{
2060 /* TODO: implement weights after binds per sns implemented */
2061}
2062
Harald Welte4f127462021-03-02 20:49:10 +01002063
2064
2065
2066/***********************************************************************
2067 * SGSN role
2068 ***********************************************************************/
2069
2070static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2071{
2072 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2073 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2074 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
2075}
2076
2077/* We're waiting for inbound SNS-CONFIG from the BSS */
2078static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2079{
2080 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2081 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2082 struct gprs_ns2_inst *nsi = nse->nsi;
2083 uint8_t cause;
2084 int rc;
2085
2086 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2087
2088 switch (event) {
2089 case GPRS_SNS_EV_RX_CONFIG:
2090 case GPRS_SNS_EV_RX_CONFIG_END:
2091 rc = ns_sns_append_remote_eps(fi, data);
2092 if (rc < 0) {
2093 cause = -rc;
2094 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2095 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2096 return;
2097 }
2098 /* only change state if last CONFIG was received */
2099 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
2100 /* ensure sum of data weight / sig weights is > 0 */
2101 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
2102 cause = NS_CAUSE_INVAL_WEIGH;
2103 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2104 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2105 break;
2106 }
2107 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2108 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2109 } else {
2110 /* just send CONFIG-ACK */
2111 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2112 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2113 }
2114 break;
2115 }
2116}
2117
2118static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2119{
2120 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2121 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2122
Harald Welte4f127462021-03-02 20:49:10 +01002123 /* transmit SGSN-oriented SNS-CONFIG */
2124 ns2_tx_sns_config(gss->sns_nsvc, true, gss->ip4_local, gss->num_ip4_local,
2125 gss->ip6_local, gss->num_ip6_local);
2126}
2127
2128/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2129static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2130{
2131 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2132 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2133 struct tlv_parsed *tp = NULL;
2134
2135 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2136
2137 switch (event) {
2138 case GPRS_SNS_EV_RX_CONFIG_ACK:
2139 tp = data;
2140 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2141 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2142 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2143 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2144 break;
2145 }
2146 /* we currently only send one SNS-CONFIG with END FLAG */
2147 if (true) {
2148 create_missing_nsvcs(fi);
2149 /* start the test procedure on ALL NSVCs! */
2150 gprs_ns2_start_alive_all_nsvcs(nse);
2151 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2152 }
2153 break;
2154 }
2155}
2156
2157/* SGSN-side SNS state machine */
2158static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2159 [GPRS_SNS_ST_UNCONFIGURED] = {
2160 .in_event_mask = 0, /* handled by all_state_action */
2161 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2162 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2163 .name = "UNCONFIGURED",
2164 .action = ns2_sns_st_sgsn_unconfigured,
2165 },
2166 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
2167 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
2168 S(GPRS_SNS_EV_RX_CONFIG_END),
2169 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2170 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2171 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2172 .name = "SGSN_WAIT_CONFIG",
2173 .action = ns2_sns_st_sgsn_wait_config,
2174 },
2175 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
2176 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
2177 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2178 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2179 S(GPRS_SNS_ST_CONFIGURED),
2180 .name = "SGSN_WAIT_CONFIG_ACK",
2181 .action = ns2_sns_st_sgsn_wait_config_ack,
2182 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2183 },
2184 [GPRS_SNS_ST_CONFIGURED] = {
2185 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
2186 S(GPRS_SNS_EV_RX_DELETE) |
2187 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
2188 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
2189 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2190 .name = "CONFIGURED",
2191 /* shared with BSS side; once configured there's no difference */
2192 .action = ns2_sns_st_configured,
2193 .onenter = ns2_sns_st_configured_onenter,
2194 },
2195};
2196
2197static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2198{
2199 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2200 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2201 struct gprs_ns2_inst *nsi = nse->nsi;
2202
2203 gss->N++;
2204 switch (fi->T) {
2205 case 3:
2206 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2207 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2208 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2209 } else {
2210 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2211 }
2212 break;
2213 case 4:
2214 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2215 break;
2216 }
2217 return 0;
2218}
2219
2220
2221/* allstate-action for SGSN role */
2222static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2223{
2224 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2225 struct tlv_parsed *tp = NULL;
Harald Welte01fa6a32021-03-04 19:49:38 +01002226 size_t num_local_eps, num_remote_eps;
Harald Welte4f127462021-03-02 20:49:10 +01002227 uint8_t flag;
Harald Weltea2c5af52021-03-04 17:59:35 +01002228 uint8_t cause;
Harald Welte4f127462021-03-02 20:49:10 +01002229
2230 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2231
2232 switch (event) {
2233 case GPRS_SNS_EV_RX_SIZE:
2234 tp = (struct tlv_parsed *) data;
Harald Weltea2c5af52021-03-04 17:59:35 +01002235 /* check for mandatory / conditional IEs */
2236 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) ||
2237 !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) {
2238 cause = NS_CAUSE_MISSING_ESSENT_IE;
2239 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2240 break;
2241 }
2242 if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) &&
2243 !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) {
2244 cause = NS_CAUSE_MISSING_ESSENT_IE;
Harald Welte4f127462021-03-02 20:49:10 +01002245 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2246 break;
2247 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002248 if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2))
2249 gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR);
2250 if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2))
2251 gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR);
2252 /* decide if we go for IPv4 or IPv6 */
2253 if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, IPv6)) {
2254 gss->ip = IPv6;
Harald Welte2d807b62021-03-24 01:57:30 +01002255 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte01fa6a32021-03-04 19:49:38 +01002256 num_local_eps = gss->num_ip6_local;
2257 num_remote_eps = gss->num_max_ip6_remote;
2258 } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, IPv4)) {
2259 gss->ip = IPv4;
Harald Welte2d807b62021-03-24 01:57:30 +01002260 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte01fa6a32021-03-04 19:49:38 +01002261 num_local_eps = gss->num_ip4_local;
2262 num_remote_eps = gss->num_max_ip4_remote;
2263 } else {
2264 if (gss->num_ip4_local && !gss->num_max_ip4_remote)
2265 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
2266 else
2267 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
2268 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2269 break;
2270 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002271 /* ensure number of NS-VCs is sufficient for full mesh */
2272 gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC);
2273 if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) {
2274 LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, "
2275 "but BSS supports only %zu maximum NS-VCs\n", num_local_eps,
2276 num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs);
2277 cause = NS_CAUSE_INVAL_NR_NS_VC;
2278 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2279 break;
2280 }
2281 /* perform state reset, if requested */
Harald Welte4f127462021-03-02 20:49:10 +01002282 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2283 if (flag & 1) {
2284 struct gprs_ns2_vc *nsvc, *nsvc2;
2285 /* clear all state */
Harald Welte46eb7642021-03-04 17:49:59 +01002286 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
Harald Welte4f127462021-03-02 20:49:10 +01002287 gss->N = 0;
Harald Welte46eb7642021-03-04 17:49:59 +01002288 ns2_clear_ipv46_entries_local(gss);
2289 ns2_clear_ipv46_entries_remote(gss);
Harald Welte4f127462021-03-02 20:49:10 +01002290 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2291 if (nsvc == gss->sns_nsvc) {
2292 /* keep the NSVC we need for SNS, but unconfigure it */
2293 nsvc->sig_weight = 0;
2294 nsvc->data_weight = 0;
2295 ns2_vc_force_unconfigured(nsvc);
2296 } else {
2297 /* free all other NS-VCs */
2298 gprs_ns2_free_nsvc(nsvc);
2299 }
2300 }
Harald Welte2d807b62021-03-24 01:57:30 +01002301 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte4f127462021-03-02 20:49:10 +01002302 }
2303 /* send SIZE_ACK */
2304 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2305 /* only wait for SNS-CONFIG in case of Reset flag */
2306 if (flag & 1)
2307 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2308 break;
2309 default:
2310 ns2_sns_st_all_action(fi, event, data);
2311 break;
2312 }
2313}
2314
2315static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2316 .name = "GPRS-NS2-SNS-SGSN",
2317 .states = ns2_sns_sgsn_states,
2318 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
2319 .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) |
2320 S(GPRS_SNS_EV_REQ_NO_NSVC) |
2321 S(GPRS_SNS_EV_REQ_ADD_BIND) |
2322 S(GPRS_SNS_EV_REQ_DELETE_BIND),
2323 .allstate_action = ns2_sns_st_all_action_sgsn,
2324 .cleanup = NULL,
2325 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2326 .event_names = gprs_sns_event_names,
2327 .pre_term = NULL,
2328 .log_subsys = DLNS,
2329};
2330
2331/*! Allocate an IP-SNS FSM for the SGSN side.
2332 * \param[in] nse NS Entity in which the FSM runs
2333 * \param[in] id string identifier
2334 * \returns FSM instance on success; NULL on error */
2335struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2336{
2337 struct osmo_fsm_inst *fi;
2338 struct ns2_sns_state *gss;
2339
2340 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2341 if (!fi)
2342 return fi;
2343
2344 gss = talloc_zero(fi, struct ns2_sns_state);
2345 if (!gss)
2346 goto err;
2347
2348 fi->priv = gss;
2349 gss->nse = nse;
2350 gss->role = GPRS_SNS_ROLE_SGSN;
2351 INIT_LLIST_HEAD(&gss->sns_endpoints);
2352 INIT_LLIST_HEAD(&gss->binds);
2353
2354 return fi;
2355err:
2356 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2357 return NULL;
2358}
2359
2360
2361
2362
Alexander Couzens6a161492020-07-12 13:45:50 +02002363/* initialize osmo_ctx on main tread */
2364static __attribute__((constructor)) void on_dso_load_ctx(void)
2365{
2366 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002367 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002368}