blob: 2370f01c654f62977d19cd9d3a81e8261c5b31c9 [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \file gprs_ns2_sns.c
2 * NS Sub-Network Service Protocol implementation
3 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
4 * as well as its successor 3GPP TS 48.016 */
5
Harald Weltec1c7e4a2021-03-02 20:47:29 +01006/* (C) 2018-2021 by Harald Welte <laforge@gnumonks.org>
Alexander Couzens6a161492020-07-12 13:45:50 +02007 * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
8 * Author: Alexander Couzens <lynxis@fe80.eu>
9 *
10 * All Rights Reserved
11 *
12 * SPDX-License-Identifier: GPL-2.0+
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 *
27 */
28
29/* The BSS NSE only has one SGSN IP address configured, and it will use the SNS procedures
30 * to communicated its local IPs/ports as well as all the SGSN side IPs/ports and
31 * associated weights. The BSS then uses this to establish a full mesh
32 * of NSVCs between all BSS-side IPs/ports and SGSN-side IPs/ports.
33 *
34 * Known limitation/expectation/bugs:
35 * - No concurrent dual stack. It supports either IPv4 or IPv6, but not both at the same time.
36 * - SNS Add/Change/Delete: Doesn't answer on the same NSVC as received SNS ADD/CHANGE/DELETE PDUs.
37 * - SNS Add/Change/Delete: Doesn't communicated the failed IPv4/IPv6 entries on the SNS_ACK.
38 */
39
40#include <errno.h>
41
42#include <netinet/in.h>
43#include <arpa/inet.h>
44#include <stdint.h>
45
46#include <osmocom/core/fsm.h>
47#include <osmocom/core/msgb.h>
48#include <osmocom/core/socket.h>
Alexander Couzens412bc342020-11-19 05:24:37 +010049#include <osmocom/core/sockaddr_str.h>
Alexander Couzens6a161492020-07-12 13:45:50 +020050#include <osmocom/gsm/tlv.h>
51#include <osmocom/gprs/gprs_msgb.h>
52#include <osmocom/gprs/gprs_ns2.h>
53#include <osmocom/gprs/protocol/gsm_08_16.h>
54
55#include "gprs_ns2_internal.h"
56
57#define S(x) (1 << (x))
58
59enum ns2_sns_type {
60 IPv4,
61 IPv6,
62};
63
Harald Welte4f127462021-03-02 20:49:10 +010064enum ns2_sns_role {
65 GPRS_SNS_ROLE_BSS,
66 GPRS_SNS_ROLE_SGSN,
67};
68
Harald Welte694dad52021-03-23 15:22:16 +010069/* BSS-side-only states _ST_BSS_; SGSN-side only states _ST_SGSN_; others shared */
Alexander Couzens6a161492020-07-12 13:45:50 +020070enum gprs_sns_bss_state {
71 GPRS_SNS_ST_UNCONFIGURED,
Harald Welte694dad52021-03-23 15:22:16 +010072 GPRS_SNS_ST_BSS_SIZE, /*!< SNS-SIZE procedure ongoing */
73 GPRS_SNS_ST_BSS_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */
74 GPRS_SNS_ST_BSS_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */
Alexander Couzens6a161492020-07-12 13:45:50 +020075 GPRS_SNS_ST_CONFIGURED,
Harald Welte4f127462021-03-02 20:49:10 +010076 GPRS_SNS_ST_SGSN_WAIT_CONFIG, /* !< SGSN role: Wait for CONFIG from BSS */
77 GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, /* !< SGSN role: Wait for CONFIG-ACK from BSS */
Alexander Couzens6a161492020-07-12 13:45:50 +020078};
79
80enum gprs_sns_event {
Alexander Couzens67725e22021-02-15 02:37:03 +010081 GPRS_SNS_EV_REQ_SELECT_ENDPOINT, /*!< Select a SNS endpoint from the list */
82 GPRS_SNS_EV_RX_SIZE,
83 GPRS_SNS_EV_RX_SIZE_ACK,
84 GPRS_SNS_EV_RX_CONFIG,
85 GPRS_SNS_EV_RX_CONFIG_END, /*!< SNS-CONFIG with end flag received */
86 GPRS_SNS_EV_RX_CONFIG_ACK,
87 GPRS_SNS_EV_RX_ADD,
88 GPRS_SNS_EV_RX_DELETE,
89 GPRS_SNS_EV_RX_CHANGE_WEIGHT,
Harald Welteb9f23872021-03-02 20:48:54 +010090 GPRS_SNS_EV_RX_ACK, /*!< Rx of SNS-ACK (response to ADD/DELETE/CHG_WEIGHT */
Harald Welte04647e12021-03-02 18:50:40 +010091 GPRS_SNS_EV_REQ_NO_NSVC, /*!< no more NS-VC remaining (all dead) */
Alexander Couzens67725e22021-02-15 02:37:03 +010092 GPRS_SNS_EV_REQ_NSVC_ALIVE, /*!< a NS-VC became alive */
Harald Welte04647e12021-03-02 18:50:40 +010093 GPRS_SNS_EV_REQ_ADD_BIND, /*!< add a new local bind to this NSE */
94 GPRS_SNS_EV_REQ_DELETE_BIND, /*!< remove a local bind from this NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +020095};
96
97static const struct value_string gprs_sns_event_names[] = {
Alexander Couzens67725e22021-02-15 02:37:03 +010098 { GPRS_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" },
99 { GPRS_SNS_EV_RX_SIZE, "RX_SIZE" },
100 { GPRS_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" },
101 { GPRS_SNS_EV_RX_CONFIG, "RX_CONFIG" },
102 { GPRS_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" },
103 { GPRS_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" },
104 { GPRS_SNS_EV_RX_ADD, "RX_ADD" },
105 { GPRS_SNS_EV_RX_DELETE, "RX_DELETE" },
Harald Welteb9f23872021-03-02 20:48:54 +0100106 { GPRS_SNS_EV_RX_ACK, "RX_ACK" },
Alexander Couzens67725e22021-02-15 02:37:03 +0100107 { GPRS_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" },
108 { GPRS_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" },
109 { GPRS_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"},
110 { GPRS_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"},
111 { GPRS_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200112 { 0, NULL }
113};
114
Alexander Couzense769f522020-12-07 07:37:07 +0100115struct sns_endpoint {
116 struct llist_head list;
117 struct osmo_sockaddr saddr;
118};
119
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100120struct ns2_sns_bind {
121 struct llist_head list;
122 struct gprs_ns2_vc_bind *bind;
123};
124
Alexander Couzens6a161492020-07-12 13:45:50 +0200125struct ns2_sns_state {
126 struct gprs_ns2_nse *nse;
127
128 enum ns2_sns_type ip;
Harald Welte4f127462021-03-02 20:49:10 +0100129 enum ns2_sns_role role; /* local role: BSS or SGSN */
Alexander Couzens6a161492020-07-12 13:45:50 +0200130
Alexander Couzense769f522020-12-07 07:37:07 +0100131 /* holds the list of initial SNS endpoints */
132 struct llist_head sns_endpoints;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100133 /* list of used struct ns2_sns_bind */
134 struct llist_head binds;
135 /* pointer to the bind which was used to initiate the SNS connection */
136 struct ns2_sns_bind *initial_bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100137 /* prevent recursive reselection */
138 bool reselection_running;
139
140 /* The current initial SNS endpoints.
141 * The initial connection will be moved into the NSE
142 * if configured via SNS. Otherwise it will be removed
143 * in configured state. */
144 struct sns_endpoint *initial;
Alexander Couzens6a161492020-07-12 13:45:50 +0200145 /* all SNS PDU will be sent over this nsvc */
146 struct gprs_ns2_vc *sns_nsvc;
Alexander Couzens90ee9632020-12-07 06:18:32 +0100147 /* timer N */
148 int N;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100149 /* true if at least one nsvc is alive */
150 bool alive;
Alexander Couzens6a161492020-07-12 13:45:50 +0200151
152 /* local configuration to send to the remote end */
153 struct gprs_ns_ie_ip4_elem *ip4_local;
154 size_t num_ip4_local;
155
156 /* local configuration to send to the remote end */
157 struct gprs_ns_ie_ip6_elem *ip6_local;
158 size_t num_ip6_local;
159
160 /* local configuration about our capabilities in terms of connections to
161 * remote (SGSN) side */
162 size_t num_max_nsvcs;
163 size_t num_max_ip4_remote;
164 size_t num_max_ip6_remote;
165
166 /* remote configuration as received */
167 struct gprs_ns_ie_ip4_elem *ip4_remote;
168 unsigned int num_ip4_remote;
169
170 /* remote configuration as received */
171 struct gprs_ns_ie_ip6_elem *ip6_remote;
172 unsigned int num_ip6_remote;
173};
174
175static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi)
176{
177 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
178 return gss->nse;
179}
180
181/* helper function to compute the sum of all (data or signaling) weights */
182static int ip4_weight_sum(const struct gprs_ns_ie_ip4_elem *ip4, unsigned int num,
183 bool data_weight)
184{
185 unsigned int i;
186 int weight_sum = 0;
187
188 for (i = 0; i < num; i++) {
189 if (data_weight)
190 weight_sum += ip4[i].data_weight;
191 else
192 weight_sum += ip4[i].sig_weight;
193 }
194 return weight_sum;
195}
196#define ip4_weight_sum_data(x,y) ip4_weight_sum(x, y, true)
197#define ip4_weight_sum_sig(x,y) ip4_weight_sum(x, y, false)
198
199/* helper function to compute the sum of all (data or signaling) weights */
200static int ip6_weight_sum(const struct gprs_ns_ie_ip6_elem *ip6, unsigned int num,
201 bool data_weight)
202{
203 unsigned int i;
204 int weight_sum = 0;
205
206 for (i = 0; i < num; i++) {
207 if (data_weight)
208 weight_sum += ip6[i].data_weight;
209 else
210 weight_sum += ip6[i].sig_weight;
211 }
212 return weight_sum;
213}
214#define ip6_weight_sum_data(x,y) ip6_weight_sum(x, y, true)
215#define ip6_weight_sum_sig(x,y) ip6_weight_sum(x, y, false)
216
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100217static int nss_weight_sum(const struct ns2_sns_state *nss, bool data_weight)
218{
219 return ip4_weight_sum(nss->ip4_remote, nss->num_ip4_remote, data_weight) +
220 ip6_weight_sum(nss->ip6_remote, nss->num_ip6_remote, data_weight);
221}
222#define nss_weight_sum_data(nss) nss_weight_sum(nss, true)
223#define nss_weight_sum_sig(nss) nss_weight_sum(nss, false)
224
Alexander Couzens6a161492020-07-12 13:45:50 +0200225static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
226 const struct gprs_ns_ie_ip4_elem *ip4)
227{
228 struct osmo_sockaddr sa;
229 /* copy over. Both data structures use network byte order */
230 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
231 sa.u.sin.sin_port = ip4->udp_port;
232 sa.u.sin.sin_family = AF_INET;
233
Alexander Couzens38b19e82020-09-23 23:56:37 +0200234 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200235}
236
237static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
238 const struct gprs_ns_ie_ip6_elem *ip6)
239{
240 struct osmo_sockaddr sa;
241 /* copy over. Both data structures use network byte order */
242 sa.u.sin6.sin6_addr = ip6->ip_addr;
243 sa.u.sin6.sin6_port = ip6->udp_port;
244 sa.u.sin6.sin6_family = AF_INET;
245
Alexander Couzens38b19e82020-09-23 23:56:37 +0200246 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200247}
248
Alexander Couzens125298f2020-10-11 21:22:42 +0200249/*! Return the initial SNS remote socket address
250 * \param nse NS Entity
251 * \return address of the initial SNS connection; NULL in case of error
252 */
253const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
254{
255 struct ns2_sns_state *gss;
256
257 if (!nse->bss_sns_fi)
258 return NULL;
259
260 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100261 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200262}
263
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100264/*! called when a nsvc is beeing freed or the nsvc became dead */
265void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200266{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100267 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200268 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100269 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200270 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200271
272 if (!fi)
273 return;
274
275 gss = (struct ns2_sns_state *) fi->priv;
276 if (nsvc != gss->sns_nsvc)
277 return;
278
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100279 gss->sns_nsvc = NULL;
280 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200281 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100282 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200283 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100284 return;
285 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200286 }
287 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100288 /* the SNS is waiting for its first NS-VC to come up
289 * choose any other nsvc */
290 llist_for_each_entry(tmp, &nse->nsvc, list) {
291 if (nsvc != tmp) {
292 gss->sns_nsvc = tmp;
293 return;
294 }
295 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200296 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100297
Alexander Couzens67725e22021-02-15 02:37:03 +0100298 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200299}
300
Harald Welte46eb7642021-03-04 17:49:59 +0100301static void ns2_clear_ipv46_entries_local(struct ns2_sns_state *gss)
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100302{
303 TALLOC_FREE(gss->ip4_local);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100304 TALLOC_FREE(gss->ip6_local);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100305
306 gss->num_ip4_local = 0;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100307 gss->num_ip6_local = 0;
Harald Welte46eb7642021-03-04 17:49:59 +0100308}
309
310static void ns2_clear_ipv46_entries_remote(struct ns2_sns_state *gss)
311{
312 TALLOC_FREE(gss->ip4_remote);
313 TALLOC_FREE(gss->ip6_remote);
314
315 gss->num_ip4_remote = 0;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100316 gss->num_ip6_remote = 0;
317}
318
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100319static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
320 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200321{
322 struct gprs_ns2_inst *nsi = nse->nsi;
323 struct gprs_ns2_vc *nsvc;
324 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100325
326 /* for every bind, create a connection if bind type == IP */
327 llist_for_each_entry(bind, &nsi->binding, list) {
328 if (bind->ll != GPRS_NS2_LL_UDP)
329 continue;
330 /* ignore failed connection */
331 nsvc = gprs_ns2_ip_connect_inactive(bind,
332 remote,
333 nse, 0);
334 if (!nsvc) {
335 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
336 continue;
337 }
338
339 nsvc->sig_weight = sig_weight;
340 nsvc->data_weight = data_weight;
341 }
342}
343
344static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
345 struct gprs_ns2_nse *nse,
346 const struct gprs_ns_ie_ip4_elem *ip4)
347{
Alexander Couzensc068d862020-10-12 04:11:51 +0200348 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200349 /* copy over. Both data structures use network byte order */
350 remote.u.sin.sin_family = AF_INET;
351 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
352 remote.u.sin.sin_port = ip4->udp_port;
353
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100354 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200355}
356
357static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
358 struct gprs_ns2_nse *nse,
359 const struct gprs_ns_ie_ip6_elem *ip6)
360{
Alexander Couzens6a161492020-07-12 13:45:50 +0200361 struct osmo_sockaddr remote = {};
362 /* copy over. Both data structures use network byte order */
363 remote.u.sin6.sin6_family = AF_INET6;
364 remote.u.sin6.sin6_addr = ip6->ip_addr;
365 remote.u.sin6.sin6_port = ip6->udp_port;
366
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100367 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200368}
369
Harald Weltee8c61062021-03-24 13:16:27 +0100370static struct gprs_ns2_vc *nsvc_for_bind_and_remote(struct gprs_ns2_nse *nse,
371 struct gprs_ns2_vc_bind *bind,
372 const struct osmo_sockaddr *remote)
373{
374 struct gprs_ns2_vc *nsvc;
375
376 llist_for_each_entry(nsvc, &nse->nsvc, list) {
377 if (nsvc->bind != bind)
378 continue;
379
380 if (!osmo_sockaddr_cmp(remote, gprs_ns2_ip_vc_remote(nsvc)))
381 return nsvc;
382 }
383 return NULL;
384}
Alexander Couzens6a161492020-07-12 13:45:50 +0200385
386static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
387{
388 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
389 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
390 struct gprs_ns2_vc *nsvc;
391 struct gprs_ns2_vc_bind *bind;
392 struct osmo_sockaddr remote = { };
393 unsigned int i;
394
Harald Weltee8c61062021-03-24 13:16:27 +0100395 /* iterate over all remote IPv4 endpoints */
Alexander Couzens6a161492020-07-12 13:45:50 +0200396 for (i = 0; i < gss->num_ip4_remote; i++) {
397 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->ip4_remote[i];
398
399 remote.u.sin.sin_family = AF_INET;
400 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
401 remote.u.sin.sin_port = ip4->udp_port;
402
Harald Weltee8c61062021-03-24 13:16:27 +0100403 /* iterate over all local binds */
Alexander Couzens6a161492020-07-12 13:45:50 +0200404 llist_for_each_entry(bind, &nse->nsi->binding, list) {
Harald Weltee8c61062021-03-24 13:16:27 +0100405 /* we only care about UDP binds */
Daniel Willmann967e2c12021-01-14 16:58:17 +0100406 if (bind->ll != GPRS_NS2_LL_UDP)
407 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200408
Harald Weltee8c61062021-03-24 13:16:27 +0100409 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
410 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200411 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
412 if (!nsvc) {
413 /* TODO: add to a list to send back a NS-STATUS */
414 continue;
415 }
416 }
417
418 /* update data / signalling weight */
419 nsvc->data_weight = ip4->data_weight;
420 nsvc->sig_weight = ip4->sig_weight;
421 nsvc->sns_only = false;
422 }
423 }
424
Harald Weltee8c61062021-03-24 13:16:27 +0100425 /* iterate over all remote IPv4 endpoints */
Alexander Couzens6a161492020-07-12 13:45:50 +0200426 for (i = 0; i < gss->num_ip6_remote; i++) {
427 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->ip6_remote[i];
428
429 remote.u.sin6.sin6_family = AF_INET6;
430 remote.u.sin6.sin6_addr = ip6->ip_addr;
431 remote.u.sin6.sin6_port = ip6->udp_port;
432
Harald Weltee8c61062021-03-24 13:16:27 +0100433 /* iterate over all local binds */
Alexander Couzens6a161492020-07-12 13:45:50 +0200434 llist_for_each_entry(bind, &nse->nsi->binding, list) {
Daniel Willmann967e2c12021-01-14 16:58:17 +0100435 if (bind->ll != GPRS_NS2_LL_UDP)
436 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200437
Harald Weltee8c61062021-03-24 13:16:27 +0100438 /* we only care about UDP binds */
439 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
440 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200441 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
442 if (!nsvc) {
443 /* TODO: add to a list to send back a NS-STATUS */
444 continue;
445 }
446 }
447
448 /* update data / signalling weight */
449 nsvc->data_weight = ip6->data_weight;
450 nsvc->sig_weight = ip6->sig_weight;
451 nsvc->sns_only = false;
452 }
453 }
454
455
456 return 0;
457}
458
459/* Add a given remote IPv4 element to gprs_sns_state */
460static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
461{
462 unsigned int i;
463
464 if (gss->num_ip4_remote >= gss->num_max_ip4_remote)
465 return -NS_CAUSE_INVAL_NR_NS_VC;
466
467 /* check for duplicates */
468 for (i = 0; i < gss->num_ip4_remote; i++) {
469 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
470 continue;
471 /* TODO: log message duplicate */
Alexander Couzens6a161492020-07-12 13:45:50 +0200472 return -NS_CAUSE_PROTO_ERR_UNSPEC;
473 }
474
475 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem,
476 gss->num_ip4_remote+1);
477 gss->ip4_remote[gss->num_ip4_remote] = *ip4;
478 gss->num_ip4_remote += 1;
479 return 0;
480}
481
482/* Remove a given remote IPv4 element from gprs_sns_state */
483static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
484{
485 unsigned int i;
486
487 for (i = 0; i < gss->num_ip4_remote; i++) {
488 if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4)))
489 continue;
490 /* all array elements < i remain as they are; all > i are shifted left by one */
491 memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1);
492 gss->num_ip4_remote -= 1;
493 return 0;
494 }
495 return -1;
496}
497
498/* update the weights for specified remote IPv4 */
499static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4)
500{
501 unsigned int i;
502
503 for (i = 0; i < gss->num_ip4_remote; i++) {
504 if (gss->ip4_remote[i].ip_addr != ip4->ip_addr ||
505 gss->ip4_remote[i].udp_port != ip4->udp_port)
506 continue;
507
508 gss->ip4_remote[i].sig_weight = ip4->sig_weight;
509 gss->ip4_remote[i].data_weight = ip4->data_weight;
510 return 0;
511 }
512 return -1;
513}
514
515/* Add a given remote IPv6 element to gprs_sns_state */
516static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
517{
518 if (gss->num_ip6_remote >= gss->num_max_ip6_remote)
519 return -NS_CAUSE_INVAL_NR_NS_VC;
520
521 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, struct gprs_ns_ie_ip6_elem,
522 gss->num_ip6_remote+1);
523 gss->ip6_remote[gss->num_ip6_remote] = *ip6;
524 gss->num_ip6_remote += 1;
525 return 0;
526}
527
528/* Remove a given remote IPv6 element from gprs_sns_state */
529static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
530{
531 unsigned int i;
532
533 for (i = 0; i < gss->num_ip6_remote; i++) {
534 if (memcmp(&gss->ip6_remote[i], ip6, sizeof(*ip6)))
535 continue;
536 /* all array elements < i remain as they are; all > i are shifted left by one */
537 memmove(&gss->ip6_remote[i], &gss->ip6_remote[i+1], gss->num_ip6_remote-i-1);
538 gss->num_ip6_remote -= 1;
539 return 0;
540 }
541 return -1;
542}
543
544/* update the weights for specified remote IPv6 */
545static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6)
546{
547 unsigned int i;
548
549 for (i = 0; i < gss->num_ip6_remote; i++) {
550 if (memcmp(&gss->ip6_remote[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
551 gss->ip6_remote[i].udp_port != ip6->udp_port)
552 continue;
553 gss->ip6_remote[i].sig_weight = ip6->sig_weight;
554 gss->ip6_remote[i].data_weight = ip6->data_weight;
555 return 0;
556 }
557 return -1;
558}
559
560static 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)
561{
562 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
563 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
564 struct gprs_ns2_vc *nsvc;
565 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200566 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200567 uint8_t new_signal;
568 uint8_t new_data;
569
570 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
571 * signalling weights of all the peer IP endpoints configured for this NSE is
572 * equal to zero or if the resulting sum of the data weights of all the peer IP
573 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
574 * SNS-ACK PDU with a cause code of "Invalid weights". */
575
576 if (ip4) {
577 if (update_remote_ip4_elem(gss, ip4))
578 return -NS_CAUSE_UNKN_IP_EP;
579
580 /* copy over. Both data structures use network byte order */
581 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
582 sa.u.sin.sin_port = ip4->udp_port;
583 sa.u.sin.sin_family = AF_INET;
584 new_signal = ip4->sig_weight;
585 new_data = ip4->data_weight;
586 } else if (ip6) {
587 if (update_remote_ip6_elem(gss, ip6))
588 return -NS_CAUSE_UNKN_IP_EP;
589
590 /* copy over. Both data structures use network byte order */
591 sa.u.sin6.sin6_addr = ip6->ip_addr;
592 sa.u.sin6.sin6_port = ip6->udp_port;
593 sa.u.sin6.sin6_family = AF_INET6;
594 new_signal = ip6->sig_weight;
595 new_data = ip6->data_weight;
596 } else {
597 OSMO_ASSERT(false);
598 }
599
600 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200601 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200602 /* all nsvc in NSE should be IP/UDP nsvc */
603 OSMO_ASSERT(remote);
604
605 if (osmo_sockaddr_cmp(&sa, remote))
606 continue;
607
608 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
609 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
610 nsvc->sig_weight, new_signal);
611
612 nsvc->data_weight = new_data;
613 nsvc->sig_weight = new_signal;
614 }
615
616 return 0;
617}
618
619static int do_sns_delete(struct osmo_fsm_inst *fi,
620 const struct gprs_ns_ie_ip4_elem *ip4,
621 const struct gprs_ns_ie_ip6_elem *ip6)
622{
623 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
624 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
625 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200626 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200627 struct osmo_sockaddr sa = {};
628
629 if (ip4) {
630 if (remove_remote_ip4_elem(gss, ip4) < 0)
631 return -NS_CAUSE_UNKN_IP_EP;
632 /* copy over. Both data structures use network byte order */
633 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
634 sa.u.sin.sin_port = ip4->udp_port;
635 sa.u.sin.sin_family = AF_INET;
636 } else if (ip6) {
637 if (remove_remote_ip6_elem(gss, ip6))
638 return -NS_CAUSE_UNKN_IP_EP;
639
640 /* copy over. Both data structures use network byte order */
641 sa.u.sin6.sin6_addr = ip6->ip_addr;
642 sa.u.sin6.sin6_port = ip6->udp_port;
643 sa.u.sin6.sin6_family = AF_INET6;
644 } else {
645 OSMO_ASSERT(false);
646 }
647
648 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200649 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200650 /* all nsvc in NSE should be IP/UDP nsvc */
651 OSMO_ASSERT(remote);
652 if (osmo_sockaddr_cmp(&sa, remote))
653 continue;
654
655 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
656 gprs_ns2_free_nsvc(nsvc);
657 }
658
659 return 0;
660}
661
662static int do_sns_add(struct osmo_fsm_inst *fi,
663 const struct gprs_ns_ie_ip4_elem *ip4,
664 const struct gprs_ns_ie_ip6_elem *ip6)
665{
666 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
667 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
668 struct gprs_ns2_vc *nsvc;
669 int rc = 0;
670
671 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
672 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
673 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
674 switch (gss->ip) {
675 case IPv4:
676 rc = add_remote_ip4_elem(gss, ip4);
677 break;
678 case IPv6:
679 rc = add_remote_ip6_elem(gss, ip6);
680 break;
681 default:
682 /* the gss->ip is initialized with the bss */
683 OSMO_ASSERT(false);
684 }
685
686 if (rc)
687 return rc;
688
689 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
690 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
691 * unspecified" */
692 switch (gss->ip) {
693 case IPv4:
694 nsvc = nsvc_by_ip4_elem(nse, ip4);
695 if (nsvc) {
696 /* the nsvc should be already in sync with the ip4 / ip6 elements */
697 return -NS_CAUSE_PROTO_ERR_UNSPEC;
698 }
699
700 /* TODO: failure case */
701 ns2_nsvc_create_ip4(fi, nse, ip4);
702 break;
703 case IPv6:
704 nsvc = nsvc_by_ip6_elem(nse, ip6);
705 if (nsvc) {
706 /* the nsvc should be already in sync with the ip4 / ip6 elements */
707 return -NS_CAUSE_PROTO_ERR_UNSPEC;
708 }
709
710 /* TODO: failure case */
711 ns2_nsvc_create_ip6(fi, nse, ip6);
712 break;
713 }
714
715 gprs_ns2_start_alive_all_nsvcs(nse);
716
717 return 0;
718}
719
720
Harald Welte694dad52021-03-23 15:22:16 +0100721static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200722{
Harald Weltef61a9152021-03-02 22:20:17 +0100723 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
724 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzense769f522020-12-07 07:37:07 +0100725 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200726}
727
Harald Welte694dad52021-03-23 15:22:16 +0100728static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200729{
Harald Weltef61a9152021-03-02 22:20:17 +0100730 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200731 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
732 struct gprs_ns2_inst *nsi = nse->nsi;
733 struct tlv_parsed *tp = NULL;
734
Harald Weltef61a9152021-03-02 22:20:17 +0100735 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
736
Alexander Couzens6a161492020-07-12 13:45:50 +0200737 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100738 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200739 tp = data;
740 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
741 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
742 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
743 /* TODO: What to do? */
744 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100745 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200746 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
747 }
748 break;
749 default:
750 OSMO_ASSERT(0);
751 }
752}
753
Harald Welte01fa6a32021-03-04 19:49:38 +0100754static int ns2_sns_count_num_local_ep(struct osmo_fsm_inst *fi, enum ns2_sns_type stype)
755{
756 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
757 struct ns2_sns_bind *sbind;
758 int count = 0;
759
760 llist_for_each_entry(sbind, &gss->binds, list) {
761 const struct osmo_sockaddr *sa = gprs_ns2_ip_bind_sockaddr(sbind->bind);
762 if (!sa)
763 continue;
764
765 switch (stype) {
766 case IPv4:
767 if (sa->u.sas.ss_family == AF_INET)
768 count++;
769 break;
770 case IPv6:
771 if (sa->u.sas.ss_family == AF_INET6)
772 count++;
773 break;
774 }
775 }
776 return count;
777}
778
Harald Welte24920e22021-03-04 13:03:27 +0100779static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200780{
781 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100782 struct gprs_ns_ie_ip4_elem *ip4_elems;
783 struct gprs_ns_ie_ip6_elem *ip6_elems;
784 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100785 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100786 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100787 const struct osmo_sockaddr *sa;
788 struct osmo_sockaddr local;
789 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200790
Harald Welte46eb7642021-03-04 17:49:59 +0100791 ns2_clear_ipv46_entries_local(gss);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100792
Alexander Couzense769f522020-12-07 07:37:07 +0100793 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100794 if (gss->role == GPRS_SNS_ROLE_BSS) {
795 if (!gss->initial)
796 return;
797 remote = &gss->initial->saddr;
798 } else
799 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100800
801 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100802 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100803 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100804 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100805 return;
806 }
807
Alexander Couzense769f522020-12-07 07:37:07 +0100808 switch (gss->ip) {
809 case IPv4:
810 ip4_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip4_elem) * count);
811 if (!ip4_elems)
812 return;
813
814 gss->ip4_local = ip4_elems;
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_INET)
822 continue;
823
824 /* check if this is an specific bind */
825 if (sa->u.sin.sin_addr.s_addr == 0) {
826 if (osmo_sockaddr_local_ip(&local, remote))
827 continue;
828
829 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
830 } else {
831 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
832 }
833
834 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100835 ip4_elems->sig_weight = bind->sns_sig_weight;
836 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100837 ip4_elems++;
838 }
839
840 gss->num_ip4_local = count;
Alexander Couzense769f522020-12-07 07:37:07 +0100841 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->num_ip4_local, 8);
842 break;
843 case IPv6:
844 /* IPv6 */
845 ip6_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip6_elem) * count);
846 if (!ip6_elems)
847 return;
848
849 gss->ip6_local = ip6_elems;
850
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100851 llist_for_each_entry(sbind, &gss->binds, list) {
852 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100853 sa = gprs_ns2_ip_bind_sockaddr(bind);
854 if (!sa)
855 continue;
856
857 if (sa->u.sas.ss_family != AF_INET6)
858 continue;
859
860 /* check if this is an specific bind */
861 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
862 if (osmo_sockaddr_local_ip(&local, remote))
863 continue;
864
865 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
866 } else {
867 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
868 }
869
870 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100871 ip6_elems->sig_weight = bind->sns_sig_weight;
872 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100873
874 ip6_elems++;
875 }
876 gss->num_ip6_local = count;
Alexander Couzense769f522020-12-07 07:37:07 +0100877 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->num_ip6_local, 8);
878 break;
879 }
Harald Welte24920e22021-03-04 13:03:27 +0100880}
881
882/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Harald Welte694dad52021-03-23 15:22:16 +0100883static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Harald Welte24920e22021-03-04 13:03:27 +0100884{
885 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
886
Harald Weltef61a9152021-03-02 22:20:17 +0100887 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
888
Harald Welte24920e22021-03-04 13:03:27 +0100889 /* on a generic failure, the timer callback will recover */
890 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
891 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Harald Welte694dad52021-03-23 15:22:16 +0100892 if (old_state != GPRS_SNS_ST_BSS_SIZE)
Harald Welte24920e22021-03-04 13:03:27 +0100893 gss->N = 0;
894
895 gss->alive = false;
896
897 ns2_sns_compute_local_ep_from_binds(fi);
898
899 /* take the first bind or take the next bind */
900 if (!gss->initial_bind) {
901 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
902 } else {
903 if (gss->initial_bind->list.next != &gss->binds) {
904 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
905 } else {
906 gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list);
907 }
908 }
909
910
911 /* setup the NSVC */
912 if (!gss->sns_nsvc) {
913 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
914 struct osmo_sockaddr *remote = &gss->initial->saddr;
915 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
916 if (!gss->sns_nsvc)
917 return;
Harald Weltec962a2e2021-03-05 08:09:08 +0100918 /* A pre-configured endpoint shall not be used for NSE data or signalling traffic
919 * (with the exception of Size and Configuration procedures) unless it is configured
920 * by the SGSN using the auto-configuration procedures */
Harald Welte24920e22021-03-04 13:03:27 +0100921 gss->sns_nsvc->sns_only = true;
922 }
923
Alexander Couzense769f522020-12-07 07:37:07 +0100924
Alexander Couzens6a161492020-07-12 13:45:50 +0200925 if (gss->num_max_ip4_remote > 0)
Harald Welte4e41acc2021-03-23 11:55:25 +0100926 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->num_ip4_local, -1);
Alexander Couzens6a161492020-07-12 13:45:50 +0200927 else
Harald Welte4e41acc2021-03-23 11:55:25 +0100928 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200929}
930
Harald Welte694dad52021-03-23 15:22:16 +0100931static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200932{
Harald Weltef61a9152021-03-02 22:20:17 +0100933 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens3df58862021-02-05 17:18:08 +0100934 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Harald Weltef61a9152021-03-02 22:20:17 +0100935 struct tlv_parsed *tp = NULL;
936
937 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzens6a161492020-07-12 13:45:50 +0200938
939 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100940 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200941 tp = (struct tlv_parsed *) data;
942 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
943 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
944 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
945 /* TODO: What to do? */
946 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100947 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 +0200948 }
949 break;
950 default:
951 OSMO_ASSERT(0);
952 }
953}
954
Harald Welte694dad52021-03-23 15:22:16 +0100955static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200956{
957 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100958
Harald Weltef61a9152021-03-02 22:20:17 +0100959 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
960
Harald Welte694dad52021-03-23 15:22:16 +0100961 if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS)
Alexander Couzens790a9632021-02-05 17:18:39 +0100962 gss->N = 0;
963
Alexander Couzens6a161492020-07-12 13:45:50 +0200964 /* Transmit SNS-CONFIG */
Alexander Couzens6a161492020-07-12 13:45:50 +0200965 switch (gss->ip) {
966 case IPv4:
967 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100968 gss->ip4_local, gss->num_ip4_local,
969 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200970 break;
971 case IPv6:
972 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100973 NULL, 0,
974 gss->ip6_local, gss->num_ip6_local);
Alexander Couzens6a161492020-07-12 13:45:50 +0200975 break;
976 }
977}
978
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100979/* calculate the timeout of the configured state. the configured
980 * state will fail if not at least one NS-VC is alive within X second.
981 */
982static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
983{
984 int secs;
985 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
986 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
987 secs += nsi->timeout[NS_TOUT_TNS_TEST];
988
989 return secs;
990}
Alexander Couzens6a161492020-07-12 13:45:50 +0200991
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100992/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
993static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200994{
995 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200996
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100997 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
998 const struct gprs_ns_ie_ip4_elem *v4_list;
999 unsigned int num_v4;
1000 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1001 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +02001002
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001003 if (num_v4 && gss->ip6_remote)
1004 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +02001005
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001006 /* realloc to the new size */
1007 gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote,
1008 struct gprs_ns_ie_ip4_elem,
1009 gss->num_ip4_remote + num_v4);
1010 /* append the new entries to the end of the list */
1011 memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list));
1012 gss->num_ip4_remote += num_v4;
1013
1014 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
1015 gss->num_ip4_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +02001016 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001017
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001018 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1019 const struct gprs_ns_ie_ip6_elem *v6_list;
1020 unsigned int num_v6;
1021 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1022 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
1023
1024 if (num_v6 && gss->ip4_remote)
1025 return -NS_CAUSE_INVAL_NR_IPv6_EP;
1026
1027 /* realloc to the new size */
1028 gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote,
1029 struct gprs_ns_ie_ip6_elem,
1030 gss->num_ip6_remote + num_v6);
1031 /* append the new entries to the end of the list */
1032 memcpy(&gss->ip6_remote[gss->num_ip6_remote], v6_list, num_v6*sizeof(*v6_list));
1033 gss->num_ip6_remote += num_v6;
1034
1035 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %u entries\n",
1036 gss->num_ip6_remote);
Alexander Couzens6a161492020-07-12 13:45:50 +02001037 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001038
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001039 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001040}
1041
Harald Welte694dad52021-03-23 15:22:16 +01001042static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens790a9632021-02-05 17:18:39 +01001043{
1044 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1045
Harald Weltef61a9152021-03-02 22:20:17 +01001046 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1047
Harald Welte694dad52021-03-23 15:22:16 +01001048 if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN)
Alexander Couzens790a9632021-02-05 17:18:39 +01001049 gss->N = 0;
1050}
1051
Harald Welte694dad52021-03-23 15:22:16 +01001052static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +02001053{
1054 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001055 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1056 uint8_t cause;
1057 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001058
Harald Weltef61a9152021-03-02 22:20:17 +01001059 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1060
Alexander Couzens6a161492020-07-12 13:45:50 +02001061 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001062 case GPRS_SNS_EV_RX_CONFIG_END:
1063 case GPRS_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001064 rc = ns_sns_append_remote_eps(fi, data);
1065 if (rc < 0) {
1066 cause = -rc;
1067 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1068 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1069 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001070 }
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001071 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1072 /* check if sum of data / sig weights == 0 */
1073 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
1074 cause = NS_CAUSE_INVAL_WEIGH;
1075 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1076 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1077 return;
1078 }
1079 create_missing_nsvcs(fi);
1080 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1081 /* start the test procedure on ALL NSVCs! */
1082 gprs_ns2_start_alive_all_nsvcs(nse);
1083 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1084 } else {
1085 /* just send CONFIG-ACK */
1086 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1087 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001088 }
1089 break;
1090 default:
1091 OSMO_ASSERT(0);
1092 }
1093}
1094
Alexander Couzens67725e22021-02-15 02:37:03 +01001095/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001096static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1097 struct ns2_sns_state *gss,
1098 struct tlv_parsed *tp)
1099{
1100 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1101 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1102 int num_v4 = 0, num_v6 = 0;
1103 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001104 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001105 int rc = 0;
1106
1107 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1108 * check uniqueness within the lists (no doublicate entries)
1109 * check not-known-by-us and sent back a list of unknown/known values
1110 * (abnormal behaviour according to 48.016)
1111 */
1112
1113 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1114 if (gss->ip == IPv4) {
1115 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1116 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1117 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1118 return;
1119 }
1120
1121 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1122 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001123 for (i = 0; i < num_v4; i++) {
1124 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001125 rc = do_sns_add(fi, &v4_list[i], NULL);
1126 if (rc < 0) {
1127 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001128 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001129 do_sns_delete(fi, &v4_list[j], NULL);
1130 cause = -rc;
1131 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1132 break;
1133 }
1134 }
1135 } else { /* IPv6 */
1136 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1137 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1138 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1139 return;
1140 }
1141
1142 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1143 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001144 for (i = 0; i < num_v6; i++) {
1145 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001146 rc = do_sns_add(fi, NULL, &v6_list[i]);
1147 if (rc < 0) {
1148 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001149 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001150 do_sns_delete(fi, NULL, &v6_list[j]);
1151 cause = -rc;
1152 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1153 break;
1154 }
1155 }
1156 }
1157
1158 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1159 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1160}
1161
1162static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1163 struct ns2_sns_state *gss,
1164 struct tlv_parsed *tp)
1165{
1166 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1167 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1168 int num_v4 = 0, num_v6 = 0;
1169 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001170 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001171 int rc = 0;
1172
1173 /* TODO: split up delete into v4 + v6
1174 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1175 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1176 */
1177 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1178 if (gss->ip == IPv4) {
1179 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1180 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1181 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001182 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001183 rc = do_sns_delete(fi, &v4_list[i], NULL);
1184 if (rc < 0) {
1185 cause = -rc;
1186 /* continue to delete others */
1187 }
1188 }
1189 if (cause != 0xff) {
1190 /* TODO: create list of not-deleted and return it */
1191 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1192 return;
1193 }
1194
1195 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
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_ip4_elem *ip4_remote;
1199 uint32_t ip_addr = *(uint32_t *)(ie+1);
1200 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1201 cause = NS_CAUSE_UNKN_IP_ADDR;
1202 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1203 return;
1204 }
1205 /* make a copy as do_sns_delete() will change the array underneath us */
1206 ip4_remote = talloc_memdup(fi, gss->ip4_remote,
1207 gss->num_ip4_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001208 for (i = 0; i < gss->num_ip4_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001209 if (ip4_remote[i].ip_addr == ip_addr) {
1210 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1211 if (rc < 0) {
1212 cause = -rc;
1213 /* continue to delete others */
1214 }
1215 }
1216 }
1217 talloc_free(ip4_remote);
1218 if (cause != 0xff) {
1219 /* TODO: create list of not-deleted and return it */
1220 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1221 return;
1222 }
1223 } else {
1224 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1225 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1226 return;
1227 }
1228 } else { /* IPv6 */
1229 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1230 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1231 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001232 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001233 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1234 if (rc < 0) {
1235 cause = -rc;
1236 /* continue to delete others */
1237 }
1238 }
1239 if (cause != 0xff) {
1240 /* TODO: create list of not-deleted and return it */
1241 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1242 return;
1243 }
1244 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1245 /* delete all NS-VCs for given IPv4 address */
1246 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1247 struct gprs_ns_ie_ip6_elem *ip6_remote;
1248 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001249 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001250 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1251 cause = NS_CAUSE_UNKN_IP_ADDR;
1252 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1253 return;
1254 }
1255 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1256 /* make a copy as do_sns_delete() will change the array underneath us */
1257 ip6_remote = talloc_memdup(fi, gss->ip6_remote,
1258 gss->num_ip6_remote * sizeof(*v4_list));
Harald Welte7da6ace2020-09-18 09:48:05 +02001259 for (i = 0; i < gss->num_ip6_remote; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001260 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1261 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1262 if (rc < 0) {
1263 cause = -rc;
1264 /* continue to delete others */
1265 }
1266 }
1267 }
1268
1269 talloc_free(ip6_remote);
1270 if (cause != 0xff) {
1271 /* TODO: create list of not-deleted and return it */
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_IPv6_EP;
1277 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1278 return;
1279 }
1280 }
1281 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1282}
1283
1284static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1285 struct ns2_sns_state *gss,
1286 struct tlv_parsed *tp)
1287{
1288 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1289 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1290 int num_v4 = 0, num_v6 = 0;
1291 uint8_t trans_id, cause = 0xff;
1292 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001293 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001294
1295 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1296 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1297 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1298 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001299 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001300 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1301 if (rc < 0) {
1302 cause = -rc;
1303 /* continue to others */
1304 }
1305 }
1306 if (cause != 0xff) {
1307 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1308 return;
1309 }
1310 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1311 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1312 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001313 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001314 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1315 if (rc < 0) {
1316 cause = -rc;
1317 /* continue to others */
1318 }
1319 }
1320 if (cause != 0xff) {
1321 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1322 return;
1323 }
1324 } else {
1325 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1326 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1327 return;
1328 }
1329 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1330}
1331
1332static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1333{
1334 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1335 struct tlv_parsed *tp = data;
1336
1337 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001338 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001339 ns2_sns_st_configured_add(fi, gss, tp);
1340 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001341 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001342 ns2_sns_st_configured_delete(fi, gss, tp);
1343 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001344 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001345 ns2_sns_st_configured_change(fi, gss, tp);
1346 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001347 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001348 osmo_timer_del(&fi->timer);
1349 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001350 }
1351}
1352
1353static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1354{
1355 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens138b96f2021-01-25 16:23:29 +01001356 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001357}
1358
1359static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1360 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001361 .in_event_mask = 0, /* handled by all_state_action */
Harald Welte694dad52021-03-23 15:22:16 +01001362 .out_state_mask = S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001363 .name = "UNCONFIGURED",
Harald Welte694dad52021-03-23 15:22:16 +01001364 .action = ns2_sns_st_bss_unconfigured,
Alexander Couzens6a161492020-07-12 13:45:50 +02001365 },
Harald Welte694dad52021-03-23 15:22:16 +01001366 [GPRS_SNS_ST_BSS_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001367 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001368 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001369 S(GPRS_SNS_ST_BSS_SIZE) |
1370 S(GPRS_SNS_ST_BSS_CONFIG_BSS),
1371 .name = "BSS_SIZE",
1372 .action = ns2_sns_st_bss_size,
1373 .onenter = ns2_sns_st_bss_size_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001374 },
Harald Welte694dad52021-03-23 15:22:16 +01001375 [GPRS_SNS_ST_BSS_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001376 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001377 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001378 S(GPRS_SNS_ST_BSS_CONFIG_BSS) |
1379 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
1380 S(GPRS_SNS_ST_BSS_SIZE),
1381 .name = "BSS_CONFIG_BSS",
1382 .action = ns2_sns_st_bss_config_bss,
1383 .onenter = ns2_sns_st_bss_config_bss_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001384 },
Harald Welte694dad52021-03-23 15:22:16 +01001385 [GPRS_SNS_ST_BSS_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001386 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1387 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001388 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001389 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens6a161492020-07-12 13:45:50 +02001390 S(GPRS_SNS_ST_CONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001391 S(GPRS_SNS_ST_BSS_SIZE),
1392 .name = "BSS_CONFIG_SGSN",
1393 .action = ns2_sns_st_bss_config_sgsn,
1394 .onenter = ns2_sns_st_bss_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001395 },
1396 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001397 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1398 S(GPRS_SNS_EV_RX_DELETE) |
1399 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1400 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001401 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001402 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001403 .name = "CONFIGURED",
1404 .action = ns2_sns_st_configured,
1405 .onenter = ns2_sns_st_configured_onenter,
1406 },
1407};
1408
1409static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1410{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001411 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001412 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1413 struct gprs_ns2_inst *nsi = nse->nsi;
1414
Alexander Couzens90ee9632020-12-07 06:18:32 +01001415 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001416 switch (fi->T) {
1417 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001418 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1419 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001420 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001421 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001422 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 +01001423 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001424 break;
1425 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001426 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001427 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 +01001428 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001429 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001430 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 +01001431 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001432 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001433 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001434 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1435 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 +01001436 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001437 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001438 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 +01001439 }
1440 break;
1441 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001442 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 +01001443 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001444 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001445 }
1446 return 0;
1447}
1448
Harald Welte9e37bf42021-03-02 20:48:31 +01001449/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001450static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1451{
1452 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001453 struct ns2_sns_bind *sbind;
1454 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001455
Alexander Couzense769f522020-12-07 07:37:07 +01001456 switch (event) {
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001457 case GPRS_SNS_EV_REQ_ADD_BIND:
1458 sbind = data;
1459 switch (fi->state) {
1460 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001461 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001462 break;
Harald Welte694dad52021-03-23 15:22:16 +01001463 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001464 /* TODO: add the ip4 element to the list */
1465 break;
Harald Welte694dad52021-03-23 15:22:16 +01001466 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1467 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001468 case GPRS_SNS_ST_CONFIGURED:
1469 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1470 break;
1471 }
1472 break;
1473 case GPRS_SNS_EV_REQ_DELETE_BIND:
1474 sbind = data;
1475 switch (fi->state) {
1476 case GPRS_SNS_ST_UNCONFIGURED:
1477 break;
Harald Welte694dad52021-03-23 15:22:16 +01001478 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001479 /* TODO: remove the ip4 element from the list */
1480 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1481 if (nsvc->bind == sbind->bind) {
1482 gprs_ns2_free_nsvc(nsvc);
1483 }
1484 }
1485 break;
Harald Welte694dad52021-03-23 15:22:16 +01001486 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1487 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001488 case GPRS_SNS_ST_CONFIGURED:
1489 /* TODO: do an delete SNS-IP procedure */
1490 /* TODO: remove the ip4 element to the list */
1491 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1492 if (nsvc->bind == sbind->bind) {
1493 gprs_ns2_free_nsvc(nsvc);
1494 }
1495 }
1496 break;
1497 }
1498 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1499 talloc_free(sbind);
1500 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001501 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001502}
1503
Harald Welte9e37bf42021-03-02 20:48:31 +01001504/* allstate-action for BSS role */
1505static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1506{
1507 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1508 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1509
1510 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
1511 switch (event) {
1512 case GPRS_SNS_EV_REQ_NO_NSVC:
1513 /* ignore reselection running */
1514 if (gss->reselection_running)
1515 break;
1516
1517 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1518 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1519 break;
1520 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
1521 /* tear down previous state
1522 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1523 gss->reselection_running = true;
1524 gprs_ns2_free_nsvcs(nse);
Harald Welte46eb7642021-03-04 17:49:59 +01001525 ns2_clear_ipv46_entries_local(gss);
1526 ns2_clear_ipv46_entries_remote(gss);
Harald Welte9e37bf42021-03-02 20:48:31 +01001527
1528 /* Choose the next sns endpoint. */
1529 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds)) {
1530 gss->initial = NULL;
1531 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1532 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1533 return;
1534 } else if (!gss->initial) {
1535 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1536 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1537 /* last entry, continue with first */
1538 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1539 } else {
1540 /* next element is an entry */
1541 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1542 }
1543
1544 gss->reselection_running = false;
Harald Welte694dad52021-03-23 15:22:16 +01001545 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 +01001546 break;
1547 default:
1548 ns2_sns_st_all_action(fi, event, data);
1549 break;
1550 }
1551}
1552
Alexander Couzens6a161492020-07-12 13:45:50 +02001553static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1554 .name = "GPRS-NS2-SNS-BSS",
1555 .states = ns2_sns_bss_states,
1556 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001557 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1558 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001559 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1560 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001561 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001562 .cleanup = NULL,
1563 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001564 .event_names = gprs_sns_event_names,
1565 .pre_term = NULL,
1566 .log_subsys = DLNS,
1567};
1568
Harald Welte5bef2cc2020-09-18 22:33:24 +02001569/*! Allocate an IP-SNS FSM for the BSS side.
1570 * \param[in] nse NS Entity in which the FSM runs
1571 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001572 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001573struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1574 const char *id)
1575{
1576 struct osmo_fsm_inst *fi;
1577 struct ns2_sns_state *gss;
1578
1579 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1580 if (!fi)
1581 return fi;
1582
1583 gss = talloc_zero(fi, struct ns2_sns_state);
1584 if (!gss)
1585 goto err;
1586
1587 fi->priv = gss;
1588 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001589 gss->role = GPRS_SNS_ROLE_BSS;
Harald Welte24f4df52021-03-04 18:02:54 +01001590 /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */
1591 gss->num_max_ip4_remote = 8192;
1592 gss->num_max_ip6_remote = 8192;
Alexander Couzense769f522020-12-07 07:37:07 +01001593 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001594 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001595
1596 return fi;
1597err:
1598 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1599 return NULL;
1600}
1601
Harald Welte5bef2cc2020-09-18 22:33:24 +02001602/*! main entry point for receiving SNS messages from the network.
1603 * \param[in] nsvc NS-VC on which the message was received
1604 * \param[in] msg message buffer of the IP-SNS message
1605 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001606 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001607int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001608{
1609 struct gprs_ns2_nse *nse = nsvc->nse;
1610 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1611 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001612 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001613 struct osmo_fsm_inst *fi;
1614
1615 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001616 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1617 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001618 return -EINVAL;
1619 }
1620
Alexander Couzens6a161492020-07-12 13:45:50 +02001621 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1622 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001623 gss = (struct ns2_sns_state *) fi->priv;
1624 if (!gss->sns_nsvc)
1625 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001626
Harald Weltef2949742021-01-20 14:54:14 +01001627 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1628 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1629
Alexander Couzens6a161492020-07-12 13:45:50 +02001630 switch (nsh->pdu_type) {
1631 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001632 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001633 break;
1634 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001635 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001636 break;
1637 case SNS_PDUT_CONFIG:
1638 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001639 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001640 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001641 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001642 break;
1643 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001644 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001645 break;
1646 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001647 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001648 break;
1649 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001650 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001651 break;
1652 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001653 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001654 break;
1655 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001656 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001657 break;
1658 default:
Harald Weltef2949742021-01-20 14:54:14 +01001659 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1660 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens6a161492020-07-12 13:45:50 +02001661 return -EINVAL;
1662 }
1663
1664 return 0;
1665}
1666
1667#include <osmocom/vty/vty.h>
1668#include <osmocom/vty/misc.h>
1669
Harald Welte1262c4f2021-01-19 20:58:33 +01001670static 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 +02001671{
1672 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001673 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001674 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1675}
1676
Harald Welte1262c4f2021-01-19 20:58:33 +01001677static 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 +02001678{
1679 char ip_addr[INET6_ADDRSTRLEN] = {};
1680 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1681 strcpy(ip_addr, "Invalid IPv6");
1682
Harald Welte1262c4f2021-01-19 20:58:33 +01001683 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001684 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1685}
1686
Harald Welte5bef2cc2020-09-18 22:33:24 +02001687/*! Dump the IP-SNS state to a vty.
1688 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001689 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001690 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1691 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001692void 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 +02001693{
1694 struct ns2_sns_state *gss;
1695 unsigned int i;
1696
1697 if (!nse->bss_sns_fi)
1698 return;
1699
Harald Welte1262c4f2021-01-19 20:58:33 +01001700 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001701 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1702
Harald Welte1262c4f2021-01-19 20:58:33 +01001703 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1704 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001705
1706 if (gss->num_ip4_local && gss->num_ip4_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001707 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001708 for (i = 0; i < gss->num_ip4_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001709 vty_dump_sns_ip4(vty, prefix, &gss->ip4_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001710
Harald Welte1262c4f2021-01-19 20:58:33 +01001711 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001712 for (i = 0; i < gss->num_ip4_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001713 vty_dump_sns_ip4(vty, prefix, &gss->ip4_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001714 }
1715
1716 if (gss->num_ip6_local && gss->num_ip6_remote) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001717 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001718 for (i = 0; i < gss->num_ip6_local; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001719 vty_dump_sns_ip6(vty, prefix, &gss->ip6_local[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001720
Harald Welte1262c4f2021-01-19 20:58:33 +01001721 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001722 for (i = 0; i < gss->num_ip6_remote; i++)
Harald Welte1262c4f2021-01-19 20:58:33 +01001723 vty_dump_sns_ip6(vty, prefix, &gss->ip6_remote[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001724 }
1725}
1726
Alexander Couzens412bc342020-11-19 05:24:37 +01001727/*! write IP-SNS to a vty
1728 * \param[in] vty VTY to which the state shall be printed
1729 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001730void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001731{
1732 struct ns2_sns_state *gss;
1733 struct osmo_sockaddr_str addr_str;
1734 struct sns_endpoint *endpoint;
1735
1736 if (!nse->bss_sns_fi)
1737 return;
1738
1739 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1740 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001741 /* It's unlikely that an error happens, but let's better be safe. */
1742 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1743 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001744 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001745 }
1746}
1747
Alexander Couzense769f522020-12-07 07:37:07 +01001748static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1749 const struct osmo_sockaddr *saddr)
1750{
1751 struct sns_endpoint *endpoint;
1752
1753 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1754 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1755 return endpoint;
1756 }
1757
1758 return NULL;
1759}
1760
1761/*! gprs_ns2_sns_add_endpoint
1762 * \param[in] nse
1763 * \param[in] sockaddr
1764 * \return
1765 */
1766int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1767 const struct osmo_sockaddr *saddr)
1768{
1769 struct ns2_sns_state *gss;
1770 struct sns_endpoint *endpoint;
1771 bool do_selection = false;
1772
1773 if (nse->ll != GPRS_NS2_LL_UDP) {
1774 return -EINVAL;
1775 }
1776
Alexander Couzens138b96f2021-01-25 16:23:29 +01001777 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001778 return -EINVAL;
1779 }
1780
1781 gss = nse->bss_sns_fi->priv;
1782
1783 if (ns2_get_sns_endpoint(gss, saddr))
1784 return -EADDRINUSE;
1785
1786 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1787 if (!endpoint)
1788 return -ENOMEM;
1789
1790 endpoint->saddr = *saddr;
1791 if (llist_empty(&gss->sns_endpoints))
1792 do_selection = true;
1793
1794 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1795 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001796 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001797
1798 return 0;
1799}
1800
1801/*! gprs_ns2_sns_del_endpoint
1802 * \param[in] nse
1803 * \param[in] sockaddr
1804 * \return 0 on success, otherwise < 0
1805 */
1806int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1807 const struct osmo_sockaddr *saddr)
1808{
1809 struct ns2_sns_state *gss;
1810 struct sns_endpoint *endpoint;
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 endpoint = ns2_get_sns_endpoint(gss, saddr);
1822 if (!endpoint)
1823 return -ENOENT;
1824
1825 /* if this is an unused SNS endpoint it's done */
1826 if (gss->initial != endpoint) {
1827 llist_del(&endpoint->list);
1828 talloc_free(endpoint);
1829 return 0;
1830 }
1831
Alexander Couzens67725e22021-02-15 02:37:03 +01001832 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001833 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001834 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001835 "Closing all NS-VC and restart SNS-SIZE procedure"
1836 "with a remaining SNS endpoint.\n");
1837
1838 /* Continue with the next endpoint in the list.
1839 * Special case if the endpoint is at the start or end of the list */
1840 if (endpoint->list.prev == &gss->sns_endpoints ||
1841 endpoint->list.next == &gss->sns_endpoints)
1842 gss->initial = NULL;
1843 else
1844 gss->initial = llist_entry(endpoint->list.next->prev,
1845 struct sns_endpoint,
1846 list);
1847
1848 llist_del(&endpoint->list);
1849 gprs_ns2_free_nsvcs(nse);
1850 talloc_free(endpoint);
1851
1852 return 0;
1853}
1854
1855/*! gprs_ns2_sns_count
1856 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1857 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1858 */
1859int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1860{
1861 struct ns2_sns_state *gss;
1862 struct sns_endpoint *endpoint;
1863 int count = 0;
1864
1865 if (nse->ll != GPRS_NS2_LL_UDP) {
1866 return -EINVAL;
1867 }
1868
Alexander Couzens138b96f2021-01-25 16:23:29 +01001869 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001870 return -EINVAL;
1871 }
1872
1873 gss = nse->bss_sns_fi->priv;
1874 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1875 count++;
1876
1877 return count;
1878}
1879
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001880void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1881{
1882 struct ns2_sns_state *gss;
1883 struct gprs_ns2_vc *tmp;
1884
1885 if (!nse->bss_sns_fi)
1886 return;
1887
1888 gss = nse->bss_sns_fi->priv;
1889 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1890 return;
1891
1892 if (alive == gss->alive)
1893 return;
1894
1895 /* check if this is the current SNS NS-VC */
1896 if (nsvc == gss->sns_nsvc) {
1897 /* only replace the SNS NS-VC if there are other alive NS-VC.
1898 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1899 * and couldn't confirm yet if the NS-VC comes up */
1900 if (gss->alive && !alive)
1901 ns2_sns_replace_nsvc(nsvc);
1902 }
1903
1904 if (alive) {
1905 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001906 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001907 } else {
1908 /* is there at least another alive nsvc? */
1909 llist_for_each_entry(tmp, &nse->nsvc, list) {
1910 if (ns2_vc_is_unblocked(tmp))
1911 return;
1912 }
1913
1914 /* all NS-VC have failed */
1915 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001916 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001917 }
1918}
1919
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001920int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1921 struct gprs_ns2_vc_bind *bind)
1922{
1923 struct ns2_sns_state *gss;
1924 struct ns2_sns_bind *tmp;
1925
1926 OSMO_ASSERT(nse->bss_sns_fi);
1927 gss = nse->bss_sns_fi->priv;
1928
1929 if (!gprs_ns2_is_ip_bind(bind)) {
1930 return -EINVAL;
1931 }
1932
1933 if (!llist_empty(&gss->binds)) {
1934 llist_for_each_entry(tmp, &gss->binds, list) {
1935 if (tmp->bind == bind)
1936 return -EALREADY;
1937 }
1938 }
1939
1940 tmp = talloc_zero(gss, struct ns2_sns_bind);
1941 if (!tmp)
1942 return -ENOMEM;
1943 tmp->bind = bind;
1944 llist_add_tail(&tmp->list, &gss->binds);
1945
1946 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
1947 return 0;
1948}
1949
1950/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
1951int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
1952 struct gprs_ns2_vc_bind *bind)
1953{
1954 struct ns2_sns_state *gss;
1955 struct ns2_sns_bind *tmp, *tmp2;
1956 bool found = false;
1957
1958 if (!nse->bss_sns_fi)
1959 return -EINVAL;
1960
1961 gss = nse->bss_sns_fi->priv;
1962 if (gss->initial_bind && gss->initial_bind->bind == bind) {
1963 if (gss->initial_bind->list.prev == &gss->binds)
1964 gss->initial_bind = NULL;
1965 else
1966 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
1967 }
1968
1969 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
1970 if (tmp->bind == bind) {
1971 llist_del(&tmp->list);
1972 found = true;
1973 }
1974 }
1975
1976 if (!found)
1977 return -ENOENT;
1978
1979 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
1980 return 0;
1981}
1982
Alexander Couzensc4704762021-02-08 23:13:12 +01001983/* Update SNS weights
1984 * \param[in] nsvc the NSVC which should be updated
1985 */
1986void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
1987{
1988 /* TODO: implement weights after binds per sns implemented */
1989}
1990
Harald Welte4f127462021-03-02 20:49:10 +01001991
1992
1993
1994/***********************************************************************
1995 * SGSN role
1996 ***********************************************************************/
1997
1998static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1999{
2000 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2001 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2002 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
2003}
2004
2005/* We're waiting for inbound SNS-CONFIG from the BSS */
2006static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2007{
2008 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2009 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2010 struct gprs_ns2_inst *nsi = nse->nsi;
2011 uint8_t cause;
2012 int rc;
2013
2014 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2015
2016 switch (event) {
2017 case GPRS_SNS_EV_RX_CONFIG:
2018 case GPRS_SNS_EV_RX_CONFIG_END:
2019 rc = ns_sns_append_remote_eps(fi, data);
2020 if (rc < 0) {
2021 cause = -rc;
2022 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2023 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2024 return;
2025 }
2026 /* only change state if last CONFIG was received */
2027 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
2028 /* ensure sum of data weight / sig weights is > 0 */
2029 if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) {
2030 cause = NS_CAUSE_INVAL_WEIGH;
2031 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2032 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2033 break;
2034 }
2035 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2036 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2037 } else {
2038 /* just send CONFIG-ACK */
2039 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2040 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2041 }
2042 break;
2043 }
2044}
2045
2046static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2047{
2048 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2049 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2050
Harald Welte4f127462021-03-02 20:49:10 +01002051 /* transmit SGSN-oriented SNS-CONFIG */
2052 ns2_tx_sns_config(gss->sns_nsvc, true, gss->ip4_local, gss->num_ip4_local,
2053 gss->ip6_local, gss->num_ip6_local);
2054}
2055
2056/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2057static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2058{
2059 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2060 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2061 struct tlv_parsed *tp = NULL;
2062
2063 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2064
2065 switch (event) {
2066 case GPRS_SNS_EV_RX_CONFIG_ACK:
2067 tp = data;
2068 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2069 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2070 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2071 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2072 break;
2073 }
2074 /* we currently only send one SNS-CONFIG with END FLAG */
2075 if (true) {
2076 create_missing_nsvcs(fi);
2077 /* start the test procedure on ALL NSVCs! */
2078 gprs_ns2_start_alive_all_nsvcs(nse);
2079 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2080 }
2081 break;
2082 }
2083}
2084
2085/* SGSN-side SNS state machine */
2086static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2087 [GPRS_SNS_ST_UNCONFIGURED] = {
2088 .in_event_mask = 0, /* handled by all_state_action */
2089 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2090 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2091 .name = "UNCONFIGURED",
2092 .action = ns2_sns_st_sgsn_unconfigured,
2093 },
2094 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
2095 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
2096 S(GPRS_SNS_EV_RX_CONFIG_END),
2097 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2098 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2099 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2100 .name = "SGSN_WAIT_CONFIG",
2101 .action = ns2_sns_st_sgsn_wait_config,
2102 },
2103 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
2104 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
2105 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2106 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2107 S(GPRS_SNS_ST_CONFIGURED),
2108 .name = "SGSN_WAIT_CONFIG_ACK",
2109 .action = ns2_sns_st_sgsn_wait_config_ack,
2110 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2111 },
2112 [GPRS_SNS_ST_CONFIGURED] = {
2113 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
2114 S(GPRS_SNS_EV_RX_DELETE) |
2115 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
2116 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
2117 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2118 .name = "CONFIGURED",
2119 /* shared with BSS side; once configured there's no difference */
2120 .action = ns2_sns_st_configured,
2121 .onenter = ns2_sns_st_configured_onenter,
2122 },
2123};
2124
2125static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2126{
2127 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2128 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2129 struct gprs_ns2_inst *nsi = nse->nsi;
2130
2131 gss->N++;
2132 switch (fi->T) {
2133 case 3:
2134 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2135 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2136 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2137 } else {
2138 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2139 }
2140 break;
2141 case 4:
2142 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2143 break;
2144 }
2145 return 0;
2146}
2147
2148
2149/* allstate-action for SGSN role */
2150static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2151{
2152 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2153 struct tlv_parsed *tp = NULL;
Harald Welte01fa6a32021-03-04 19:49:38 +01002154 size_t num_local_eps, num_remote_eps;
Harald Welte4f127462021-03-02 20:49:10 +01002155 uint8_t flag;
Harald Weltea2c5af52021-03-04 17:59:35 +01002156 uint8_t cause;
Harald Welte4f127462021-03-02 20:49:10 +01002157
2158 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2159
2160 switch (event) {
2161 case GPRS_SNS_EV_RX_SIZE:
2162 tp = (struct tlv_parsed *) data;
Harald Weltea2c5af52021-03-04 17:59:35 +01002163 /* check for mandatory / conditional IEs */
2164 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) ||
2165 !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) {
2166 cause = NS_CAUSE_MISSING_ESSENT_IE;
2167 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2168 break;
2169 }
2170 if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) &&
2171 !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) {
2172 cause = NS_CAUSE_MISSING_ESSENT_IE;
Harald Welte4f127462021-03-02 20:49:10 +01002173 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2174 break;
2175 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002176 if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2))
2177 gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR);
2178 if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2))
2179 gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR);
2180 /* decide if we go for IPv4 or IPv6 */
2181 if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, IPv6)) {
2182 gss->ip = IPv6;
Harald Welte2d807b62021-03-24 01:57:30 +01002183 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte01fa6a32021-03-04 19:49:38 +01002184 num_local_eps = gss->num_ip6_local;
2185 num_remote_eps = gss->num_max_ip6_remote;
2186 } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, IPv4)) {
2187 gss->ip = IPv4;
Harald Welte2d807b62021-03-24 01:57:30 +01002188 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte01fa6a32021-03-04 19:49:38 +01002189 num_local_eps = gss->num_ip4_local;
2190 num_remote_eps = gss->num_max_ip4_remote;
2191 } else {
2192 if (gss->num_ip4_local && !gss->num_max_ip4_remote)
2193 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
2194 else
2195 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
2196 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2197 break;
2198 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002199 /* ensure number of NS-VCs is sufficient for full mesh */
2200 gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC);
2201 if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) {
2202 LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, "
2203 "but BSS supports only %zu maximum NS-VCs\n", num_local_eps,
2204 num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs);
2205 cause = NS_CAUSE_INVAL_NR_NS_VC;
2206 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2207 break;
2208 }
2209 /* perform state reset, if requested */
Harald Welte4f127462021-03-02 20:49:10 +01002210 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2211 if (flag & 1) {
2212 struct gprs_ns2_vc *nsvc, *nsvc2;
2213 /* clear all state */
Harald Welte46eb7642021-03-04 17:49:59 +01002214 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
Harald Welte4f127462021-03-02 20:49:10 +01002215 gss->N = 0;
Harald Welte46eb7642021-03-04 17:49:59 +01002216 ns2_clear_ipv46_entries_local(gss);
2217 ns2_clear_ipv46_entries_remote(gss);
Harald Welte4f127462021-03-02 20:49:10 +01002218 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2219 if (nsvc == gss->sns_nsvc) {
2220 /* keep the NSVC we need for SNS, but unconfigure it */
2221 nsvc->sig_weight = 0;
2222 nsvc->data_weight = 0;
2223 ns2_vc_force_unconfigured(nsvc);
2224 } else {
2225 /* free all other NS-VCs */
2226 gprs_ns2_free_nsvc(nsvc);
2227 }
2228 }
Harald Welte2d807b62021-03-24 01:57:30 +01002229 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte4f127462021-03-02 20:49:10 +01002230 }
2231 /* send SIZE_ACK */
2232 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2233 /* only wait for SNS-CONFIG in case of Reset flag */
2234 if (flag & 1)
2235 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2236 break;
2237 default:
2238 ns2_sns_st_all_action(fi, event, data);
2239 break;
2240 }
2241}
2242
2243static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2244 .name = "GPRS-NS2-SNS-SGSN",
2245 .states = ns2_sns_sgsn_states,
2246 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
2247 .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) |
2248 S(GPRS_SNS_EV_REQ_NO_NSVC) |
2249 S(GPRS_SNS_EV_REQ_ADD_BIND) |
2250 S(GPRS_SNS_EV_REQ_DELETE_BIND),
2251 .allstate_action = ns2_sns_st_all_action_sgsn,
2252 .cleanup = NULL,
2253 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2254 .event_names = gprs_sns_event_names,
2255 .pre_term = NULL,
2256 .log_subsys = DLNS,
2257};
2258
2259/*! Allocate an IP-SNS FSM for the SGSN side.
2260 * \param[in] nse NS Entity in which the FSM runs
2261 * \param[in] id string identifier
2262 * \returns FSM instance on success; NULL on error */
2263struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2264{
2265 struct osmo_fsm_inst *fi;
2266 struct ns2_sns_state *gss;
2267
2268 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2269 if (!fi)
2270 return fi;
2271
2272 gss = talloc_zero(fi, struct ns2_sns_state);
2273 if (!gss)
2274 goto err;
2275
2276 fi->priv = gss;
2277 gss->nse = nse;
2278 gss->role = GPRS_SNS_ROLE_SGSN;
2279 INIT_LLIST_HEAD(&gss->sns_endpoints);
2280 INIT_LLIST_HEAD(&gss->binds);
2281
2282 return fi;
2283err:
2284 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2285 return NULL;
2286}
2287
2288
2289
2290
Alexander Couzens6a161492020-07-12 13:45:50 +02002291/* initialize osmo_ctx on main tread */
2292static __attribute__((constructor)) void on_dso_load_ctx(void)
2293{
2294 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002295 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002296}