blob: 11c5a8ea56dc48e8d4d5ff9d43f2a88020ed6bbf [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \file gprs_ns2_sns.c
2 * NS Sub-Network Service Protocol implementation
3 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
4 * as well as its successor 3GPP TS 48.016 */
5
Harald Weltec1c7e4a2021-03-02 20:47:29 +01006/* (C) 2018-2021 by Harald Welte <laforge@gnumonks.org>
Alexander Couzens6a161492020-07-12 13:45:50 +02007 * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
8 * Author: Alexander Couzens <lynxis@fe80.eu>
9 *
10 * All Rights Reserved
11 *
12 * SPDX-License-Identifier: GPL-2.0+
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 *
27 */
28
29/* The BSS NSE only has one SGSN IP address configured, and it will use the SNS procedures
30 * to communicated its local IPs/ports as well as all the SGSN side IPs/ports and
31 * associated weights. The BSS then uses this to establish a full mesh
32 * of NSVCs between all BSS-side IPs/ports and SGSN-side IPs/ports.
33 *
34 * Known limitation/expectation/bugs:
35 * - No concurrent dual stack. It supports either IPv4 or IPv6, but not both at the same time.
36 * - SNS Add/Change/Delete: Doesn't answer on the same NSVC as received SNS ADD/CHANGE/DELETE PDUs.
37 * - SNS Add/Change/Delete: Doesn't communicated the failed IPv4/IPv6 entries on the SNS_ACK.
38 */
39
40#include <errno.h>
41
42#include <netinet/in.h>
43#include <arpa/inet.h>
44#include <stdint.h>
45
46#include <osmocom/core/fsm.h>
47#include <osmocom/core/msgb.h>
48#include <osmocom/core/socket.h>
Alexander Couzens412bc342020-11-19 05:24:37 +010049#include <osmocom/core/sockaddr_str.h>
Alexander Couzens6a161492020-07-12 13:45:50 +020050#include <osmocom/gsm/tlv.h>
51#include <osmocom/gprs/gprs_msgb.h>
52#include <osmocom/gprs/gprs_ns2.h>
53#include <osmocom/gprs/protocol/gsm_08_16.h>
54
55#include "gprs_ns2_internal.h"
56
57#define S(x) (1 << (x))
58
59enum ns2_sns_type {
60 IPv4,
61 IPv6,
62};
63
Harald Welte4f127462021-03-02 20:49:10 +010064enum ns2_sns_role {
65 GPRS_SNS_ROLE_BSS,
66 GPRS_SNS_ROLE_SGSN,
67};
68
Harald Welte694dad52021-03-23 15:22:16 +010069/* BSS-side-only states _ST_BSS_; SGSN-side only states _ST_SGSN_; others shared */
Alexander Couzens6a161492020-07-12 13:45:50 +020070enum gprs_sns_bss_state {
71 GPRS_SNS_ST_UNCONFIGURED,
Harald Welte694dad52021-03-23 15:22:16 +010072 GPRS_SNS_ST_BSS_SIZE, /*!< SNS-SIZE procedure ongoing */
73 GPRS_SNS_ST_BSS_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */
74 GPRS_SNS_ST_BSS_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */
Alexander Couzens6a161492020-07-12 13:45:50 +020075 GPRS_SNS_ST_CONFIGURED,
Harald Welte4f127462021-03-02 20:49:10 +010076 GPRS_SNS_ST_SGSN_WAIT_CONFIG, /* !< SGSN role: Wait for CONFIG from BSS */
77 GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, /* !< SGSN role: Wait for CONFIG-ACK from BSS */
Alexander Couzens6a161492020-07-12 13:45:50 +020078};
79
80enum gprs_sns_event {
Alexander Couzens67725e22021-02-15 02:37:03 +010081 GPRS_SNS_EV_REQ_SELECT_ENDPOINT, /*!< Select a SNS endpoint from the list */
82 GPRS_SNS_EV_RX_SIZE,
83 GPRS_SNS_EV_RX_SIZE_ACK,
84 GPRS_SNS_EV_RX_CONFIG,
85 GPRS_SNS_EV_RX_CONFIG_END, /*!< SNS-CONFIG with end flag received */
86 GPRS_SNS_EV_RX_CONFIG_ACK,
87 GPRS_SNS_EV_RX_ADD,
88 GPRS_SNS_EV_RX_DELETE,
89 GPRS_SNS_EV_RX_CHANGE_WEIGHT,
Harald Welteb9f23872021-03-02 20:48:54 +010090 GPRS_SNS_EV_RX_ACK, /*!< Rx of SNS-ACK (response to ADD/DELETE/CHG_WEIGHT */
Harald Welte04647e12021-03-02 18:50:40 +010091 GPRS_SNS_EV_REQ_NO_NSVC, /*!< no more NS-VC remaining (all dead) */
Alexander Couzens67725e22021-02-15 02:37:03 +010092 GPRS_SNS_EV_REQ_NSVC_ALIVE, /*!< a NS-VC became alive */
Harald Welte04647e12021-03-02 18:50:40 +010093 GPRS_SNS_EV_REQ_ADD_BIND, /*!< add a new local bind to this NSE */
94 GPRS_SNS_EV_REQ_DELETE_BIND, /*!< remove a local bind from this NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +020095};
96
97static const struct value_string gprs_sns_event_names[] = {
Alexander Couzens67725e22021-02-15 02:37:03 +010098 { GPRS_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" },
99 { GPRS_SNS_EV_RX_SIZE, "RX_SIZE" },
100 { GPRS_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" },
101 { GPRS_SNS_EV_RX_CONFIG, "RX_CONFIG" },
102 { GPRS_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" },
103 { GPRS_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" },
104 { GPRS_SNS_EV_RX_ADD, "RX_ADD" },
105 { GPRS_SNS_EV_RX_DELETE, "RX_DELETE" },
Harald Welteb9f23872021-03-02 20:48:54 +0100106 { GPRS_SNS_EV_RX_ACK, "RX_ACK" },
Alexander Couzens67725e22021-02-15 02:37:03 +0100107 { GPRS_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" },
108 { GPRS_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" },
109 { GPRS_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"},
110 { GPRS_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"},
111 { GPRS_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200112 { 0, NULL }
113};
114
Alexander Couzense769f522020-12-07 07:37:07 +0100115struct sns_endpoint {
116 struct llist_head list;
117 struct osmo_sockaddr saddr;
118};
119
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100120struct ns2_sns_bind {
121 struct llist_head list;
122 struct gprs_ns2_vc_bind *bind;
123};
124
Alexander Couzens6a161492020-07-12 13:45:50 +0200125struct ns2_sns_state {
126 struct gprs_ns2_nse *nse;
127
128 enum ns2_sns_type ip;
Harald Welte4f127462021-03-02 20:49:10 +0100129 enum ns2_sns_role role; /* local role: BSS or SGSN */
Alexander Couzens6a161492020-07-12 13:45:50 +0200130
Alexander Couzense769f522020-12-07 07:37:07 +0100131 /* holds the list of initial SNS endpoints */
132 struct llist_head sns_endpoints;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100133 /* list of used struct ns2_sns_bind */
134 struct llist_head binds;
135 /* pointer to the bind which was used to initiate the SNS connection */
136 struct ns2_sns_bind *initial_bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100137 /* prevent recursive reselection */
138 bool reselection_running;
139
140 /* The current initial SNS endpoints.
141 * The initial connection will be moved into the NSE
142 * if configured via SNS. Otherwise it will be removed
143 * in configured state. */
144 struct sns_endpoint *initial;
Alexander Couzens6a161492020-07-12 13:45:50 +0200145 /* all SNS PDU will be sent over this nsvc */
146 struct gprs_ns2_vc *sns_nsvc;
Alexander Couzens90ee9632020-12-07 06:18:32 +0100147 /* timer N */
148 int N;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100149 /* true if at least one nsvc is alive */
150 bool alive;
Alexander Couzens6a161492020-07-12 13:45:50 +0200151
152 /* local configuration to send to the remote end */
153 struct gprs_ns_ie_ip4_elem *ip4_local;
154 size_t num_ip4_local;
155
156 /* local configuration to send to the remote end */
157 struct gprs_ns_ie_ip6_elem *ip6_local;
158 size_t num_ip6_local;
159
160 /* local configuration about our capabilities in terms of connections to
161 * remote (SGSN) side */
162 size_t num_max_nsvcs;
163 size_t num_max_ip4_remote;
164 size_t num_max_ip6_remote;
165
166 /* remote configuration as received */
167 struct gprs_ns_ie_ip4_elem *ip4_remote;
168 unsigned int num_ip4_remote;
169
170 /* remote configuration as received */
171 struct gprs_ns_ie_ip6_elem *ip6_remote;
172 unsigned int num_ip6_remote;
173};
174
175static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi)
176{
177 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
178 return gss->nse;
179}
180
181/* helper function to compute the sum of all (data or signaling) weights */
182static int ip4_weight_sum(const struct gprs_ns_ie_ip4_elem *ip4, unsigned int num,
183 bool data_weight)
184{
185 unsigned int i;
186 int weight_sum = 0;
187
188 for (i = 0; i < num; i++) {
189 if (data_weight)
190 weight_sum += ip4[i].data_weight;
191 else
192 weight_sum += ip4[i].sig_weight;
193 }
194 return weight_sum;
195}
196#define ip4_weight_sum_data(x,y) ip4_weight_sum(x, y, true)
197#define ip4_weight_sum_sig(x,y) ip4_weight_sum(x, y, false)
198
199/* helper function to compute the sum of all (data or signaling) weights */
200static int ip6_weight_sum(const struct gprs_ns_ie_ip6_elem *ip6, unsigned int num,
201 bool data_weight)
202{
203 unsigned int i;
204 int weight_sum = 0;
205
206 for (i = 0; i < num; i++) {
207 if (data_weight)
208 weight_sum += ip6[i].data_weight;
209 else
210 weight_sum += ip6[i].sig_weight;
211 }
212 return weight_sum;
213}
214#define ip6_weight_sum_data(x,y) ip6_weight_sum(x, y, true)
215#define ip6_weight_sum_sig(x,y) ip6_weight_sum(x, y, false)
216
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100217static int nss_weight_sum(const struct ns2_sns_state *nss, bool data_weight)
218{
219 return ip4_weight_sum(nss->ip4_remote, nss->num_ip4_remote, data_weight) +
220 ip6_weight_sum(nss->ip6_remote, nss->num_ip6_remote, data_weight);
221}
222#define nss_weight_sum_data(nss) nss_weight_sum(nss, true)
223#define nss_weight_sum_sig(nss) nss_weight_sum(nss, false)
224
Alexander Couzens6a161492020-07-12 13:45:50 +0200225static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
226 const struct gprs_ns_ie_ip4_elem *ip4)
227{
228 struct osmo_sockaddr sa;
229 /* copy over. Both data structures use network byte order */
230 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
231 sa.u.sin.sin_port = ip4->udp_port;
232 sa.u.sin.sin_family = AF_INET;
233
Alexander Couzens38b19e82020-09-23 23:56:37 +0200234 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200235}
236
237static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
238 const struct gprs_ns_ie_ip6_elem *ip6)
239{
240 struct osmo_sockaddr sa;
241 /* copy over. Both data structures use network byte order */
242 sa.u.sin6.sin6_addr = ip6->ip_addr;
243 sa.u.sin6.sin6_port = ip6->udp_port;
244 sa.u.sin6.sin6_family = AF_INET;
245
Alexander Couzens38b19e82020-09-23 23:56:37 +0200246 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200247}
248
Alexander Couzens125298f2020-10-11 21:22:42 +0200249/*! Return the initial SNS remote socket address
250 * \param nse NS Entity
251 * \return address of the initial SNS connection; NULL in case of error
252 */
253const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
254{
255 struct ns2_sns_state *gss;
256
257 if (!nse->bss_sns_fi)
258 return NULL;
259
260 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100261 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200262}
263
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100264/*! called when a nsvc is beeing freed or the nsvc became dead */
265void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200266{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100267 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200268 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100269 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200270 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200271
272 if (!fi)
273 return;
274
275 gss = (struct ns2_sns_state *) fi->priv;
276 if (nsvc != gss->sns_nsvc)
277 return;
278
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100279 gss->sns_nsvc = NULL;
280 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200281 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100282 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200283 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100284 return;
285 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200286 }
287 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100288 /* the SNS is waiting for its first NS-VC to come up
289 * choose any other nsvc */
290 llist_for_each_entry(tmp, &nse->nsvc, list) {
291 if (nsvc != tmp) {
292 gss->sns_nsvc = tmp;
293 return;
294 }
295 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200296 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100297
Alexander Couzens67725e22021-02-15 02:37:03 +0100298 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200299}
300
Harald Welte46eb7642021-03-04 17:49:59 +0100301static void ns2_clear_ipv46_entries_local(struct ns2_sns_state *gss)
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100302{
303 TALLOC_FREE(gss->ip4_local);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100304 TALLOC_FREE(gss->ip6_local);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100305
306 gss->num_ip4_local = 0;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100307 gss->num_ip6_local = 0;
Harald Welte46eb7642021-03-04 17:49:59 +0100308}
309
310static void ns2_clear_ipv46_entries_remote(struct ns2_sns_state *gss)
311{
312 TALLOC_FREE(gss->ip4_remote);
313 TALLOC_FREE(gss->ip6_remote);
314
315 gss->num_ip4_remote = 0;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100316 gss->num_ip6_remote = 0;
317}
318
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100319static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
320 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200321{
322 struct gprs_ns2_inst *nsi = nse->nsi;
323 struct gprs_ns2_vc *nsvc;
324 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100325
326 /* for every bind, create a connection if bind type == IP */
327 llist_for_each_entry(bind, &nsi->binding, list) {
328 if (bind->ll != GPRS_NS2_LL_UDP)
329 continue;
330 /* ignore failed connection */
331 nsvc = gprs_ns2_ip_connect_inactive(bind,
332 remote,
333 nse, 0);
334 if (!nsvc) {
335 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
336 continue;
337 }
338
339 nsvc->sig_weight = sig_weight;
340 nsvc->data_weight = data_weight;
341 }
342}
343
344static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
345 struct gprs_ns2_nse *nse,
346 const struct gprs_ns_ie_ip4_elem *ip4)
347{
Alexander Couzensc068d862020-10-12 04:11:51 +0200348 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200349 /* copy over. Both data structures use network byte order */
350 remote.u.sin.sin_family = AF_INET;
351 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
352 remote.u.sin.sin_port = ip4->udp_port;
353
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100354 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200355}
356
357static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
358 struct gprs_ns2_nse *nse,
359 const struct gprs_ns_ie_ip6_elem *ip6)
360{
Alexander Couzens6a161492020-07-12 13:45:50 +0200361 struct osmo_sockaddr remote = {};
362 /* copy over. Both data structures use network byte order */
363 remote.u.sin6.sin6_family = AF_INET6;
364 remote.u.sin6.sin6_addr = ip6->ip_addr;
365 remote.u.sin6.sin6_port = ip6->udp_port;
366
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100367 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200368}
369
370
371static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
372{
373 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
374 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
375 struct gprs_ns2_vc *nsvc;
376 struct gprs_ns2_vc_bind *bind;
377 struct osmo_sockaddr remote = { };
378 unsigned int i;
379
380 for (i = 0; i < gss->num_ip4_remote; i++) {
381 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->ip4_remote[i];
382
383 remote.u.sin.sin_family = AF_INET;
384 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
385 remote.u.sin.sin_port = ip4->udp_port;
386
387 llist_for_each_entry(bind, &nse->nsi->binding, list) {
388 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100389 if (bind->ll != GPRS_NS2_LL_UDP)
390 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200391
392 llist_for_each_entry(nsvc, &nse->nsvc, list) {
393 if (nsvc->bind != bind)
394 continue;
395
Alexander Couzensc4229a42020-10-11 20:58:04 +0200396 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200397 found = true;
398 break;
399 }
400 }
401
402 if (!found) {
403 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
404 if (!nsvc) {
405 /* TODO: add to a list to send back a NS-STATUS */
406 continue;
407 }
408 }
409
410 /* update data / signalling weight */
411 nsvc->data_weight = ip4->data_weight;
412 nsvc->sig_weight = ip4->sig_weight;
413 nsvc->sns_only = false;
414 }
415 }
416
417 for (i = 0; i < gss->num_ip6_remote; i++) {
418 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->ip6_remote[i];
419
420 remote.u.sin6.sin6_family = AF_INET6;
421 remote.u.sin6.sin6_addr = ip6->ip_addr;
422 remote.u.sin6.sin6_port = ip6->udp_port;
423
424 llist_for_each_entry(bind, &nse->nsi->binding, list) {
425 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100426 if (bind->ll != GPRS_NS2_LL_UDP)
427 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200428
429 llist_for_each_entry(nsvc, &nse->nsvc, list) {
430 if (nsvc->bind != bind)
431 continue;
432
Alexander Couzensc4229a42020-10-11 20:58:04 +0200433 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200434 found = true;
435 break;
436 }
437 }
438
439 if (!found) {
440 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
441 if (!nsvc) {
442 /* TODO: add to a list to send back a NS-STATUS */
443 continue;
444 }
445 }
446
447 /* update data / signalling weight */
448 nsvc->data_weight = ip6->data_weight;
449 nsvc->sig_weight = ip6->sig_weight;
450 nsvc->sns_only = false;
451 }
452 }
453
454
455 return 0;
456}
457
458/* Add a given remote IPv4 element to gprs_sns_state */
459static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
460{
461 unsigned int i;
462
463 if (gss->num_ip4_remote >= gss->num_max_ip4_remote)
464 return -NS_CAUSE_INVAL_NR_NS_VC;
465
466 /* check for duplicates */
467 for (i = 0; i < gss->num_ip4_remote; i++) {
468 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
469 continue;
470 /* TODO: log message duplicate */
Alexander Couzens6a161492020-07-12 13:45:50 +0200471 return -NS_CAUSE_PROTO_ERR_UNSPEC;
472 }
473
474 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem,
475 gss->num_ip4_remote+1);
476 gss->ip4_remote[gss->num_ip4_remote] = *ip4;
477 gss->num_ip4_remote += 1;
478 return 0;
479}
480
481/* Remove a given remote IPv4 element from gprs_sns_state */
482static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
483{
484 unsigned int i;
485
486 for (i = 0; i < gss->num_ip4_remote; i++) {
487 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
488 continue;
489 /* all array elements < i remain as they are; all > i are shifted left by one */
490 memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1);
491 gss->num_ip4_remote -= 1;
492 return 0;
493 }
494 return -1;
495}
496
497/* update the weights for specified remote IPv4 */
498static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
499{
500 unsigned int i;
501
502 for (i = 0; i < gss->num_ip4_remote; i++) {
503 if (gss->ip4_remote[i].ip_addr != ip4->ip_addr ||
504 gss->ip4_remote[i].udp_port != ip4->udp_port)
505 continue;
506
507 gss->ip4_remote[i].sig_weight = ip4->sig_weight;
508 gss->ip4_remote[i].data_weight = ip4->data_weight;
509 return 0;
510 }
511 return -1;
512}
513
514/* Add a given remote IPv6 element to gprs_sns_state */
515static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
516{
517 if (gss->num_ip6_remote >= gss->num_max_ip6_remote)
518 return -NS_CAUSE_INVAL_NR_NS_VC;
519
520 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, struct gprs_ns_ie_ip6_elem,
521 gss->num_ip6_remote+1);
522 gss->ip6_remote[gss->num_ip6_remote] = *ip6;
523 gss->num_ip6_remote += 1;
524 return 0;
525}
526
527/* Remove a given remote IPv6 element from gprs_sns_state */
528static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
529{
530 unsigned int i;
531
532 for (i = 0; i < gss->num_ip6_remote; i++) {
533 if (memcmp(&gss->ip6_remote[i], ip6, sizeof(*ip6)))
534 continue;
535 /* all array elements < i remain as they are; all > i are shifted left by one */
536 memmove(&gss->ip6_remote[i], &gss->ip6_remote[i+1], gss->num_ip6_remote-i-1);
537 gss->num_ip6_remote -= 1;
538 return 0;
539 }
540 return -1;
541}
542
543/* update the weights for specified remote IPv6 */
544static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
545{
546 unsigned int i;
547
548 for (i = 0; i < gss->num_ip6_remote; i++) {
549 if (memcmp(&gss->ip6_remote[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
550 gss->ip6_remote[i].udp_port != ip6->udp_port)
551 continue;
552 gss->ip6_remote[i].sig_weight = ip6->sig_weight;
553 gss->ip6_remote[i].data_weight = ip6->data_weight;
554 return 0;
555 }
556 return -1;
557}
558
559static int do_sns_change_weight(struct osmo_fsm_inst *fi, const struct gprs_ns_ie_ip4_elem *ip4, const struct gprs_ns_ie_ip6_elem *ip6)
560{
561 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
562 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
563 struct gprs_ns2_vc *nsvc;
564 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200565 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200566 uint8_t new_signal;
567 uint8_t new_data;
568
569 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
570 * signalling weights of all the peer IP endpoints configured for this NSE is
571 * equal to zero or if the resulting sum of the data weights of all the peer IP
572 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
573 * SNS-ACK PDU with a cause code of "Invalid weights". */
574
575 if (ip4) {
576 if (update_remote_ip4_elem(gss, ip4))
577 return -NS_CAUSE_UNKN_IP_EP;
578
579 /* copy over. Both data structures use network byte order */
580 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
581 sa.u.sin.sin_port = ip4->udp_port;
582 sa.u.sin.sin_family = AF_INET;
583 new_signal = ip4->sig_weight;
584 new_data = ip4->data_weight;
585 } else if (ip6) {
586 if (update_remote_ip6_elem(gss, ip6))
587 return -NS_CAUSE_UNKN_IP_EP;
588
589 /* copy over. Both data structures use network byte order */
590 sa.u.sin6.sin6_addr = ip6->ip_addr;
591 sa.u.sin6.sin6_port = ip6->udp_port;
592 sa.u.sin6.sin6_family = AF_INET6;
593 new_signal = ip6->sig_weight;
594 new_data = ip6->data_weight;
595 } else {
596 OSMO_ASSERT(false);
597 }
598
599 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200600 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200601 /* all nsvc in NSE should be IP/UDP nsvc */
602 OSMO_ASSERT(remote);
603
604 if (osmo_sockaddr_cmp(&sa, remote))
605 continue;
606
607 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
608 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
609 nsvc->sig_weight, new_signal);
610
611 nsvc->data_weight = new_data;
612 nsvc->sig_weight = new_signal;
613 }
614
615 return 0;
616}
617
618static int do_sns_delete(struct osmo_fsm_inst *fi,
619 const struct gprs_ns_ie_ip4_elem *ip4,
620 const struct gprs_ns_ie_ip6_elem *ip6)
621{
622 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
623 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
624 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200625 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200626 struct osmo_sockaddr sa = {};
627
628 if (ip4) {
629 if (remove_remote_ip4_elem(gss, ip4) < 0)
630 return -NS_CAUSE_UNKN_IP_EP;
631 /* copy over. Both data structures use network byte order */
632 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
633 sa.u.sin.sin_port = ip4->udp_port;
634 sa.u.sin.sin_family = AF_INET;
635 } else if (ip6) {
636 if (remove_remote_ip6_elem(gss, ip6))
637 return -NS_CAUSE_UNKN_IP_EP;
638
639 /* copy over. Both data structures use network byte order */
640 sa.u.sin6.sin6_addr = ip6->ip_addr;
641 sa.u.sin6.sin6_port = ip6->udp_port;
642 sa.u.sin6.sin6_family = AF_INET6;
643 } else {
644 OSMO_ASSERT(false);
645 }
646
647 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200648 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200649 /* all nsvc in NSE should be IP/UDP nsvc */
650 OSMO_ASSERT(remote);
651 if (osmo_sockaddr_cmp(&sa, remote))
652 continue;
653
654 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
655 gprs_ns2_free_nsvc(nsvc);
656 }
657
658 return 0;
659}
660
661static int do_sns_add(struct osmo_fsm_inst *fi,
662 const struct gprs_ns_ie_ip4_elem *ip4,
663 const struct gprs_ns_ie_ip6_elem *ip6)
664{
665 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
666 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
667 struct gprs_ns2_vc *nsvc;
668 int rc = 0;
669
670 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
671 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
672 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
673 switch (gss->ip) {
674 case IPv4:
675 rc = add_remote_ip4_elem(gss, ip4);
676 break;
677 case IPv6:
678 rc = add_remote_ip6_elem(gss, ip6);
679 break;
680 default:
681 /* the gss->ip is initialized with the bss */
682 OSMO_ASSERT(false);
683 }
684
685 if (rc)
686 return rc;
687
688 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
689 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
690 * unspecified" */
691 switch (gss->ip) {
692 case IPv4:
693 nsvc = nsvc_by_ip4_elem(nse, ip4);
694 if (nsvc) {
695 /* the nsvc should be already in sync with the ip4 / ip6 elements */
696 return -NS_CAUSE_PROTO_ERR_UNSPEC;
697 }
698
699 /* TODO: failure case */
700 ns2_nsvc_create_ip4(fi, nse, ip4);
701 break;
702 case IPv6:
703 nsvc = nsvc_by_ip6_elem(nse, ip6);
704 if (nsvc) {
705 /* the nsvc should be already in sync with the ip4 / ip6 elements */
706 return -NS_CAUSE_PROTO_ERR_UNSPEC;
707 }
708
709 /* TODO: failure case */
710 ns2_nsvc_create_ip6(fi, nse, ip6);
711 break;
712 }
713
714 gprs_ns2_start_alive_all_nsvcs(nse);
715
716 return 0;
717}
718
719
Harald Welte694dad52021-03-23 15:22:16 +0100720static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200721{
Harald Weltef61a9152021-03-02 22:20:17 +0100722 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
723 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzense769f522020-12-07 07:37:07 +0100724 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200725}
726
Harald Welte694dad52021-03-23 15:22:16 +0100727static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200728{
Harald Weltef61a9152021-03-02 22:20:17 +0100729 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200730 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
731 struct gprs_ns2_inst *nsi = nse->nsi;
732 struct tlv_parsed *tp = NULL;
733
Harald Weltef61a9152021-03-02 22:20:17 +0100734 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
735
Alexander Couzens6a161492020-07-12 13:45:50 +0200736 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100737 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200738 tp = data;
739 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
740 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
741 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
742 /* TODO: What to do? */
743 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100744 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200745 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
746 }
747 break;
748 default:
749 OSMO_ASSERT(0);
750 }
751}
752
Harald Welte01fa6a32021-03-04 19:49:38 +0100753static int ns2_sns_count_num_local_ep(struct osmo_fsm_inst *fi, enum ns2_sns_type stype)
754{
755 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
756 struct ns2_sns_bind *sbind;
757 int count = 0;
758
759 llist_for_each_entry(sbind, &gss->binds, list) {
760 const struct osmo_sockaddr *sa = gprs_ns2_ip_bind_sockaddr(sbind->bind);
761 if (!sa)
762 continue;
763
764 switch (stype) {
765 case IPv4:
766 if (sa->u.sas.ss_family == AF_INET)
767 count++;
768 break;
769 case IPv6:
770 if (sa->u.sas.ss_family == AF_INET6)
771 count++;
772 break;
773 }
774 }
775 return count;
776}
777
Harald Welte24920e22021-03-04 13:03:27 +0100778static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200779{
780 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100781 struct gprs_ns_ie_ip4_elem *ip4_elems;
782 struct gprs_ns_ie_ip6_elem *ip6_elems;
783 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100784 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100785 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100786 const struct osmo_sockaddr *sa;
787 struct osmo_sockaddr local;
788 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200789
Harald Welte46eb7642021-03-04 17:49:59 +0100790 ns2_clear_ipv46_entries_local(gss);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100791
Alexander Couzense769f522020-12-07 07:37:07 +0100792 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100793 if (gss->role == GPRS_SNS_ROLE_BSS) {
794 if (!gss->initial)
795 return;
796 remote = &gss->initial->saddr;
797 } else
798 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100799
800 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100801 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100802 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100803 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100804 return;
805 }
806
Alexander Couzense769f522020-12-07 07:37:07 +0100807 switch (gss->ip) {
808 case IPv4:
809 ip4_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip4_elem) * count);
810 if (!ip4_elems)
811 return;
812
813 gss->ip4_local = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100814 llist_for_each_entry(sbind, &gss->binds, list) {
815 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100816 sa = gprs_ns2_ip_bind_sockaddr(bind);
817 if (!sa)
818 continue;
819
820 if (sa->u.sas.ss_family != AF_INET)
821 continue;
822
823 /* check if this is an specific bind */
824 if (sa->u.sin.sin_addr.s_addr == 0) {
825 if (osmo_sockaddr_local_ip(&local, remote))
826 continue;
827
828 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
829 } else {
830 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
831 }
832
833 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100834 ip4_elems->sig_weight = bind->sns_sig_weight;
835 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100836 ip4_elems++;
837 }
838
839 gss->num_ip4_local = count;
Alexander Couzense769f522020-12-07 07:37:07 +0100840 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->num_ip4_local, 8);
841 break;
842 case IPv6:
843 /* IPv6 */
844 ip6_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip6_elem) * count);
845 if (!ip6_elems)
846 return;
847
848 gss->ip6_local = ip6_elems;
849
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100850 llist_for_each_entry(sbind, &gss->binds, list) {
851 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100852 sa = gprs_ns2_ip_bind_sockaddr(bind);
853 if (!sa)
854 continue;
855
856 if (sa->u.sas.ss_family != AF_INET6)
857 continue;
858
859 /* check if this is an specific bind */
860 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
861 if (osmo_sockaddr_local_ip(&local, remote))
862 continue;
863
864 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
865 } else {
866 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
867 }
868
869 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100870 ip6_elems->sig_weight = bind->sns_sig_weight;
871 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100872
873 ip6_elems++;
874 }
875 gss->num_ip6_local = count;
Alexander Couzense769f522020-12-07 07:37:07 +0100876 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->num_ip6_local, 8);
877 break;
878 }
Harald Welte24920e22021-03-04 13:03:27 +0100879}
880
881/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Harald Welte694dad52021-03-23 15:22:16 +0100882static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Harald Welte24920e22021-03-04 13:03:27 +0100883{
884 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
885
Harald Weltef61a9152021-03-02 22:20:17 +0100886 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
887
Harald Welte24920e22021-03-04 13:03:27 +0100888 /* on a generic failure, the timer callback will recover */
889 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
890 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Harald Welte694dad52021-03-23 15:22:16 +0100891 if (old_state != GPRS_SNS_ST_BSS_SIZE)
Harald Welte24920e22021-03-04 13:03:27 +0100892 gss->N = 0;
893
894 gss->alive = false;
895
896 ns2_sns_compute_local_ep_from_binds(fi);
897
898 /* take the first bind or take the next bind */
899 if (!gss->initial_bind) {
900 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
901 } else {
902 if (gss->initial_bind->list.next != &gss->binds) {
903 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
904 } else {
905 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
906 }
907 }
908
909
910 /* setup the NSVC */
911 if (!gss->sns_nsvc) {
912 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
913 struct osmo_sockaddr *remote = &gss->initial->saddr;
914 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
915 if (!gss->sns_nsvc)
916 return;
917 gss->sns_nsvc->sns_only = true;
918 }
919
Alexander Couzense769f522020-12-07 07:37:07 +0100920
Alexander Couzens6a161492020-07-12 13:45:50 +0200921 if (gss->num_max_ip4_remote > 0)
Harald Welte4e41acc2021-03-23 11:55:25 +0100922 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->num_ip4_local, -1);
Alexander Couzens6a161492020-07-12 13:45:50 +0200923 else
Harald Welte4e41acc2021-03-23 11:55:25 +0100924 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200925}
926
Harald Welte694dad52021-03-23 15:22:16 +0100927static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200928{
Harald Weltef61a9152021-03-02 22:20:17 +0100929 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens3df58862021-02-05 17:18:08 +0100930 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Harald Weltef61a9152021-03-02 22:20:17 +0100931 struct tlv_parsed *tp = NULL;
932
933 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzens6a161492020-07-12 13:45:50 +0200934
935 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100936 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200937 tp = (struct tlv_parsed *) data;
938 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
939 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
940 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
941 /* TODO: What to do? */
942 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100943 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 +0200944 }
945 break;
946 default:
947 OSMO_ASSERT(0);
948 }
949}
950
Harald Welte694dad52021-03-23 15:22:16 +0100951static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200952{
953 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100954
Harald Weltef61a9152021-03-02 22:20:17 +0100955 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
956
Harald Welte694dad52021-03-23 15:22:16 +0100957 if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS)
Alexander Couzens790a9632021-02-05 17:18:39 +0100958 gss->N = 0;
959
Alexander Couzens6a161492020-07-12 13:45:50 +0200960 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200961 switch (gss->ip) {
962 case IPv4:
963 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100964 gss->ip4_local, gss->num_ip4_local,
965 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200966 break;
967 case IPv6:
968 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100969 NULL, 0,
970 gss->ip6_local, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200971 break;
972 }
973}
974
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100975/* calculate the timeout of the configured state. the configured
976 * state will fail if not at least one NS-VC is alive within X second.
977 */
978static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
979{
980 int secs;
981 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
982 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
983 secs += nsi->timeout[NS_TOUT_TNS_TEST];
984
985 return secs;
986}
Alexander Couzens6a161492020-07-12 13:45:50 +0200987
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100988/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
989static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200990{
991 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200992
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100993 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
994 const struct gprs_ns_ie_ip4_elem *v4_list;
995 unsigned int num_v4;
996 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
997 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +0200998
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100999 if (num_v4 && gss->ip6_remote)
1000 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +02001001
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001002 /* realloc to the new size */
1003 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote,
1004 struct gprs_ns_ie_ip4_elem,
1005 gss->num_ip4_remote + num_v4);
1006 /* append the new entries to the end of the list */
1007 memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list));
1008 gss->num_ip4_remote += num_v4;
1009
1010 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
1011 gss->num_ip4_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +02001012 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001013
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001014 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1015 const struct gprs_ns_ie_ip6_elem *v6_list;
1016 unsigned int num_v6;
1017 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1018 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
1019
1020 if (num_v6 && gss->ip4_remote)
1021 return -NS_CAUSE_INVAL_NR_IPv6_EP;
1022
1023 /* realloc to the new size */
1024 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote,
1025 struct gprs_ns_ie_ip6_elem,
1026 gss->num_ip6_remote + num_v6);
1027 /* append the new entries to the end of the list */
1028 memcpy(&gss->ip6_remote[gss->num_ip6_remote], v6_list, num_v6*sizeof(*v6_list));
1029 gss->num_ip6_remote += num_v6;
1030
1031 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %u entries\n",
1032 gss->num_ip6_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +02001033 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001034
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001035 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001036}
1037
Harald Welte694dad52021-03-23 15:22:16 +01001038static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens790a9632021-02-05 17:18:39 +01001039{
1040 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1041
Harald Weltef61a9152021-03-02 22:20:17 +01001042 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1043
Harald Welte694dad52021-03-23 15:22:16 +01001044 if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN)
Alexander Couzens790a9632021-02-05 17:18:39 +01001045 gss->N = 0;
1046}
1047
Harald Welte694dad52021-03-23 15:22:16 +01001048static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +02001049{
1050 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001051 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1052 uint8_t cause;
1053 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001054
Harald Weltef61a9152021-03-02 22:20:17 +01001055 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1056
Alexander Couzens6a161492020-07-12 13:45:50 +02001057 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001058 case GPRS_SNS_EV_RX_CONFIG_END:
1059 case GPRS_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001060 rc = ns_sns_append_remote_eps(fi, data);
1061 if (rc < 0) {
1062 cause = -rc;
1063 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1064 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1065 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001066 }
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001067 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1068 /* check if sum of data / sig weights == 0 */
1069 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1070 cause = NS_CAUSE_INVAL_WEIGH;
1071 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1072 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1073 return;
1074 }
1075 create_missing_nsvcs(fi);
1076 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1077 /* start the test procedure on ALL NSVCs! */
1078 gprs_ns2_start_alive_all_nsvcs(nse);
1079 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1080 } else {
1081 /* just send CONFIG-ACK */
1082 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1083 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001084 }
1085 break;
1086 default:
1087 OSMO_ASSERT(0);
1088 }
1089}
1090
Alexander Couzens67725e22021-02-15 02:37:03 +01001091/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001092static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1093 struct ns2_sns_state *gss,
1094 struct tlv_parsed *tp)
1095{
1096 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1097 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1098 int num_v4 = 0, num_v6 = 0;
1099 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001100 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001101 int rc = 0;
1102
1103 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1104 * check uniqueness within the lists (no doublicate entries)
1105 * check not-known-by-us and sent back a list of unknown/known values
1106 * (abnormal behaviour according to 48.016)
1107 */
1108
1109 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1110 if (gss->ip == IPv4) {
1111 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1112 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1113 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1114 return;
1115 }
1116
1117 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1118 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001119 for (i = 0; i < num_v4; i++) {
1120 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001121 rc = do_sns_add(fi, &v4_list[i], NULL);
1122 if (rc < 0) {
1123 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001124 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001125 do_sns_delete(fi, &v4_list[j], NULL);
1126 cause = -rc;
1127 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1128 break;
1129 }
1130 }
1131 } else { /* IPv6 */
1132 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1133 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1134 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1135 return;
1136 }
1137
1138 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1139 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001140 for (i = 0; i < num_v6; i++) {
1141 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001142 rc = do_sns_add(fi, NULL, &v6_list[i]);
1143 if (rc < 0) {
1144 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001145 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001146 do_sns_delete(fi, NULL, &v6_list[j]);
1147 cause = -rc;
1148 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1149 break;
1150 }
1151 }
1152 }
1153
1154 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1155 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1156}
1157
1158static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1159 struct ns2_sns_state *gss,
1160 struct tlv_parsed *tp)
1161{
1162 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1163 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1164 int num_v4 = 0, num_v6 = 0;
1165 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001166 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001167 int rc = 0;
1168
1169 /* TODO: split up delete into v4 + v6
1170 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1171 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1172 */
1173 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1174 if (gss->ip == IPv4) {
1175 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1176 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1177 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001178 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001179 rc = do_sns_delete(fi, &v4_list[i], NULL);
1180 if (rc < 0) {
1181 cause = -rc;
1182 /* continue to delete others */
1183 }
1184 }
1185 if (cause != 0xff) {
1186 /* TODO: create list of not-deleted and return it */
1187 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1188 return;
1189 }
1190
1191 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1192 /* delete all NS-VCs for given IPv4 address */
1193 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1194 struct gprs_ns_ie_ip4_elem *ip4_remote;
1195 uint32_t ip_addr = *(uint32_t *)(ie+1);
1196 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1197 cause = NS_CAUSE_UNKN_IP_ADDR;
1198 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1199 return;
1200 }
1201 /* make a copy as do_sns_delete() will change the array underneath us */
1202 ip4_remote = talloc_memdup(fi, gss->ip4_remote,
1203 gss->num_ip4_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001204 for (i = 0; i < gss->num_ip4_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001205 if (ip4_remote[i].ip_addr == ip_addr) {
1206 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1207 if (rc < 0) {
1208 cause = -rc;
1209 /* continue to delete others */
1210 }
1211 }
1212 }
1213 talloc_free(ip4_remote);
1214 if (cause != 0xff) {
1215 /* TODO: create list of not-deleted and return it */
1216 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1217 return;
1218 }
1219 } else {
1220 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1221 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1222 return;
1223 }
1224 } else { /* IPv6 */
1225 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1226 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1227 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001228 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001229 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1230 if (rc < 0) {
1231 cause = -rc;
1232 /* continue to delete others */
1233 }
1234 }
1235 if (cause != 0xff) {
1236 /* TODO: create list of not-deleted and return it */
1237 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1238 return;
1239 }
1240 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1241 /* delete all NS-VCs for given IPv4 address */
1242 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1243 struct gprs_ns_ie_ip6_elem *ip6_remote;
1244 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001245 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001246 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1247 cause = NS_CAUSE_UNKN_IP_ADDR;
1248 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1249 return;
1250 }
1251 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1252 /* make a copy as do_sns_delete() will change the array underneath us */
1253 ip6_remote = talloc_memdup(fi, gss->ip6_remote,
1254 gss->num_ip6_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001255 for (i = 0; i < gss->num_ip6_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001256 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1257 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1258 if (rc < 0) {
1259 cause = -rc;
1260 /* continue to delete others */
1261 }
1262 }
1263 }
1264
1265 talloc_free(ip6_remote);
1266 if (cause != 0xff) {
1267 /* TODO: create list of not-deleted and return it */
1268 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1269 return;
1270 }
1271 } else {
1272 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1273 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1274 return;
1275 }
1276 }
1277 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1278}
1279
1280static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1281 struct ns2_sns_state *gss,
1282 struct tlv_parsed *tp)
1283{
1284 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1285 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1286 int num_v4 = 0, num_v6 = 0;
1287 uint8_t trans_id, cause = 0xff;
1288 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001289 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001290
1291 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1292 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1293 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1294 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001295 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001296 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1297 if (rc < 0) {
1298 cause = -rc;
1299 /* continue to others */
1300 }
1301 }
1302 if (cause != 0xff) {
1303 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1304 return;
1305 }
1306 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1307 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1308 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001309 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001310 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1311 if (rc < 0) {
1312 cause = -rc;
1313 /* continue to others */
1314 }
1315 }
1316 if (cause != 0xff) {
1317 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1318 return;
1319 }
1320 } else {
1321 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1322 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1323 return;
1324 }
1325 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1326}
1327
1328static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1329{
1330 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1331 struct tlv_parsed *tp = data;
1332
1333 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001334 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001335 ns2_sns_st_configured_add(fi, gss, tp);
1336 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001337 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001338 ns2_sns_st_configured_delete(fi, gss, tp);
1339 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001340 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001341 ns2_sns_st_configured_change(fi, gss, tp);
1342 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001343 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001344 osmo_timer_del(&fi->timer);
1345 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001346 }
1347}
1348
1349static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1350{
1351 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens138b96f2021-01-25 16:23:29 +01001352 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001353}
1354
1355static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1356 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001357 .in_event_mask = 0, /* handled by all_state_action */
Harald Welte694dad52021-03-23 15:22:16 +01001358 .out_state_mask = S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001359 .name = "UNCONFIGURED",
Harald Welte694dad52021-03-23 15:22:16 +01001360 .action = ns2_sns_st_bss_unconfigured,
Alexander Couzens6a161492020-07-12 13:45:50 +02001361 },
Harald Welte694dad52021-03-23 15:22:16 +01001362 [GPRS_SNS_ST_BSS_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001363 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001364 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001365 S(GPRS_SNS_ST_BSS_SIZE) |
1366 S(GPRS_SNS_ST_BSS_CONFIG_BSS),
1367 .name = "BSS_SIZE",
1368 .action = ns2_sns_st_bss_size,
1369 .onenter = ns2_sns_st_bss_size_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001370 },
Harald Welte694dad52021-03-23 15:22:16 +01001371 [GPRS_SNS_ST_BSS_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001372 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001373 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001374 S(GPRS_SNS_ST_BSS_CONFIG_BSS) |
1375 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
1376 S(GPRS_SNS_ST_BSS_SIZE),
1377 .name = "BSS_CONFIG_BSS",
1378 .action = ns2_sns_st_bss_config_bss,
1379 .onenter = ns2_sns_st_bss_config_bss_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001380 },
Harald Welte694dad52021-03-23 15:22:16 +01001381 [GPRS_SNS_ST_BSS_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001382 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1383 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001384 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001385 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens6a161492020-07-12 13:45:50 +02001386 S(GPRS_SNS_ST_CONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001387 S(GPRS_SNS_ST_BSS_SIZE),
1388 .name = "BSS_CONFIG_SGSN",
1389 .action = ns2_sns_st_bss_config_sgsn,
1390 .onenter = ns2_sns_st_bss_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001391 },
1392 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001393 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1394 S(GPRS_SNS_EV_RX_DELETE) |
1395 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1396 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001397 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001398 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001399 .name = "CONFIGURED",
1400 .action = ns2_sns_st_configured,
1401 .onenter = ns2_sns_st_configured_onenter,
1402 },
1403};
1404
1405static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1406{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001407 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001408 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1409 struct gprs_ns2_inst *nsi = nse->nsi;
1410
Alexander Couzens90ee9632020-12-07 06:18:32 +01001411 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001412 switch (fi->T) {
1413 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001414 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1415 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001416 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001417 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001418 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 +01001419 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001420 break;
1421 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001422 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001423 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 +01001424 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001425 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001426 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 +01001427 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001428 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001429 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001430 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1431 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 +01001432 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001433 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001434 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 +01001435 }
1436 break;
1437 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001438 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 +01001439 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001440 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001441 }
1442 return 0;
1443}
1444
Harald Welte9e37bf42021-03-02 20:48:31 +01001445/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001446static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1447{
1448 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001449 struct ns2_sns_bind *sbind;
1450 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001451
Alexander Couzense769f522020-12-07 07:37:07 +01001452 switch (event) {
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001453 case GPRS_SNS_EV_REQ_ADD_BIND:
1454 sbind = data;
1455 switch (fi->state) {
1456 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001457 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001458 break;
Harald Welte694dad52021-03-23 15:22:16 +01001459 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001460 /* TODO: add the ip4 element to the list */
1461 break;
Harald Welte694dad52021-03-23 15:22:16 +01001462 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1463 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001464 case GPRS_SNS_ST_CONFIGURED:
1465 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1466 break;
1467 }
1468 break;
1469 case GPRS_SNS_EV_REQ_DELETE_BIND:
1470 sbind = data;
1471 switch (fi->state) {
1472 case GPRS_SNS_ST_UNCONFIGURED:
1473 break;
Harald Welte694dad52021-03-23 15:22:16 +01001474 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001475 /* TODO: remove the ip4 element from the list */
1476 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1477 if (nsvc->bind == sbind->bind) {
1478 gprs_ns2_free_nsvc(nsvc);
1479 }
1480 }
1481 break;
Harald Welte694dad52021-03-23 15:22:16 +01001482 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1483 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001484 case GPRS_SNS_ST_CONFIGURED:
1485 /* TODO: do an delete SNS-IP procedure */
1486 /* TODO: remove the ip4 element to the list */
1487 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1488 if (nsvc->bind == sbind->bind) {
1489 gprs_ns2_free_nsvc(nsvc);
1490 }
1491 }
1492 break;
1493 }
1494 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1495 talloc_free(sbind);
1496 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001497 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001498}
1499
Harald Welte9e37bf42021-03-02 20:48:31 +01001500/* allstate-action for BSS role */
1501static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1502{
1503 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1504 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1505
1506 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
1507 switch (event) {
1508 case GPRS_SNS_EV_REQ_NO_NSVC:
1509 /* ignore reselection running */
1510 if (gss->reselection_running)
1511 break;
1512
1513 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1514 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1515 break;
1516 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
1517 /* tear down previous state
1518 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1519 gss->reselection_running = true;
1520 gprs_ns2_free_nsvcs(nse);
Harald Welte46eb7642021-03-04 17:49:59 +01001521 ns2_clear_ipv46_entries_local(gss);
1522 ns2_clear_ipv46_entries_remote(gss);
Harald Welte9e37bf42021-03-02 20:48:31 +01001523
1524 /* Choose the next sns endpoint. */
1525 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds)) {
1526 gss->initial = NULL;
1527 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1528 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1529 return;
1530 } else if (!gss->initial) {
1531 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1532 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1533 /* last entry, continue with first */
1534 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1535 } else {
1536 /* next element is an entry */
1537 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1538 }
1539
1540 gss->reselection_running = false;
Harald Welte694dad52021-03-23 15:22:16 +01001541 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 +01001542 break;
1543 default:
1544 ns2_sns_st_all_action(fi, event, data);
1545 break;
1546 }
1547}
1548
Alexander Couzens6a161492020-07-12 13:45:50 +02001549static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1550 .name = "GPRS-NS2-SNS-BSS",
1551 .states = ns2_sns_bss_states,
1552 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001553 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1554 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001555 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1556 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001557 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001558 .cleanup = NULL,
1559 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001560 .event_names = gprs_sns_event_names,
1561 .pre_term = NULL,
1562 .log_subsys = DLNS,
1563};
1564
Harald Welte5bef2cc2020-09-18 22:33:24 +02001565/*! Allocate an IP-SNS FSM for the BSS side.
1566 * \param[in] nse NS Entity in which the FSM runs
1567 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001568 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001569struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1570 const char *id)
1571{
1572 struct osmo_fsm_inst *fi;
1573 struct ns2_sns_state *gss;
1574
1575 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1576 if (!fi)
1577 return fi;
1578
1579 gss = talloc_zero(fi, struct ns2_sns_state);
1580 if (!gss)
1581 goto err;
1582
1583 fi->priv = gss;
1584 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001585 gss->role = GPRS_SNS_ROLE_BSS;
Harald Welte24f4df52021-03-04 18:02:54 +01001586 /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */
1587 gss->num_max_ip4_remote = 8192;
1588 gss->num_max_ip6_remote = 8192;
Alexander Couzense769f522020-12-07 07:37:07 +01001589 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001590 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001591
1592 return fi;
1593err:
1594 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1595 return NULL;
1596}
1597
Harald Welte5bef2cc2020-09-18 22:33:24 +02001598/*! main entry point for receiving SNS messages from the network.
1599 * \param[in] nsvc NS-VC on which the message was received
1600 * \param[in] msg message buffer of the IP-SNS message
1601 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001602 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001603int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001604{
1605 struct gprs_ns2_nse *nse = nsvc->nse;
1606 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1607 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001608 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001609 struct osmo_fsm_inst *fi;
1610
1611 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001612 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1613 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001614 return -EINVAL;
1615 }
1616
Alexander Couzens6a161492020-07-12 13:45:50 +02001617 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1618 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001619 gss = (struct ns2_sns_state *) fi->priv;
1620 if (!gss->sns_nsvc)
1621 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001622
Harald Weltef2949742021-01-20 14:54:14 +01001623 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1624 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1625
Alexander Couzens6a161492020-07-12 13:45:50 +02001626 switch (nsh->pdu_type) {
1627 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001628 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001629 break;
1630 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001631 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001632 break;
1633 case SNS_PDUT_CONFIG:
1634 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001635 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001636 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001637 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001638 break;
1639 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001640 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001641 break;
1642 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001643 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001644 break;
1645 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001646 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001647 break;
1648 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001649 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001650 break;
1651 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001652 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001653 break;
1654 default:
Harald Weltef2949742021-01-20 14:54:14 +01001655 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1656 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001657 return -EINVAL;
1658 }
1659
1660 return 0;
1661}
1662
1663#include <osmocom/vty/vty.h>
1664#include <osmocom/vty/misc.h>
1665
Harald Welte1262c4f2021-01-19 20:58:33 +01001666static 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 +02001667{
1668 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001669 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001670 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1671}
1672
Harald Welte1262c4f2021-01-19 20:58:33 +01001673static 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 +02001674{
1675 char ip_addr[INET6_ADDRSTRLEN] = {};
1676 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1677 strcpy(ip_addr, "Invalid IPv6");
1678
Harald Welte1262c4f2021-01-19 20:58:33 +01001679 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001680 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1681}
1682
Harald Welte5bef2cc2020-09-18 22:33:24 +02001683/*! Dump the IP-SNS state to a vty.
1684 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001685 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001686 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1687 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001688void 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 +02001689{
1690 struct ns2_sns_state *gss;
1691 unsigned int i;
1692
1693 if (!nse->bss_sns_fi)
1694 return;
1695
Harald Welte1262c4f2021-01-19 20:58:33 +01001696 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001697 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1698
Harald Welte1262c4f2021-01-19 20:58:33 +01001699 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1700 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001701
1702 if (gss->num_ip4_local && gss->num_ip4_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001703 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001704 for (i = 0; i < gss->num_ip4_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001705 vty_dump_sns_ip4(vty, prefix, &gss->ip4_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001706
Harald Welte1262c4f2021-01-19 20:58:33 +01001707 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001708 for (i = 0; i < gss->num_ip4_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001709 vty_dump_sns_ip4(vty, prefix, &gss->ip4_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001710 }
1711
1712 if (gss->num_ip6_local && gss->num_ip6_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001713 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001714 for (i = 0; i < gss->num_ip6_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001715 vty_dump_sns_ip6(vty, prefix, &gss->ip6_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001716
Harald Welte1262c4f2021-01-19 20:58:33 +01001717 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001718 for (i = 0; i < gss->num_ip6_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001719 vty_dump_sns_ip6(vty, prefix, &gss->ip6_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001720 }
1721}
1722
Alexander Couzens412bc342020-11-19 05:24:37 +01001723/*! write IP-SNS to a vty
1724 * \param[in] vty VTY to which the state shall be printed
1725 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001726void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001727{
1728 struct ns2_sns_state *gss;
1729 struct osmo_sockaddr_str addr_str;
1730 struct sns_endpoint *endpoint;
1731
1732 if (!nse->bss_sns_fi)
1733 return;
1734
1735 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1736 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001737 /* It's unlikely that an error happens, but let's better be safe. */
1738 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1739 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001740 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001741 }
1742}
1743
Alexander Couzense769f522020-12-07 07:37:07 +01001744static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1745 const struct osmo_sockaddr *saddr)
1746{
1747 struct sns_endpoint *endpoint;
1748
1749 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1750 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1751 return endpoint;
1752 }
1753
1754 return NULL;
1755}
1756
1757/*! gprs_ns2_sns_add_endpoint
1758 * \param[in] nse
1759 * \param[in] sockaddr
1760 * \return
1761 */
1762int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1763 const struct osmo_sockaddr *saddr)
1764{
1765 struct ns2_sns_state *gss;
1766 struct sns_endpoint *endpoint;
1767 bool do_selection = false;
1768
1769 if (nse->ll != GPRS_NS2_LL_UDP) {
1770 return -EINVAL;
1771 }
1772
Alexander Couzens138b96f2021-01-25 16:23:29 +01001773 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001774 return -EINVAL;
1775 }
1776
1777 gss = nse->bss_sns_fi->priv;
1778
1779 if (ns2_get_sns_endpoint(gss, saddr))
1780 return -EADDRINUSE;
1781
1782 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1783 if (!endpoint)
1784 return -ENOMEM;
1785
1786 endpoint->saddr = *saddr;
1787 if (llist_empty(&gss->sns_endpoints))
1788 do_selection = true;
1789
1790 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1791 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001792 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001793
1794 return 0;
1795}
1796
1797/*! gprs_ns2_sns_del_endpoint
1798 * \param[in] nse
1799 * \param[in] sockaddr
1800 * \return 0 on success, otherwise < 0
1801 */
1802int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1803 const struct osmo_sockaddr *saddr)
1804{
1805 struct ns2_sns_state *gss;
1806 struct sns_endpoint *endpoint;
1807
1808 if (nse->ll != GPRS_NS2_LL_UDP) {
1809 return -EINVAL;
1810 }
1811
Alexander Couzens138b96f2021-01-25 16:23:29 +01001812 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001813 return -EINVAL;
1814 }
1815
1816 gss = nse->bss_sns_fi->priv;
1817 endpoint = ns2_get_sns_endpoint(gss, saddr);
1818 if (!endpoint)
1819 return -ENOENT;
1820
1821 /* if this is an unused SNS endpoint it's done */
1822 if (gss->initial != endpoint) {
1823 llist_del(&endpoint->list);
1824 talloc_free(endpoint);
1825 return 0;
1826 }
1827
Alexander Couzens67725e22021-02-15 02:37:03 +01001828 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001829 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001830 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001831 "Closing all NS-VC and restart SNS-SIZE procedure"
1832 "with a remaining SNS endpoint.\n");
1833
1834 /* Continue with the next endpoint in the list.
1835 * Special case if the endpoint is at the start or end of the list */
1836 if (endpoint->list.prev == &gss->sns_endpoints ||
1837 endpoint->list.next == &gss->sns_endpoints)
1838 gss->initial = NULL;
1839 else
1840 gss->initial = llist_entry(endpoint->list.next->prev,
1841 struct sns_endpoint,
1842 list);
1843
1844 llist_del(&endpoint->list);
1845 gprs_ns2_free_nsvcs(nse);
1846 talloc_free(endpoint);
1847
1848 return 0;
1849}
1850
1851/*! gprs_ns2_sns_count
1852 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1853 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1854 */
1855int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1856{
1857 struct ns2_sns_state *gss;
1858 struct sns_endpoint *endpoint;
1859 int count = 0;
1860
1861 if (nse->ll != GPRS_NS2_LL_UDP) {
1862 return -EINVAL;
1863 }
1864
Alexander Couzens138b96f2021-01-25 16:23:29 +01001865 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001866 return -EINVAL;
1867 }
1868
1869 gss = nse->bss_sns_fi->priv;
1870 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1871 count++;
1872
1873 return count;
1874}
1875
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001876void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1877{
1878 struct ns2_sns_state *gss;
1879 struct gprs_ns2_vc *tmp;
1880
1881 if (!nse->bss_sns_fi)
1882 return;
1883
1884 gss = nse->bss_sns_fi->priv;
1885 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1886 return;
1887
1888 if (alive == gss->alive)
1889 return;
1890
1891 /* check if this is the current SNS NS-VC */
1892 if (nsvc == gss->sns_nsvc) {
1893 /* only replace the SNS NS-VC if there are other alive NS-VC.
1894 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1895 * and couldn't confirm yet if the NS-VC comes up */
1896 if (gss->alive && !alive)
1897 ns2_sns_replace_nsvc(nsvc);
1898 }
1899
1900 if (alive) {
1901 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001902 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001903 } else {
1904 /* is there at least another alive nsvc? */
1905 llist_for_each_entry(tmp, &nse->nsvc, list) {
1906 if (ns2_vc_is_unblocked(tmp))
1907 return;
1908 }
1909
1910 /* all NS-VC have failed */
1911 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001912 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001913 }
1914}
1915
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001916int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1917 struct gprs_ns2_vc_bind *bind)
1918{
1919 struct ns2_sns_state *gss;
1920 struct ns2_sns_bind *tmp;
1921
1922 OSMO_ASSERT(nse->bss_sns_fi);
1923 gss = nse->bss_sns_fi->priv;
1924
1925 if (!gprs_ns2_is_ip_bind(bind)) {
1926 return -EINVAL;
1927 }
1928
1929 if (!llist_empty(&gss->binds)) {
1930 llist_for_each_entry(tmp, &gss->binds, list) {
1931 if (tmp->bind == bind)
1932 return -EALREADY;
1933 }
1934 }
1935
1936 tmp = talloc_zero(gss, struct ns2_sns_bind);
1937 if (!tmp)
1938 return -ENOMEM;
1939 tmp->bind = bind;
1940 llist_add_tail(&tmp->list, &gss->binds);
1941
1942 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
1943 return 0;
1944}
1945
1946/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
1947int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
1948 struct gprs_ns2_vc_bind *bind)
1949{
1950 struct ns2_sns_state *gss;
1951 struct ns2_sns_bind *tmp, *tmp2;
1952 bool found = false;
1953
1954 if (!nse->bss_sns_fi)
1955 return -EINVAL;
1956
1957 gss = nse->bss_sns_fi->priv;
1958 if (gss->initial_bind && gss->initial_bind->bind == bind) {
1959 if (gss->initial_bind->list.prev == &gss->binds)
1960 gss->initial_bind = NULL;
1961 else
1962 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
1963 }
1964
1965 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
1966 if (tmp->bind == bind) {
1967 llist_del(&tmp->list);
1968 found = true;
1969 }
1970 }
1971
1972 if (!found)
1973 return -ENOENT;
1974
1975 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
1976 return 0;
1977}
1978
Alexander Couzensc4704762021-02-08 23:13:12 +01001979/* Update SNS weights
1980 * \param[in] nsvc the NSVC which should be updated
1981 */
1982void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
1983{
1984 /* TODO: implement weights after binds per sns implemented */
1985}
1986
Harald Welte4f127462021-03-02 20:49:10 +01001987
1988
1989
1990/***********************************************************************
1991 * SGSN role
1992 ***********************************************************************/
1993
1994static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1995{
1996 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1997 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
1998 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
1999}
2000
2001/* We're waiting for inbound SNS-CONFIG from the BSS */
2002static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2003{
2004 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2005 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2006 struct gprs_ns2_inst *nsi = nse->nsi;
2007 uint8_t cause;
2008 int rc;
2009
2010 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2011
2012 switch (event) {
2013 case GPRS_SNS_EV_RX_CONFIG:
2014 case GPRS_SNS_EV_RX_CONFIG_END:
2015 rc = ns_sns_append_remote_eps(fi, data);
2016 if (rc < 0) {
2017 cause = -rc;
2018 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2019 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2020 return;
2021 }
2022 /* only change state if last CONFIG was received */
2023 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
2024 /* ensure sum of data weight / sig weights is > 0 */
2025 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
2026 cause = NS_CAUSE_INVAL_WEIGH;
2027 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2028 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2029 break;
2030 }
2031 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2032 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2033 } else {
2034 /* just send CONFIG-ACK */
2035 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2036 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2037 }
2038 break;
2039 }
2040}
2041
2042static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2043{
2044 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2045 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2046
Harald Welte4f127462021-03-02 20:49:10 +01002047 /* transmit SGSN-oriented SNS-CONFIG */
2048 ns2_tx_sns_config(gss->sns_nsvc, true, gss->ip4_local, gss->num_ip4_local,
2049 gss->ip6_local, gss->num_ip6_local);
2050}
2051
2052/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2053static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2054{
2055 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2056 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2057 struct tlv_parsed *tp = NULL;
2058
2059 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2060
2061 switch (event) {
2062 case GPRS_SNS_EV_RX_CONFIG_ACK:
2063 tp = data;
2064 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2065 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2066 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2067 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2068 break;
2069 }
2070 /* we currently only send one SNS-CONFIG with END FLAG */
2071 if (true) {
2072 create_missing_nsvcs(fi);
2073 /* start the test procedure on ALL NSVCs! */
2074 gprs_ns2_start_alive_all_nsvcs(nse);
2075 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2076 }
2077 break;
2078 }
2079}
2080
2081/* SGSN-side SNS state machine */
2082static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2083 [GPRS_SNS_ST_UNCONFIGURED] = {
2084 .in_event_mask = 0, /* handled by all_state_action */
2085 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2086 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2087 .name = "UNCONFIGURED",
2088 .action = ns2_sns_st_sgsn_unconfigured,
2089 },
2090 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
2091 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
2092 S(GPRS_SNS_EV_RX_CONFIG_END),
2093 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2094 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2095 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2096 .name = "SGSN_WAIT_CONFIG",
2097 .action = ns2_sns_st_sgsn_wait_config,
2098 },
2099 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
2100 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
2101 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2102 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2103 S(GPRS_SNS_ST_CONFIGURED),
2104 .name = "SGSN_WAIT_CONFIG_ACK",
2105 .action = ns2_sns_st_sgsn_wait_config_ack,
2106 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2107 },
2108 [GPRS_SNS_ST_CONFIGURED] = {
2109 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
2110 S(GPRS_SNS_EV_RX_DELETE) |
2111 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
2112 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
2113 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2114 .name = "CONFIGURED",
2115 /* shared with BSS side; once configured there's no difference */
2116 .action = ns2_sns_st_configured,
2117 .onenter = ns2_sns_st_configured_onenter,
2118 },
2119};
2120
2121static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2122{
2123 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2124 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2125 struct gprs_ns2_inst *nsi = nse->nsi;
2126
2127 gss->N++;
2128 switch (fi->T) {
2129 case 3:
2130 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2131 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2132 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2133 } else {
2134 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2135 }
2136 break;
2137 case 4:
2138 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2139 break;
2140 }
2141 return 0;
2142}
2143
2144
2145/* allstate-action for SGSN role */
2146static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2147{
2148 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2149 struct tlv_parsed *tp = NULL;
Harald Welte01fa6a32021-03-04 19:49:38 +01002150 size_t num_local_eps, num_remote_eps;
Harald Welte4f127462021-03-02 20:49:10 +01002151 uint8_t flag;
Harald Weltea2c5af52021-03-04 17:59:35 +01002152 uint8_t cause;
Harald Welte4f127462021-03-02 20:49:10 +01002153
2154 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2155
2156 switch (event) {
2157 case GPRS_SNS_EV_RX_SIZE:
2158 tp = (struct tlv_parsed *) data;
Harald Weltea2c5af52021-03-04 17:59:35 +01002159 /* check for mandatory / conditional IEs */
2160 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) ||
2161 !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) {
2162 cause = NS_CAUSE_MISSING_ESSENT_IE;
2163 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2164 break;
2165 }
2166 if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) &&
2167 !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) {
2168 cause = NS_CAUSE_MISSING_ESSENT_IE;
Harald Welte4f127462021-03-02 20:49:10 +01002169 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2170 break;
2171 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002172 if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2))
2173 gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR);
2174 if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2))
2175 gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR);
2176 /* decide if we go for IPv4 or IPv6 */
2177 if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, IPv6)) {
2178 gss->ip = IPv6;
2179 num_local_eps = gss->num_ip6_local;
2180 num_remote_eps = gss->num_max_ip6_remote;
2181 } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, IPv4)) {
2182 gss->ip = IPv4;
2183 num_local_eps = gss->num_ip4_local;
2184 num_remote_eps = gss->num_max_ip4_remote;
2185 } else {
2186 if (gss->num_ip4_local && !gss->num_max_ip4_remote)
2187 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
2188 else
2189 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
2190 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2191 break;
2192 }
2193 ns2_sns_compute_local_ep_from_binds(fi);
2194 /* ensure number of NS-VCs is sufficient for full mesh */
2195 gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC);
2196 if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) {
2197 LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, "
2198 "but BSS supports only %zu maximum NS-VCs\n", num_local_eps,
2199 num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs);
2200 cause = NS_CAUSE_INVAL_NR_NS_VC;
2201 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2202 break;
2203 }
2204 /* perform state reset, if requested */
Harald Welte4f127462021-03-02 20:49:10 +01002205 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2206 if (flag & 1) {
2207 struct gprs_ns2_vc *nsvc, *nsvc2;
2208 /* clear all state */
Harald Welte46eb7642021-03-04 17:49:59 +01002209 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
Harald Welte4f127462021-03-02 20:49:10 +01002210 gss->N = 0;
Harald Welte46eb7642021-03-04 17:49:59 +01002211 ns2_clear_ipv46_entries_local(gss);
2212 ns2_clear_ipv46_entries_remote(gss);
Harald Welte4f127462021-03-02 20:49:10 +01002213 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2214 if (nsvc == gss->sns_nsvc) {
2215 /* keep the NSVC we need for SNS, but unconfigure it */
2216 nsvc->sig_weight = 0;
2217 nsvc->data_weight = 0;
2218 ns2_vc_force_unconfigured(nsvc);
2219 } else {
2220 /* free all other NS-VCs */
2221 gprs_ns2_free_nsvc(nsvc);
2222 }
2223 }
2224 }
2225 /* send SIZE_ACK */
2226 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2227 /* only wait for SNS-CONFIG in case of Reset flag */
2228 if (flag & 1)
2229 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2230 break;
2231 default:
2232 ns2_sns_st_all_action(fi, event, data);
2233 break;
2234 }
2235}
2236
2237static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2238 .name = "GPRS-NS2-SNS-SGSN",
2239 .states = ns2_sns_sgsn_states,
2240 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
2241 .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) |
2242 S(GPRS_SNS_EV_REQ_NO_NSVC) |
2243 S(GPRS_SNS_EV_REQ_ADD_BIND) |
2244 S(GPRS_SNS_EV_REQ_DELETE_BIND),
2245 .allstate_action = ns2_sns_st_all_action_sgsn,
2246 .cleanup = NULL,
2247 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2248 .event_names = gprs_sns_event_names,
2249 .pre_term = NULL,
2250 .log_subsys = DLNS,
2251};
2252
2253/*! Allocate an IP-SNS FSM for the SGSN side.
2254 * \param[in] nse NS Entity in which the FSM runs
2255 * \param[in] id string identifier
2256 * \returns FSM instance on success; NULL on error */
2257struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2258{
2259 struct osmo_fsm_inst *fi;
2260 struct ns2_sns_state *gss;
2261
2262 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2263 if (!fi)
2264 return fi;
2265
2266 gss = talloc_zero(fi, struct ns2_sns_state);
2267 if (!gss)
2268 goto err;
2269
2270 fi->priv = gss;
2271 gss->nse = nse;
2272 gss->role = GPRS_SNS_ROLE_SGSN;
2273 INIT_LLIST_HEAD(&gss->sns_endpoints);
2274 INIT_LLIST_HEAD(&gss->binds);
2275
2276 return fi;
2277err:
2278 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2279 return NULL;
2280}
2281
2282
2283
2284
Alexander Couzens6a161492020-07-12 13:45:50 +02002285/* initialize osmo_ctx on main tread */
2286static __attribute__((constructor)) void on_dso_load_ctx(void)
2287{
2288 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002289 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002290}