blob: c6e864290f633cc329e6ac618237690acb5912aa [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
Alexander Couzens6a161492020-07-12 13:45:50 +020069enum gprs_sns_bss_state {
70 GPRS_SNS_ST_UNCONFIGURED,
71 GPRS_SNS_ST_SIZE, /*!< SNS-SIZE procedure ongoing */
72 GPRS_SNS_ST_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */
73 GPRS_SNS_ST_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */
74 GPRS_SNS_ST_CONFIGURED,
Harald Welte4f127462021-03-02 20:49:10 +010075 GPRS_SNS_ST_SGSN_WAIT_CONFIG, /* !< SGSN role: Wait for CONFIG from BSS */
76 GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, /* !< SGSN role: Wait for CONFIG-ACK from BSS */
Alexander Couzens6a161492020-07-12 13:45:50 +020077};
78
79enum gprs_sns_event {
Alexander Couzens67725e22021-02-15 02:37:03 +010080 GPRS_SNS_EV_REQ_SELECT_ENDPOINT, /*!< Select a SNS endpoint from the list */
81 GPRS_SNS_EV_RX_SIZE,
82 GPRS_SNS_EV_RX_SIZE_ACK,
83 GPRS_SNS_EV_RX_CONFIG,
84 GPRS_SNS_EV_RX_CONFIG_END, /*!< SNS-CONFIG with end flag received */
85 GPRS_SNS_EV_RX_CONFIG_ACK,
86 GPRS_SNS_EV_RX_ADD,
87 GPRS_SNS_EV_RX_DELETE,
88 GPRS_SNS_EV_RX_CHANGE_WEIGHT,
Harald Welteb9f23872021-03-02 20:48:54 +010089 GPRS_SNS_EV_RX_ACK, /*!< Rx of SNS-ACK (response to ADD/DELETE/CHG_WEIGHT */
Harald Welte04647e12021-03-02 18:50:40 +010090 GPRS_SNS_EV_REQ_NO_NSVC, /*!< no more NS-VC remaining (all dead) */
Alexander Couzens67725e22021-02-15 02:37:03 +010091 GPRS_SNS_EV_REQ_NSVC_ALIVE, /*!< a NS-VC became alive */
Harald Welte04647e12021-03-02 18:50:40 +010092 GPRS_SNS_EV_REQ_ADD_BIND, /*!< add a new local bind to this NSE */
93 GPRS_SNS_EV_REQ_DELETE_BIND, /*!< remove a local bind from this NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +020094};
95
96static const struct value_string gprs_sns_event_names[] = {
Alexander Couzens67725e22021-02-15 02:37:03 +010097 { GPRS_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" },
98 { GPRS_SNS_EV_RX_SIZE, "RX_SIZE" },
99 { GPRS_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" },
100 { GPRS_SNS_EV_RX_CONFIG, "RX_CONFIG" },
101 { GPRS_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" },
102 { GPRS_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" },
103 { GPRS_SNS_EV_RX_ADD, "RX_ADD" },
104 { GPRS_SNS_EV_RX_DELETE, "RX_DELETE" },
Harald Welteb9f23872021-03-02 20:48:54 +0100105 { GPRS_SNS_EV_RX_ACK, "RX_ACK" },
Alexander Couzens67725e22021-02-15 02:37:03 +0100106 { GPRS_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" },
107 { GPRS_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" },
108 { GPRS_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"},
109 { GPRS_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"},
110 { GPRS_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200111 { 0, NULL }
112};
113
Alexander Couzense769f522020-12-07 07:37:07 +0100114struct sns_endpoint {
115 struct llist_head list;
116 struct osmo_sockaddr saddr;
117};
118
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100119struct ns2_sns_bind {
120 struct llist_head list;
121 struct gprs_ns2_vc_bind *bind;
122};
123
Alexander Couzens6a161492020-07-12 13:45:50 +0200124struct ns2_sns_state {
125 struct gprs_ns2_nse *nse;
126
127 enum ns2_sns_type ip;
Harald Welte4f127462021-03-02 20:49:10 +0100128 enum ns2_sns_role role; /* local role: BSS or SGSN */
Alexander Couzens6a161492020-07-12 13:45:50 +0200129
Alexander Couzense769f522020-12-07 07:37:07 +0100130 /* holds the list of initial SNS endpoints */
131 struct llist_head sns_endpoints;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100132 /* list of used struct ns2_sns_bind */
133 struct llist_head binds;
134 /* pointer to the bind which was used to initiate the SNS connection */
135 struct ns2_sns_bind *initial_bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100136 /* prevent recursive reselection */
137 bool reselection_running;
138
139 /* The current initial SNS endpoints.
140 * The initial connection will be moved into the NSE
141 * if configured via SNS. Otherwise it will be removed
142 * in configured state. */
143 struct sns_endpoint *initial;
Alexander Couzens6a161492020-07-12 13:45:50 +0200144 /* all SNS PDU will be sent over this nsvc */
145 struct gprs_ns2_vc *sns_nsvc;
Alexander Couzens90ee9632020-12-07 06:18:32 +0100146 /* timer N */
147 int N;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100148 /* true if at least one nsvc is alive */
149 bool alive;
Alexander Couzens6a161492020-07-12 13:45:50 +0200150
151 /* local configuration to send to the remote end */
152 struct gprs_ns_ie_ip4_elem *ip4_local;
153 size_t num_ip4_local;
154
155 /* local configuration to send to the remote end */
156 struct gprs_ns_ie_ip6_elem *ip6_local;
157 size_t num_ip6_local;
158
159 /* local configuration about our capabilities in terms of connections to
160 * remote (SGSN) side */
161 size_t num_max_nsvcs;
162 size_t num_max_ip4_remote;
163 size_t num_max_ip6_remote;
164
165 /* remote configuration as received */
166 struct gprs_ns_ie_ip4_elem *ip4_remote;
167 unsigned int num_ip4_remote;
168
169 /* remote configuration as received */
170 struct gprs_ns_ie_ip6_elem *ip6_remote;
171 unsigned int num_ip6_remote;
172};
173
174static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi)
175{
176 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
177 return gss->nse;
178}
179
180/* helper function to compute the sum of all (data or signaling) weights */
181static int ip4_weight_sum(const struct gprs_ns_ie_ip4_elem *ip4, unsigned int num,
182 bool data_weight)
183{
184 unsigned int i;
185 int weight_sum = 0;
186
187 for (i = 0; i < num; i++) {
188 if (data_weight)
189 weight_sum += ip4[i].data_weight;
190 else
191 weight_sum += ip4[i].sig_weight;
192 }
193 return weight_sum;
194}
195#define ip4_weight_sum_data(x,y) ip4_weight_sum(x, y, true)
196#define ip4_weight_sum_sig(x,y) ip4_weight_sum(x, y, false)
197
198/* helper function to compute the sum of all (data or signaling) weights */
199static int ip6_weight_sum(const struct gprs_ns_ie_ip6_elem *ip6, unsigned int num,
200 bool data_weight)
201{
202 unsigned int i;
203 int weight_sum = 0;
204
205 for (i = 0; i < num; i++) {
206 if (data_weight)
207 weight_sum += ip6[i].data_weight;
208 else
209 weight_sum += ip6[i].sig_weight;
210 }
211 return weight_sum;
212}
213#define ip6_weight_sum_data(x,y) ip6_weight_sum(x, y, true)
214#define ip6_weight_sum_sig(x,y) ip6_weight_sum(x, y, false)
215
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100216static int nss_weight_sum(const struct ns2_sns_state *nss, bool data_weight)
217{
218 return ip4_weight_sum(nss->ip4_remote, nss->num_ip4_remote, data_weight) +
219 ip6_weight_sum(nss->ip6_remote, nss->num_ip6_remote, data_weight);
220}
221#define nss_weight_sum_data(nss) nss_weight_sum(nss, true)
222#define nss_weight_sum_sig(nss) nss_weight_sum(nss, false)
223
Alexander Couzens6a161492020-07-12 13:45:50 +0200224static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
225 const struct gprs_ns_ie_ip4_elem *ip4)
226{
227 struct osmo_sockaddr sa;
228 /* copy over. Both data structures use network byte order */
229 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
230 sa.u.sin.sin_port = ip4->udp_port;
231 sa.u.sin.sin_family = AF_INET;
232
Alexander Couzens38b19e82020-09-23 23:56:37 +0200233 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200234}
235
236static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
237 const struct gprs_ns_ie_ip6_elem *ip6)
238{
239 struct osmo_sockaddr sa;
240 /* copy over. Both data structures use network byte order */
241 sa.u.sin6.sin6_addr = ip6->ip_addr;
242 sa.u.sin6.sin6_port = ip6->udp_port;
243 sa.u.sin6.sin6_family = AF_INET;
244
Alexander Couzens38b19e82020-09-23 23:56:37 +0200245 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200246}
247
Alexander Couzens125298f2020-10-11 21:22:42 +0200248/*! Return the initial SNS remote socket address
249 * \param nse NS Entity
250 * \return address of the initial SNS connection; NULL in case of error
251 */
252const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
253{
254 struct ns2_sns_state *gss;
255
256 if (!nse->bss_sns_fi)
257 return NULL;
258
259 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100260 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200261}
262
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100263/*! called when a nsvc is beeing freed or the nsvc became dead */
264void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200265{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100266 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200267 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100268 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200269 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200270
271 if (!fi)
272 return;
273
274 gss = (struct ns2_sns_state *) fi->priv;
275 if (nsvc != gss->sns_nsvc)
276 return;
277
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100278 gss->sns_nsvc = NULL;
279 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200280 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100281 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200282 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100283 return;
284 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200285 }
286 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100287 /* the SNS is waiting for its first NS-VC to come up
288 * choose any other nsvc */
289 llist_for_each_entry(tmp, &nse->nsvc, list) {
290 if (nsvc != tmp) {
291 gss->sns_nsvc = tmp;
292 return;
293 }
294 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200295 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100296
Alexander Couzens67725e22021-02-15 02:37:03 +0100297 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200298}
299
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100300static void ns2_clear_ipv46_entries(struct ns2_sns_state *gss)
301{
302 TALLOC_FREE(gss->ip4_local);
303 TALLOC_FREE(gss->ip4_remote);
304 TALLOC_FREE(gss->ip6_local);
305 TALLOC_FREE(gss->ip6_remote);
306
307 gss->num_ip4_local = 0;
308 gss->num_ip4_remote = 0;
309 gss->num_ip6_local = 0;
310 gss->num_ip6_remote = 0;
311}
312
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100313static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
314 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200315{
316 struct gprs_ns2_inst *nsi = nse->nsi;
317 struct gprs_ns2_vc *nsvc;
318 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100319
320 /* for every bind, create a connection if bind type == IP */
321 llist_for_each_entry(bind, &nsi->binding, list) {
322 if (bind->ll != GPRS_NS2_LL_UDP)
323 continue;
324 /* ignore failed connection */
325 nsvc = gprs_ns2_ip_connect_inactive(bind,
326 remote,
327 nse, 0);
328 if (!nsvc) {
329 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
330 continue;
331 }
332
333 nsvc->sig_weight = sig_weight;
334 nsvc->data_weight = data_weight;
335 }
336}
337
338static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
339 struct gprs_ns2_nse *nse,
340 const struct gprs_ns_ie_ip4_elem *ip4)
341{
Alexander Couzensc068d862020-10-12 04:11:51 +0200342 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200343 /* copy over. Both data structures use network byte order */
344 remote.u.sin.sin_family = AF_INET;
345 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
346 remote.u.sin.sin_port = ip4->udp_port;
347
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100348 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200349}
350
351static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
352 struct gprs_ns2_nse *nse,
353 const struct gprs_ns_ie_ip6_elem *ip6)
354{
Alexander Couzens6a161492020-07-12 13:45:50 +0200355 struct osmo_sockaddr remote = {};
356 /* copy over. Both data structures use network byte order */
357 remote.u.sin6.sin6_family = AF_INET6;
358 remote.u.sin6.sin6_addr = ip6->ip_addr;
359 remote.u.sin6.sin6_port = ip6->udp_port;
360
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100361 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200362}
363
364
365static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
366{
367 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
368 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
369 struct gprs_ns2_vc *nsvc;
370 struct gprs_ns2_vc_bind *bind;
371 struct osmo_sockaddr remote = { };
372 unsigned int i;
373
374 for (i = 0; i < gss->num_ip4_remote; i++) {
375 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->ip4_remote[i];
376
377 remote.u.sin.sin_family = AF_INET;
378 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
379 remote.u.sin.sin_port = ip4->udp_port;
380
381 llist_for_each_entry(bind, &nse->nsi->binding, list) {
382 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100383 if (bind->ll != GPRS_NS2_LL_UDP)
384 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200385
386 llist_for_each_entry(nsvc, &nse->nsvc, list) {
387 if (nsvc->bind != bind)
388 continue;
389
Alexander Couzensc4229a42020-10-11 20:58:04 +0200390 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200391 found = true;
392 break;
393 }
394 }
395
396 if (!found) {
397 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
398 if (!nsvc) {
399 /* TODO: add to a list to send back a NS-STATUS */
400 continue;
401 }
402 }
403
404 /* update data / signalling weight */
405 nsvc->data_weight = ip4->data_weight;
406 nsvc->sig_weight = ip4->sig_weight;
407 nsvc->sns_only = false;
408 }
409 }
410
411 for (i = 0; i < gss->num_ip6_remote; i++) {
412 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->ip6_remote[i];
413
414 remote.u.sin6.sin6_family = AF_INET6;
415 remote.u.sin6.sin6_addr = ip6->ip_addr;
416 remote.u.sin6.sin6_port = ip6->udp_port;
417
418 llist_for_each_entry(bind, &nse->nsi->binding, list) {
419 bool found = false;
Daniel Willmann967e2c12021-01-14 16:58:17 +0100420 if (bind->ll != GPRS_NS2_LL_UDP)
421 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200422
423 llist_for_each_entry(nsvc, &nse->nsvc, list) {
424 if (nsvc->bind != bind)
425 continue;
426
Alexander Couzensc4229a42020-10-11 20:58:04 +0200427 if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200428 found = true;
429 break;
430 }
431 }
432
433 if (!found) {
434 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
435 if (!nsvc) {
436 /* TODO: add to a list to send back a NS-STATUS */
437 continue;
438 }
439 }
440
441 /* update data / signalling weight */
442 nsvc->data_weight = ip6->data_weight;
443 nsvc->sig_weight = ip6->sig_weight;
444 nsvc->sns_only = false;
445 }
446 }
447
448
449 return 0;
450}
451
452/* Add a given remote IPv4 element to gprs_sns_state */
453static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
454{
455 unsigned int i;
456
457 if (gss->num_ip4_remote >= gss->num_max_ip4_remote)
458 return -NS_CAUSE_INVAL_NR_NS_VC;
459
460 /* check for duplicates */
461 for (i = 0; i < gss->num_ip4_remote; i++) {
462 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
463 continue;
464 /* TODO: log message duplicate */
Alexander Couzens6a161492020-07-12 13:45:50 +0200465 return -NS_CAUSE_PROTO_ERR_UNSPEC;
466 }
467
468 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem,
469 gss->num_ip4_remote+1);
470 gss->ip4_remote[gss->num_ip4_remote] = *ip4;
471 gss->num_ip4_remote += 1;
472 return 0;
473}
474
475/* Remove a given remote IPv4 element from gprs_sns_state */
476static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
477{
478 unsigned int i;
479
480 for (i = 0; i < gss->num_ip4_remote; i++) {
481 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
482 continue;
483 /* all array elements < i remain as they are; all > i are shifted left by one */
484 memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1);
485 gss->num_ip4_remote -= 1;
486 return 0;
487 }
488 return -1;
489}
490
491/* update the weights for specified remote IPv4 */
492static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
493{
494 unsigned int i;
495
496 for (i = 0; i < gss->num_ip4_remote; i++) {
497 if (gss->ip4_remote[i].ip_addr != ip4->ip_addr ||
498 gss->ip4_remote[i].udp_port != ip4->udp_port)
499 continue;
500
501 gss->ip4_remote[i].sig_weight = ip4->sig_weight;
502 gss->ip4_remote[i].data_weight = ip4->data_weight;
503 return 0;
504 }
505 return -1;
506}
507
508/* Add a given remote IPv6 element to gprs_sns_state */
509static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
510{
511 if (gss->num_ip6_remote >= gss->num_max_ip6_remote)
512 return -NS_CAUSE_INVAL_NR_NS_VC;
513
514 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, struct gprs_ns_ie_ip6_elem,
515 gss->num_ip6_remote+1);
516 gss->ip6_remote[gss->num_ip6_remote] = *ip6;
517 gss->num_ip6_remote += 1;
518 return 0;
519}
520
521/* Remove a given remote IPv6 element from gprs_sns_state */
522static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
523{
524 unsigned int i;
525
526 for (i = 0; i < gss->num_ip6_remote; i++) {
527 if (memcmp(&gss->ip6_remote[i], ip6, sizeof(*ip6)))
528 continue;
529 /* all array elements < i remain as they are; all > i are shifted left by one */
530 memmove(&gss->ip6_remote[i], &gss->ip6_remote[i+1], gss->num_ip6_remote-i-1);
531 gss->num_ip6_remote -= 1;
532 return 0;
533 }
534 return -1;
535}
536
537/* update the weights for specified remote IPv6 */
538static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
539{
540 unsigned int i;
541
542 for (i = 0; i < gss->num_ip6_remote; i++) {
543 if (memcmp(&gss->ip6_remote[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
544 gss->ip6_remote[i].udp_port != ip6->udp_port)
545 continue;
546 gss->ip6_remote[i].sig_weight = ip6->sig_weight;
547 gss->ip6_remote[i].data_weight = ip6->data_weight;
548 return 0;
549 }
550 return -1;
551}
552
553static 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)
554{
555 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
556 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
557 struct gprs_ns2_vc *nsvc;
558 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200559 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200560 uint8_t new_signal;
561 uint8_t new_data;
562
563 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
564 * signalling weights of all the peer IP endpoints configured for this NSE is
565 * equal to zero or if the resulting sum of the data weights of all the peer IP
566 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
567 * SNS-ACK PDU with a cause code of "Invalid weights". */
568
569 if (ip4) {
570 if (update_remote_ip4_elem(gss, ip4))
571 return -NS_CAUSE_UNKN_IP_EP;
572
573 /* copy over. Both data structures use network byte order */
574 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
575 sa.u.sin.sin_port = ip4->udp_port;
576 sa.u.sin.sin_family = AF_INET;
577 new_signal = ip4->sig_weight;
578 new_data = ip4->data_weight;
579 } else if (ip6) {
580 if (update_remote_ip6_elem(gss, ip6))
581 return -NS_CAUSE_UNKN_IP_EP;
582
583 /* copy over. Both data structures use network byte order */
584 sa.u.sin6.sin6_addr = ip6->ip_addr;
585 sa.u.sin6.sin6_port = ip6->udp_port;
586 sa.u.sin6.sin6_family = AF_INET6;
587 new_signal = ip6->sig_weight;
588 new_data = ip6->data_weight;
589 } else {
590 OSMO_ASSERT(false);
591 }
592
593 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200594 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200595 /* all nsvc in NSE should be IP/UDP nsvc */
596 OSMO_ASSERT(remote);
597
598 if (osmo_sockaddr_cmp(&sa, remote))
599 continue;
600
601 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
602 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
603 nsvc->sig_weight, new_signal);
604
605 nsvc->data_weight = new_data;
606 nsvc->sig_weight = new_signal;
607 }
608
609 return 0;
610}
611
612static int do_sns_delete(struct osmo_fsm_inst *fi,
613 const struct gprs_ns_ie_ip4_elem *ip4,
614 const struct gprs_ns_ie_ip6_elem *ip6)
615{
616 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
617 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
618 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200619 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200620 struct osmo_sockaddr sa = {};
621
622 if (ip4) {
623 if (remove_remote_ip4_elem(gss, ip4) < 0)
624 return -NS_CAUSE_UNKN_IP_EP;
625 /* copy over. Both data structures use network byte order */
626 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
627 sa.u.sin.sin_port = ip4->udp_port;
628 sa.u.sin.sin_family = AF_INET;
629 } else if (ip6) {
630 if (remove_remote_ip6_elem(gss, ip6))
631 return -NS_CAUSE_UNKN_IP_EP;
632
633 /* copy over. Both data structures use network byte order */
634 sa.u.sin6.sin6_addr = ip6->ip_addr;
635 sa.u.sin6.sin6_port = ip6->udp_port;
636 sa.u.sin6.sin6_family = AF_INET6;
637 } else {
638 OSMO_ASSERT(false);
639 }
640
641 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200642 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200643 /* all nsvc in NSE should be IP/UDP nsvc */
644 OSMO_ASSERT(remote);
645 if (osmo_sockaddr_cmp(&sa, remote))
646 continue;
647
648 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
649 gprs_ns2_free_nsvc(nsvc);
650 }
651
652 return 0;
653}
654
655static int do_sns_add(struct osmo_fsm_inst *fi,
656 const struct gprs_ns_ie_ip4_elem *ip4,
657 const struct gprs_ns_ie_ip6_elem *ip6)
658{
659 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
660 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
661 struct gprs_ns2_vc *nsvc;
662 int rc = 0;
663
664 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
665 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
666 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
667 switch (gss->ip) {
668 case IPv4:
669 rc = add_remote_ip4_elem(gss, ip4);
670 break;
671 case IPv6:
672 rc = add_remote_ip6_elem(gss, ip6);
673 break;
674 default:
675 /* the gss->ip is initialized with the bss */
676 OSMO_ASSERT(false);
677 }
678
679 if (rc)
680 return rc;
681
682 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
683 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
684 * unspecified" */
685 switch (gss->ip) {
686 case IPv4:
687 nsvc = nsvc_by_ip4_elem(nse, ip4);
688 if (nsvc) {
689 /* the nsvc should be already in sync with the ip4 / ip6 elements */
690 return -NS_CAUSE_PROTO_ERR_UNSPEC;
691 }
692
693 /* TODO: failure case */
694 ns2_nsvc_create_ip4(fi, nse, ip4);
695 break;
696 case IPv6:
697 nsvc = nsvc_by_ip6_elem(nse, ip6);
698 if (nsvc) {
699 /* the nsvc should be already in sync with the ip4 / ip6 elements */
700 return -NS_CAUSE_PROTO_ERR_UNSPEC;
701 }
702
703 /* TODO: failure case */
704 ns2_nsvc_create_ip6(fi, nse, ip6);
705 break;
706 }
707
708 gprs_ns2_start_alive_all_nsvcs(nse);
709
710 return 0;
711}
712
713
714static void ns2_sns_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
715{
Alexander Couzense769f522020-12-07 07:37:07 +0100716 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200717}
718
719static void ns2_sns_st_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
720{
721 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
722 struct gprs_ns2_inst *nsi = nse->nsi;
723 struct tlv_parsed *tp = NULL;
724
725 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100726 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200727 tp = data;
728 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
729 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
730 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
731 /* TODO: What to do? */
732 } else {
733 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_BSS,
734 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
735 }
736 break;
737 default:
738 OSMO_ASSERT(0);
739 }
740}
741
Harald Welte24920e22021-03-04 13:03:27 +0100742static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200743{
744 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100745 struct gprs_ns_ie_ip4_elem *ip4_elems;
746 struct gprs_ns_ie_ip6_elem *ip6_elems;
747 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100748 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100749 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100750 const struct osmo_sockaddr *sa;
751 struct osmo_sockaddr local;
752 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200753
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100754 ns2_clear_ipv46_entries(gss);
755
Alexander Couzense769f522020-12-07 07:37:07 +0100756 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100757 if (gss->role == GPRS_SNS_ROLE_BSS) {
758 if (!gss->initial)
759 return;
760 remote = &gss->initial->saddr;
761 } else
762 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100763
764 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100765 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100766 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100767 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100768 return;
769 }
770
Alexander Couzense769f522020-12-07 07:37:07 +0100771 switch (gss->ip) {
772 case IPv4:
773 ip4_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip4_elem) * count);
774 if (!ip4_elems)
775 return;
776
777 gss->ip4_local = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100778 llist_for_each_entry(sbind, &gss->binds, list) {
779 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100780 sa = gprs_ns2_ip_bind_sockaddr(bind);
781 if (!sa)
782 continue;
783
784 if (sa->u.sas.ss_family != AF_INET)
785 continue;
786
787 /* check if this is an specific bind */
788 if (sa->u.sin.sin_addr.s_addr == 0) {
789 if (osmo_sockaddr_local_ip(&local, remote))
790 continue;
791
792 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
793 } else {
794 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
795 }
796
797 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100798 ip4_elems->sig_weight = bind->sns_sig_weight;
799 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100800 ip4_elems++;
801 }
802
803 gss->num_ip4_local = count;
804 gss->num_max_ip4_remote = 4;
805 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->num_ip4_local, 8);
806 break;
807 case IPv6:
808 /* IPv6 */
809 ip6_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip6_elem) * count);
810 if (!ip6_elems)
811 return;
812
813 gss->ip6_local = ip6_elems;
814
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100815 llist_for_each_entry(sbind, &gss->binds, list) {
816 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100817 sa = gprs_ns2_ip_bind_sockaddr(bind);
818 if (!sa)
819 continue;
820
821 if (sa->u.sas.ss_family != AF_INET6)
822 continue;
823
824 /* check if this is an specific bind */
825 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
826 if (osmo_sockaddr_local_ip(&local, remote))
827 continue;
828
829 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
830 } else {
831 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
832 }
833
834 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100835 ip6_elems->sig_weight = bind->sns_sig_weight;
836 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100837
838 ip6_elems++;
839 }
840 gss->num_ip6_local = count;
841 gss->num_max_ip6_remote = 4;
842 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->num_ip6_local, 8);
843 break;
844 }
Harald Welte24920e22021-03-04 13:03:27 +0100845}
846
847/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
848static void ns2_sns_st_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
849{
850 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
851
852 /* on a generic failure, the timer callback will recover */
853 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
854 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
855 if (old_state != GPRS_SNS_ST_SIZE)
856 gss->N = 0;
857
858 gss->alive = false;
859
860 ns2_sns_compute_local_ep_from_binds(fi);
861
862 /* take the first bind or take the next bind */
863 if (!gss->initial_bind) {
864 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
865 } else {
866 if (gss->initial_bind->list.next != &gss->binds) {
867 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
868 } else {
869 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
870 }
871 }
872
873
874 /* setup the NSVC */
875 if (!gss->sns_nsvc) {
876 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
877 struct osmo_sockaddr *remote = &gss->initial->saddr;
878 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
879 if (!gss->sns_nsvc)
880 return;
881 gss->sns_nsvc->sns_only = true;
882 }
883
Alexander Couzense769f522020-12-07 07:37:07 +0100884
Alexander Couzens6a161492020-07-12 13:45:50 +0200885 if (gss->num_max_ip4_remote > 0)
886 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->num_max_ip4_remote, -1);
887 else
888 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->num_max_ip6_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200889}
890
891static void ns2_sns_st_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
892{
893 struct tlv_parsed *tp = NULL;
Alexander Couzens3df58862021-02-05 17:18:08 +0100894 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6a161492020-07-12 13:45:50 +0200895
896 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100897 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200898 tp = (struct tlv_parsed *) data;
899 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
900 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
901 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
902 /* TODO: What to do? */
903 } else {
Alexander Couzens3df58862021-02-05 17:18:08 +0100904 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_SGSN, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 3);
Alexander Couzens6a161492020-07-12 13:45:50 +0200905 }
906 break;
907 default:
908 OSMO_ASSERT(0);
909 }
910}
911
912static void ns2_sns_st_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
913{
914 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100915
916 if (old_state != GPRS_SNS_ST_CONFIG_BSS)
917 gss->N = 0;
918
Alexander Couzens6a161492020-07-12 13:45:50 +0200919 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200920 switch (gss->ip) {
921 case IPv4:
922 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100923 gss->ip4_local, gss->num_ip4_local,
924 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200925 break;
926 case IPv6:
927 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100928 NULL, 0,
929 gss->ip6_local, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200930 break;
931 }
932}
933
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100934/* calculate the timeout of the configured state. the configured
935 * state will fail if not at least one NS-VC is alive within X second.
936 */
937static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
938{
939 int secs;
940 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
941 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
942 secs += nsi->timeout[NS_TOUT_TNS_TEST];
943
944 return secs;
945}
Alexander Couzens6a161492020-07-12 13:45:50 +0200946
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100947/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
948static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200949{
950 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200951
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100952 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
953 const struct gprs_ns_ie_ip4_elem *v4_list;
954 unsigned int num_v4;
955 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
956 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +0200957
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100958 if (num_v4 && gss->ip6_remote)
959 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +0200960
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100961 /* realloc to the new size */
962 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote,
963 struct gprs_ns_ie_ip4_elem,
964 gss->num_ip4_remote + num_v4);
965 /* append the new entries to the end of the list */
966 memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list));
967 gss->num_ip4_remote += num_v4;
968
969 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
970 gss->num_ip4_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200971 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200972
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100973 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
974 const struct gprs_ns_ie_ip6_elem *v6_list;
975 unsigned int num_v6;
976 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
977 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
978
979 if (num_v6 && gss->ip4_remote)
980 return -NS_CAUSE_INVAL_NR_IPv6_EP;
981
982 /* realloc to the new size */
983 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote,
984 struct gprs_ns_ie_ip6_elem,
985 gss->num_ip6_remote + num_v6);
986 /* append the new entries to the end of the list */
987 memcpy(&gss->ip6_remote[gss->num_ip6_remote], v6_list, num_v6*sizeof(*v6_list));
988 gss->num_ip6_remote += num_v6;
989
990 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %u entries\n",
991 gss->num_ip6_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200992 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200993
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100994 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200995}
996
Alexander Couzens790a9632021-02-05 17:18:39 +0100997static void ns2_sns_st_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
998{
999 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1000
1001 if (old_state != GPRS_SNS_ST_CONFIG_SGSN)
1002 gss->N = 0;
1003}
1004
Alexander Couzens6a161492020-07-12 13:45:50 +02001005static void ns2_sns_st_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1006{
1007 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001008 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1009 uint8_t cause;
1010 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001011
1012 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001013 case GPRS_SNS_EV_RX_CONFIG_END:
1014 case GPRS_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001015 rc = ns_sns_append_remote_eps(fi, data);
1016 if (rc < 0) {
1017 cause = -rc;
1018 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1019 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1020 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001021 }
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001022 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1023 /* check if sum of data / sig weights == 0 */
1024 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1025 cause = NS_CAUSE_INVAL_WEIGH;
1026 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1027 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1028 return;
1029 }
1030 create_missing_nsvcs(fi);
1031 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1032 /* start the test procedure on ALL NSVCs! */
1033 gprs_ns2_start_alive_all_nsvcs(nse);
1034 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1035 } else {
1036 /* just send CONFIG-ACK */
1037 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1038 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001039 }
1040 break;
1041 default:
1042 OSMO_ASSERT(0);
1043 }
1044}
1045
Alexander Couzens67725e22021-02-15 02:37:03 +01001046/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001047static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1048 struct ns2_sns_state *gss,
1049 struct tlv_parsed *tp)
1050{
1051 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1052 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1053 int num_v4 = 0, num_v6 = 0;
1054 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001055 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001056 int rc = 0;
1057
1058 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1059 * check uniqueness within the lists (no doublicate entries)
1060 * check not-known-by-us and sent back a list of unknown/known values
1061 * (abnormal behaviour according to 48.016)
1062 */
1063
1064 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1065 if (gss->ip == IPv4) {
1066 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1067 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1068 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1069 return;
1070 }
1071
1072 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1073 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001074 for (i = 0; i < num_v4; i++) {
1075 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001076 rc = do_sns_add(fi, &v4_list[i], NULL);
1077 if (rc < 0) {
1078 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001079 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001080 do_sns_delete(fi, &v4_list[j], NULL);
1081 cause = -rc;
1082 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1083 break;
1084 }
1085 }
1086 } else { /* IPv6 */
1087 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1088 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1089 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1090 return;
1091 }
1092
1093 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1094 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001095 for (i = 0; i < num_v6; i++) {
1096 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001097 rc = do_sns_add(fi, NULL, &v6_list[i]);
1098 if (rc < 0) {
1099 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001100 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001101 do_sns_delete(fi, NULL, &v6_list[j]);
1102 cause = -rc;
1103 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1104 break;
1105 }
1106 }
1107 }
1108
1109 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1110 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1111}
1112
1113static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1114 struct ns2_sns_state *gss,
1115 struct tlv_parsed *tp)
1116{
1117 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1118 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1119 int num_v4 = 0, num_v6 = 0;
1120 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001121 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001122 int rc = 0;
1123
1124 /* TODO: split up delete into v4 + v6
1125 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1126 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1127 */
1128 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1129 if (gss->ip == IPv4) {
1130 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1131 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1132 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001133 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001134 rc = do_sns_delete(fi, &v4_list[i], NULL);
1135 if (rc < 0) {
1136 cause = -rc;
1137 /* continue to delete others */
1138 }
1139 }
1140 if (cause != 0xff) {
1141 /* TODO: create list of not-deleted and return it */
1142 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1143 return;
1144 }
1145
1146 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1147 /* delete all NS-VCs for given IPv4 address */
1148 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1149 struct gprs_ns_ie_ip4_elem *ip4_remote;
1150 uint32_t ip_addr = *(uint32_t *)(ie+1);
1151 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1152 cause = NS_CAUSE_UNKN_IP_ADDR;
1153 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1154 return;
1155 }
1156 /* make a copy as do_sns_delete() will change the array underneath us */
1157 ip4_remote = talloc_memdup(fi, gss->ip4_remote,
1158 gss->num_ip4_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001159 for (i = 0; i < gss->num_ip4_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001160 if (ip4_remote[i].ip_addr == ip_addr) {
1161 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1162 if (rc < 0) {
1163 cause = -rc;
1164 /* continue to delete others */
1165 }
1166 }
1167 }
1168 talloc_free(ip4_remote);
1169 if (cause != 0xff) {
1170 /* TODO: create list of not-deleted and return it */
1171 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1172 return;
1173 }
1174 } else {
1175 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1176 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1177 return;
1178 }
1179 } else { /* IPv6 */
1180 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1181 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1182 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001183 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001184 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1185 if (rc < 0) {
1186 cause = -rc;
1187 /* continue to delete others */
1188 }
1189 }
1190 if (cause != 0xff) {
1191 /* TODO: create list of not-deleted and return it */
1192 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1193 return;
1194 }
1195 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1196 /* delete all NS-VCs for given IPv4 address */
1197 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1198 struct gprs_ns_ie_ip6_elem *ip6_remote;
1199 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001200 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001201 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1202 cause = NS_CAUSE_UNKN_IP_ADDR;
1203 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1204 return;
1205 }
1206 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1207 /* make a copy as do_sns_delete() will change the array underneath us */
1208 ip6_remote = talloc_memdup(fi, gss->ip6_remote,
1209 gss->num_ip6_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001210 for (i = 0; i < gss->num_ip6_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001211 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1212 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1213 if (rc < 0) {
1214 cause = -rc;
1215 /* continue to delete others */
1216 }
1217 }
1218 }
1219
1220 talloc_free(ip6_remote);
1221 if (cause != 0xff) {
1222 /* TODO: create list of not-deleted and return it */
1223 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1224 return;
1225 }
1226 } else {
1227 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1228 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1229 return;
1230 }
1231 }
1232 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1233}
1234
1235static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1236 struct ns2_sns_state *gss,
1237 struct tlv_parsed *tp)
1238{
1239 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1240 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1241 int num_v4 = 0, num_v6 = 0;
1242 uint8_t trans_id, cause = 0xff;
1243 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001244 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001245
1246 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1247 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1248 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1249 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001250 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001251 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1252 if (rc < 0) {
1253 cause = -rc;
1254 /* continue to others */
1255 }
1256 }
1257 if (cause != 0xff) {
1258 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1259 return;
1260 }
1261 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1262 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1263 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001264 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001265 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1266 if (rc < 0) {
1267 cause = -rc;
1268 /* continue to others */
1269 }
1270 }
1271 if (cause != 0xff) {
1272 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1273 return;
1274 }
1275 } else {
1276 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1277 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1278 return;
1279 }
1280 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1281}
1282
1283static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1284{
1285 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1286 struct tlv_parsed *tp = data;
1287
1288 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001289 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001290 ns2_sns_st_configured_add(fi, gss, tp);
1291 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001292 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001293 ns2_sns_st_configured_delete(fi, gss, tp);
1294 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001295 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001296 ns2_sns_st_configured_change(fi, gss, tp);
1297 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001298 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001299 osmo_timer_del(&fi->timer);
1300 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001301 }
1302}
1303
1304static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1305{
1306 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens138b96f2021-01-25 16:23:29 +01001307 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001308}
1309
1310static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1311 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001312 .in_event_mask = 0, /* handled by all_state_action */
Alexander Couzens6a161492020-07-12 13:45:50 +02001313 .out_state_mask = S(GPRS_SNS_ST_SIZE),
1314 .name = "UNCONFIGURED",
1315 .action = ns2_sns_st_unconfigured,
1316 },
1317 [GPRS_SNS_ST_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001318 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001319 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1320 S(GPRS_SNS_ST_SIZE) |
1321 S(GPRS_SNS_ST_CONFIG_BSS),
1322 .name = "SIZE",
1323 .action = ns2_sns_st_size,
1324 .onenter = ns2_sns_st_size_onenter,
1325 },
1326 [GPRS_SNS_ST_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001327 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001328 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1329 S(GPRS_SNS_ST_CONFIG_BSS) |
1330 S(GPRS_SNS_ST_CONFIG_SGSN) |
1331 S(GPRS_SNS_ST_SIZE),
1332 .name = "CONFIG_BSS",
1333 .action = ns2_sns_st_config_bss,
1334 .onenter = ns2_sns_st_config_bss_onenter,
1335 },
1336 [GPRS_SNS_ST_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001337 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1338 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001339 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1340 S(GPRS_SNS_ST_CONFIG_SGSN) |
1341 S(GPRS_SNS_ST_CONFIGURED) |
1342 S(GPRS_SNS_ST_SIZE),
1343 .name = "CONFIG_SGSN",
1344 .action = ns2_sns_st_config_sgsn,
Alexander Couzens790a9632021-02-05 17:18:39 +01001345 .onenter = ns2_sns_st_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001346 },
1347 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001348 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1349 S(GPRS_SNS_EV_RX_DELETE) |
1350 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1351 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001352 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1353 S(GPRS_SNS_ST_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001354 .name = "CONFIGURED",
1355 .action = ns2_sns_st_configured,
1356 .onenter = ns2_sns_st_configured_onenter,
1357 },
1358};
1359
1360static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1361{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001362 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001363 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1364 struct gprs_ns2_inst *nsi = nse->nsi;
1365
Alexander Couzens90ee9632020-12-07 06:18:32 +01001366 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001367 switch (fi->T) {
1368 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001369 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1370 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001371 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001372 } else {
Alexander Couzenscc65a252020-12-21 14:03:58 +01001373 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nsi->timeout[NS_TOUT_TSNS_PROV], 1);
Alexander Couzensa367d082020-12-21 14:06:24 +01001374 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001375 break;
1376 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001377 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001378 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 +01001379 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001380 } else {
Alexander Couzenscc65a252020-12-21 14:03:58 +01001381 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_BSS, nsi->timeout[NS_TOUT_TSNS_PROV], 2);
Alexander Couzensa367d082020-12-21 14:06:24 +01001382 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001383 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001384 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001385 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1386 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 +01001387 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001388 } else {
1389 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIG_SGSN, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
1390 }
1391 break;
1392 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001393 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 +01001394 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001395 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001396 }
1397 return 0;
1398}
1399
Harald Welte9e37bf42021-03-02 20:48:31 +01001400/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001401static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1402{
1403 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001404 struct ns2_sns_bind *sbind;
1405 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001406
Alexander Couzense769f522020-12-07 07:37:07 +01001407 switch (event) {
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001408 case GPRS_SNS_EV_REQ_ADD_BIND:
1409 sbind = data;
1410 switch (fi->state) {
1411 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001412 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001413 break;
1414 case GPRS_SNS_ST_SIZE:
1415 /* TODO: add the ip4 element to the list */
1416 break;
1417 case GPRS_SNS_ST_CONFIG_BSS:
1418 case GPRS_SNS_ST_CONFIG_SGSN:
1419 case GPRS_SNS_ST_CONFIGURED:
1420 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1421 break;
1422 }
1423 break;
1424 case GPRS_SNS_EV_REQ_DELETE_BIND:
1425 sbind = data;
1426 switch (fi->state) {
1427 case GPRS_SNS_ST_UNCONFIGURED:
1428 break;
1429 case GPRS_SNS_ST_SIZE:
1430 /* TODO: remove the ip4 element from the list */
1431 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1432 if (nsvc->bind == sbind->bind) {
1433 gprs_ns2_free_nsvc(nsvc);
1434 }
1435 }
1436 break;
1437 case GPRS_SNS_ST_CONFIG_BSS:
1438 case GPRS_SNS_ST_CONFIG_SGSN:
1439 case GPRS_SNS_ST_CONFIGURED:
1440 /* TODO: do an delete SNS-IP procedure */
1441 /* TODO: remove the ip4 element to the list */
1442 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1443 if (nsvc->bind == sbind->bind) {
1444 gprs_ns2_free_nsvc(nsvc);
1445 }
1446 }
1447 break;
1448 }
1449 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1450 talloc_free(sbind);
1451 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001452 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001453}
1454
Harald Welte9e37bf42021-03-02 20:48:31 +01001455/* allstate-action for BSS role */
1456static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1457{
1458 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1459 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1460
1461 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
1462 switch (event) {
1463 case GPRS_SNS_EV_REQ_NO_NSVC:
1464 /* ignore reselection running */
1465 if (gss->reselection_running)
1466 break;
1467
1468 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1469 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1470 break;
1471 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
1472 /* tear down previous state
1473 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1474 gss->reselection_running = true;
1475 gprs_ns2_free_nsvcs(nse);
1476 ns2_clear_ipv46_entries(gss);
1477
1478 /* Choose the next sns endpoint. */
1479 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds)) {
1480 gss->initial = NULL;
1481 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1482 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1483 return;
1484 } else if (!gss->initial) {
1485 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1486 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1487 /* last entry, continue with first */
1488 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1489 } else {
1490 /* next element is an entry */
1491 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1492 }
1493
1494 gss->reselection_running = false;
1495 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SIZE, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 1);
1496 break;
1497 default:
1498 ns2_sns_st_all_action(fi, event, data);
1499 break;
1500 }
1501}
1502
Alexander Couzens6a161492020-07-12 13:45:50 +02001503static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1504 .name = "GPRS-NS2-SNS-BSS",
1505 .states = ns2_sns_bss_states,
1506 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001507 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1508 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001509 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1510 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001511 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001512 .cleanup = NULL,
1513 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001514 .event_names = gprs_sns_event_names,
1515 .pre_term = NULL,
1516 .log_subsys = DLNS,
1517};
1518
Harald Welte5bef2cc2020-09-18 22:33:24 +02001519/*! Allocate an IP-SNS FSM for the BSS side.
1520 * \param[in] nse NS Entity in which the FSM runs
1521 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001522 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001523struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1524 const char *id)
1525{
1526 struct osmo_fsm_inst *fi;
1527 struct ns2_sns_state *gss;
1528
1529 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1530 if (!fi)
1531 return fi;
1532
1533 gss = talloc_zero(fi, struct ns2_sns_state);
1534 if (!gss)
1535 goto err;
1536
1537 fi->priv = gss;
1538 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001539 gss->role = GPRS_SNS_ROLE_BSS;
Alexander Couzense769f522020-12-07 07:37:07 +01001540 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001541 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001542
1543 return fi;
1544err:
1545 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1546 return NULL;
1547}
1548
Harald Welte5bef2cc2020-09-18 22:33:24 +02001549/*! main entry point for receiving SNS messages from the network.
1550 * \param[in] nsvc NS-VC on which the message was received
1551 * \param[in] msg message buffer of the IP-SNS message
1552 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001553 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001554int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001555{
1556 struct gprs_ns2_nse *nse = nsvc->nse;
1557 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1558 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001559 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001560 struct osmo_fsm_inst *fi;
1561
1562 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001563 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1564 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001565 return -EINVAL;
1566 }
1567
Alexander Couzens6a161492020-07-12 13:45:50 +02001568 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1569 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001570 gss = (struct ns2_sns_state *) fi->priv;
1571 if (!gss->sns_nsvc)
1572 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001573
Harald Weltef2949742021-01-20 14:54:14 +01001574 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1575 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1576
Alexander Couzens6a161492020-07-12 13:45:50 +02001577 switch (nsh->pdu_type) {
1578 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001579 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001580 break;
1581 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001582 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001583 break;
1584 case SNS_PDUT_CONFIG:
1585 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001586 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001587 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001588 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001589 break;
1590 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001591 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001592 break;
1593 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001594 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001595 break;
1596 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001597 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001598 break;
1599 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001600 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001601 break;
1602 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001603 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001604 break;
1605 default:
Harald Weltef2949742021-01-20 14:54:14 +01001606 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1607 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001608 return -EINVAL;
1609 }
1610
1611 return 0;
1612}
1613
1614#include <osmocom/vty/vty.h>
1615#include <osmocom/vty/misc.h>
1616
Harald Welte1262c4f2021-01-19 20:58:33 +01001617static 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 +02001618{
1619 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001620 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001621 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1622}
1623
Harald Welte1262c4f2021-01-19 20:58:33 +01001624static 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 +02001625{
1626 char ip_addr[INET6_ADDRSTRLEN] = {};
1627 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1628 strcpy(ip_addr, "Invalid IPv6");
1629
Harald Welte1262c4f2021-01-19 20:58:33 +01001630 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001631 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1632}
1633
Harald Welte5bef2cc2020-09-18 22:33:24 +02001634/*! Dump the IP-SNS state to a vty.
1635 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001636 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001637 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1638 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001639void 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 +02001640{
1641 struct ns2_sns_state *gss;
1642 unsigned int i;
1643
1644 if (!nse->bss_sns_fi)
1645 return;
1646
Harald Welte1262c4f2021-01-19 20:58:33 +01001647 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001648 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1649
Harald Welte1262c4f2021-01-19 20:58:33 +01001650 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1651 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001652
1653 if (gss->num_ip4_local && gss->num_ip4_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001654 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001655 for (i = 0; i < gss->num_ip4_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001656 vty_dump_sns_ip4(vty, prefix, &gss->ip4_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001657
Harald Welte1262c4f2021-01-19 20:58:33 +01001658 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001659 for (i = 0; i < gss->num_ip4_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001660 vty_dump_sns_ip4(vty, prefix, &gss->ip4_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001661 }
1662
1663 if (gss->num_ip6_local && gss->num_ip6_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001664 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001665 for (i = 0; i < gss->num_ip6_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001666 vty_dump_sns_ip6(vty, prefix, &gss->ip6_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001667
Harald Welte1262c4f2021-01-19 20:58:33 +01001668 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001669 for (i = 0; i < gss->num_ip6_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001670 vty_dump_sns_ip6(vty, prefix, &gss->ip6_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001671 }
1672}
1673
Alexander Couzens412bc342020-11-19 05:24:37 +01001674/*! write IP-SNS to a vty
1675 * \param[in] vty VTY to which the state shall be printed
1676 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001677void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001678{
1679 struct ns2_sns_state *gss;
1680 struct osmo_sockaddr_str addr_str;
1681 struct sns_endpoint *endpoint;
1682
1683 if (!nse->bss_sns_fi)
1684 return;
1685
1686 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1687 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001688 /* It's unlikely that an error happens, but let's better be safe. */
1689 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1690 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001691 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001692 }
1693}
1694
Alexander Couzense769f522020-12-07 07:37:07 +01001695static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1696 const struct osmo_sockaddr *saddr)
1697{
1698 struct sns_endpoint *endpoint;
1699
1700 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1701 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1702 return endpoint;
1703 }
1704
1705 return NULL;
1706}
1707
1708/*! gprs_ns2_sns_add_endpoint
1709 * \param[in] nse
1710 * \param[in] sockaddr
1711 * \return
1712 */
1713int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1714 const struct osmo_sockaddr *saddr)
1715{
1716 struct ns2_sns_state *gss;
1717 struct sns_endpoint *endpoint;
1718 bool do_selection = false;
1719
1720 if (nse->ll != GPRS_NS2_LL_UDP) {
1721 return -EINVAL;
1722 }
1723
Alexander Couzens138b96f2021-01-25 16:23:29 +01001724 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001725 return -EINVAL;
1726 }
1727
1728 gss = nse->bss_sns_fi->priv;
1729
1730 if (ns2_get_sns_endpoint(gss, saddr))
1731 return -EADDRINUSE;
1732
1733 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1734 if (!endpoint)
1735 return -ENOMEM;
1736
1737 endpoint->saddr = *saddr;
1738 if (llist_empty(&gss->sns_endpoints))
1739 do_selection = true;
1740
1741 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1742 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001743 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001744
1745 return 0;
1746}
1747
1748/*! gprs_ns2_sns_del_endpoint
1749 * \param[in] nse
1750 * \param[in] sockaddr
1751 * \return 0 on success, otherwise < 0
1752 */
1753int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1754 const struct osmo_sockaddr *saddr)
1755{
1756 struct ns2_sns_state *gss;
1757 struct sns_endpoint *endpoint;
1758
1759 if (nse->ll != GPRS_NS2_LL_UDP) {
1760 return -EINVAL;
1761 }
1762
Alexander Couzens138b96f2021-01-25 16:23:29 +01001763 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001764 return -EINVAL;
1765 }
1766
1767 gss = nse->bss_sns_fi->priv;
1768 endpoint = ns2_get_sns_endpoint(gss, saddr);
1769 if (!endpoint)
1770 return -ENOENT;
1771
1772 /* if this is an unused SNS endpoint it's done */
1773 if (gss->initial != endpoint) {
1774 llist_del(&endpoint->list);
1775 talloc_free(endpoint);
1776 return 0;
1777 }
1778
Alexander Couzens67725e22021-02-15 02:37:03 +01001779 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001780 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001781 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001782 "Closing all NS-VC and restart SNS-SIZE procedure"
1783 "with a remaining SNS endpoint.\n");
1784
1785 /* Continue with the next endpoint in the list.
1786 * Special case if the endpoint is at the start or end of the list */
1787 if (endpoint->list.prev == &gss->sns_endpoints ||
1788 endpoint->list.next == &gss->sns_endpoints)
1789 gss->initial = NULL;
1790 else
1791 gss->initial = llist_entry(endpoint->list.next->prev,
1792 struct sns_endpoint,
1793 list);
1794
1795 llist_del(&endpoint->list);
1796 gprs_ns2_free_nsvcs(nse);
1797 talloc_free(endpoint);
1798
1799 return 0;
1800}
1801
1802/*! gprs_ns2_sns_count
1803 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1804 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1805 */
1806int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1807{
1808 struct ns2_sns_state *gss;
1809 struct sns_endpoint *endpoint;
1810 int count = 0;
1811
1812 if (nse->ll != GPRS_NS2_LL_UDP) {
1813 return -EINVAL;
1814 }
1815
Alexander Couzens138b96f2021-01-25 16:23:29 +01001816 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001817 return -EINVAL;
1818 }
1819
1820 gss = nse->bss_sns_fi->priv;
1821 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1822 count++;
1823
1824 return count;
1825}
1826
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001827void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1828{
1829 struct ns2_sns_state *gss;
1830 struct gprs_ns2_vc *tmp;
1831
1832 if (!nse->bss_sns_fi)
1833 return;
1834
1835 gss = nse->bss_sns_fi->priv;
1836 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1837 return;
1838
1839 if (alive == gss->alive)
1840 return;
1841
1842 /* check if this is the current SNS NS-VC */
1843 if (nsvc == gss->sns_nsvc) {
1844 /* only replace the SNS NS-VC if there are other alive NS-VC.
1845 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1846 * and couldn't confirm yet if the NS-VC comes up */
1847 if (gss->alive && !alive)
1848 ns2_sns_replace_nsvc(nsvc);
1849 }
1850
1851 if (alive) {
1852 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001853 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001854 } else {
1855 /* is there at least another alive nsvc? */
1856 llist_for_each_entry(tmp, &nse->nsvc, list) {
1857 if (ns2_vc_is_unblocked(tmp))
1858 return;
1859 }
1860
1861 /* all NS-VC have failed */
1862 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001863 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001864 }
1865}
1866
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001867int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1868 struct gprs_ns2_vc_bind *bind)
1869{
1870 struct ns2_sns_state *gss;
1871 struct ns2_sns_bind *tmp;
1872
1873 OSMO_ASSERT(nse->bss_sns_fi);
1874 gss = nse->bss_sns_fi->priv;
1875
1876 if (!gprs_ns2_is_ip_bind(bind)) {
1877 return -EINVAL;
1878 }
1879
1880 if (!llist_empty(&gss->binds)) {
1881 llist_for_each_entry(tmp, &gss->binds, list) {
1882 if (tmp->bind == bind)
1883 return -EALREADY;
1884 }
1885 }
1886
1887 tmp = talloc_zero(gss, struct ns2_sns_bind);
1888 if (!tmp)
1889 return -ENOMEM;
1890 tmp->bind = bind;
1891 llist_add_tail(&tmp->list, &gss->binds);
1892
1893 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
1894 return 0;
1895}
1896
1897/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
1898int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
1899 struct gprs_ns2_vc_bind *bind)
1900{
1901 struct ns2_sns_state *gss;
1902 struct ns2_sns_bind *tmp, *tmp2;
1903 bool found = false;
1904
1905 if (!nse->bss_sns_fi)
1906 return -EINVAL;
1907
1908 gss = nse->bss_sns_fi->priv;
1909 if (gss->initial_bind && gss->initial_bind->bind == bind) {
1910 if (gss->initial_bind->list.prev == &gss->binds)
1911 gss->initial_bind = NULL;
1912 else
1913 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
1914 }
1915
1916 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
1917 if (tmp->bind == bind) {
1918 llist_del(&tmp->list);
1919 found = true;
1920 }
1921 }
1922
1923 if (!found)
1924 return -ENOENT;
1925
1926 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
1927 return 0;
1928}
1929
Alexander Couzensc4704762021-02-08 23:13:12 +01001930/* Update SNS weights
1931 * \param[in] nsvc the NSVC which should be updated
1932 */
1933void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
1934{
1935 /* TODO: implement weights after binds per sns implemented */
1936}
1937
Harald Welte4f127462021-03-02 20:49:10 +01001938
1939
1940
1941/***********************************************************************
1942 * SGSN role
1943 ***********************************************************************/
1944
1945static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1946{
1947 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1948 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
1949 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
1950}
1951
1952/* We're waiting for inbound SNS-CONFIG from the BSS */
1953static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1954{
1955 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1956 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1957 struct gprs_ns2_inst *nsi = nse->nsi;
1958 uint8_t cause;
1959 int rc;
1960
1961 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
1962
1963 switch (event) {
1964 case GPRS_SNS_EV_RX_CONFIG:
1965 case GPRS_SNS_EV_RX_CONFIG_END:
1966 rc = ns_sns_append_remote_eps(fi, data);
1967 if (rc < 0) {
1968 cause = -rc;
1969 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1970 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1971 return;
1972 }
1973 /* only change state if last CONFIG was received */
1974 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1975 /* ensure sum of data weight / sig weights is > 0 */
1976 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1977 cause = NS_CAUSE_INVAL_WEIGH;
1978 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1979 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1980 break;
1981 }
1982 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1983 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
1984 } else {
1985 /* just send CONFIG-ACK */
1986 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1987 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
1988 }
1989 break;
1990 }
1991}
1992
1993static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1994{
1995 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1996 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
1997
1998 ns2_sns_compute_local_ep_from_binds(fi);
1999 /* transmit SGSN-oriented SNS-CONFIG */
2000 ns2_tx_sns_config(gss->sns_nsvc, true, gss->ip4_local, gss->num_ip4_local,
2001 gss->ip6_local, gss->num_ip6_local);
2002}
2003
2004/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2005static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2006{
2007 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2008 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2009 struct tlv_parsed *tp = NULL;
2010
2011 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2012
2013 switch (event) {
2014 case GPRS_SNS_EV_RX_CONFIG_ACK:
2015 tp = data;
2016 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2017 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2018 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2019 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2020 break;
2021 }
2022 /* we currently only send one SNS-CONFIG with END FLAG */
2023 if (true) {
2024 create_missing_nsvcs(fi);
2025 /* start the test procedure on ALL NSVCs! */
2026 gprs_ns2_start_alive_all_nsvcs(nse);
2027 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2028 }
2029 break;
2030 }
2031}
2032
2033/* SGSN-side SNS state machine */
2034static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2035 [GPRS_SNS_ST_UNCONFIGURED] = {
2036 .in_event_mask = 0, /* handled by all_state_action */
2037 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2038 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2039 .name = "UNCONFIGURED",
2040 .action = ns2_sns_st_sgsn_unconfigured,
2041 },
2042 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
2043 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
2044 S(GPRS_SNS_EV_RX_CONFIG_END),
2045 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2046 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2047 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2048 .name = "SGSN_WAIT_CONFIG",
2049 .action = ns2_sns_st_sgsn_wait_config,
2050 },
2051 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
2052 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
2053 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2054 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2055 S(GPRS_SNS_ST_CONFIGURED),
2056 .name = "SGSN_WAIT_CONFIG_ACK",
2057 .action = ns2_sns_st_sgsn_wait_config_ack,
2058 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2059 },
2060 [GPRS_SNS_ST_CONFIGURED] = {
2061 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
2062 S(GPRS_SNS_EV_RX_DELETE) |
2063 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
2064 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
2065 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2066 .name = "CONFIGURED",
2067 /* shared with BSS side; once configured there's no difference */
2068 .action = ns2_sns_st_configured,
2069 .onenter = ns2_sns_st_configured_onenter,
2070 },
2071};
2072
2073static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2074{
2075 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2076 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2077 struct gprs_ns2_inst *nsi = nse->nsi;
2078
2079 gss->N++;
2080 switch (fi->T) {
2081 case 3:
2082 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2083 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2084 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2085 } else {
2086 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2087 }
2088 break;
2089 case 4:
2090 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2091 break;
2092 }
2093 return 0;
2094}
2095
2096
2097/* allstate-action for SGSN role */
2098static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2099{
2100 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2101 struct tlv_parsed *tp = NULL;
2102 uint8_t flag;
2103
2104 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2105
2106 switch (event) {
2107 case GPRS_SNS_EV_RX_SIZE:
2108 tp = (struct tlv_parsed *) data;
2109 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1)) {
2110 uint8_t cause = NS_CAUSE_MISSING_ESSENT_IE;
2111 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2112 break;
2113 }
2114 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2115 if (flag & 1) {
2116 struct gprs_ns2_vc *nsvc, *nsvc2;
2117 /* clear all state */
2118 gss->N = 0;
2119 ns2_clear_ipv46_entries(gss);
2120 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2121 if (nsvc == gss->sns_nsvc) {
2122 /* keep the NSVC we need for SNS, but unconfigure it */
2123 nsvc->sig_weight = 0;
2124 nsvc->data_weight = 0;
2125 ns2_vc_force_unconfigured(nsvc);
2126 } else {
2127 /* free all other NS-VCs */
2128 gprs_ns2_free_nsvc(nsvc);
2129 }
2130 }
2131 }
2132 /* send SIZE_ACK */
2133 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2134 /* only wait for SNS-CONFIG in case of Reset flag */
2135 if (flag & 1)
2136 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2137 break;
2138 default:
2139 ns2_sns_st_all_action(fi, event, data);
2140 break;
2141 }
2142}
2143
2144static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2145 .name = "GPRS-NS2-SNS-SGSN",
2146 .states = ns2_sns_sgsn_states,
2147 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
2148 .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) |
2149 S(GPRS_SNS_EV_REQ_NO_NSVC) |
2150 S(GPRS_SNS_EV_REQ_ADD_BIND) |
2151 S(GPRS_SNS_EV_REQ_DELETE_BIND),
2152 .allstate_action = ns2_sns_st_all_action_sgsn,
2153 .cleanup = NULL,
2154 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2155 .event_names = gprs_sns_event_names,
2156 .pre_term = NULL,
2157 .log_subsys = DLNS,
2158};
2159
2160/*! Allocate an IP-SNS FSM for the SGSN side.
2161 * \param[in] nse NS Entity in which the FSM runs
2162 * \param[in] id string identifier
2163 * \returns FSM instance on success; NULL on error */
2164struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2165{
2166 struct osmo_fsm_inst *fi;
2167 struct ns2_sns_state *gss;
2168
2169 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2170 if (!fi)
2171 return fi;
2172
2173 gss = talloc_zero(fi, struct ns2_sns_state);
2174 if (!gss)
2175 goto err;
2176
2177 fi->priv = gss;
2178 gss->nse = nse;
2179 gss->role = GPRS_SNS_ROLE_SGSN;
2180 INIT_LLIST_HEAD(&gss->sns_endpoints);
2181 INIT_LLIST_HEAD(&gss->binds);
2182
2183 return fi;
2184err:
2185 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2186 return NULL;
2187}
2188
2189
2190
2191
Alexander Couzens6a161492020-07-12 13:45:50 +02002192/* initialize osmo_ctx on main tread */
2193static __attribute__((constructor)) void on_dso_load_ctx(void)
2194{
2195 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002196 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002197}