blob: 367f77c00916e25a3131ca89653747986e661b85 [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
Harald Welte4f127462021-03-02 20:49:10 +010059enum ns2_sns_role {
60 GPRS_SNS_ROLE_BSS,
61 GPRS_SNS_ROLE_SGSN,
62};
63
Harald Welte694dad52021-03-23 15:22:16 +010064/* BSS-side-only states _ST_BSS_; SGSN-side only states _ST_SGSN_; others shared */
Alexander Couzens6a161492020-07-12 13:45:50 +020065enum gprs_sns_bss_state {
66 GPRS_SNS_ST_UNCONFIGURED,
Harald Welte694dad52021-03-23 15:22:16 +010067 GPRS_SNS_ST_BSS_SIZE, /*!< SNS-SIZE procedure ongoing */
68 GPRS_SNS_ST_BSS_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */
69 GPRS_SNS_ST_BSS_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */
Alexander Couzens6a161492020-07-12 13:45:50 +020070 GPRS_SNS_ST_CONFIGURED,
Harald Welte4f127462021-03-02 20:49:10 +010071 GPRS_SNS_ST_SGSN_WAIT_CONFIG, /* !< SGSN role: Wait for CONFIG from BSS */
72 GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, /* !< SGSN role: Wait for CONFIG-ACK from BSS */
Alexander Couzens6a161492020-07-12 13:45:50 +020073};
74
75enum gprs_sns_event {
Alexander Couzens67725e22021-02-15 02:37:03 +010076 GPRS_SNS_EV_REQ_SELECT_ENDPOINT, /*!< Select a SNS endpoint from the list */
77 GPRS_SNS_EV_RX_SIZE,
78 GPRS_SNS_EV_RX_SIZE_ACK,
79 GPRS_SNS_EV_RX_CONFIG,
80 GPRS_SNS_EV_RX_CONFIG_END, /*!< SNS-CONFIG with end flag received */
81 GPRS_SNS_EV_RX_CONFIG_ACK,
82 GPRS_SNS_EV_RX_ADD,
83 GPRS_SNS_EV_RX_DELETE,
84 GPRS_SNS_EV_RX_CHANGE_WEIGHT,
Harald Welteb9f23872021-03-02 20:48:54 +010085 GPRS_SNS_EV_RX_ACK, /*!< Rx of SNS-ACK (response to ADD/DELETE/CHG_WEIGHT */
Harald Welte04647e12021-03-02 18:50:40 +010086 GPRS_SNS_EV_REQ_NO_NSVC, /*!< no more NS-VC remaining (all dead) */
Alexander Couzens67725e22021-02-15 02:37:03 +010087 GPRS_SNS_EV_REQ_NSVC_ALIVE, /*!< a NS-VC became alive */
Harald Welte04647e12021-03-02 18:50:40 +010088 GPRS_SNS_EV_REQ_ADD_BIND, /*!< add a new local bind to this NSE */
89 GPRS_SNS_EV_REQ_DELETE_BIND, /*!< remove a local bind from this NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +020090};
91
92static const struct value_string gprs_sns_event_names[] = {
Alexander Couzens67725e22021-02-15 02:37:03 +010093 { GPRS_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" },
94 { GPRS_SNS_EV_RX_SIZE, "RX_SIZE" },
95 { GPRS_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" },
96 { GPRS_SNS_EV_RX_CONFIG, "RX_CONFIG" },
97 { GPRS_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" },
98 { GPRS_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" },
99 { GPRS_SNS_EV_RX_ADD, "RX_ADD" },
100 { GPRS_SNS_EV_RX_DELETE, "RX_DELETE" },
Harald Welteb9f23872021-03-02 20:48:54 +0100101 { GPRS_SNS_EV_RX_ACK, "RX_ACK" },
Alexander Couzens67725e22021-02-15 02:37:03 +0100102 { GPRS_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" },
103 { GPRS_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" },
104 { GPRS_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"},
105 { GPRS_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"},
106 { GPRS_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"},
Alexander Couzens6a161492020-07-12 13:45:50 +0200107 { 0, NULL }
108};
109
Alexander Couzense769f522020-12-07 07:37:07 +0100110struct sns_endpoint {
111 struct llist_head list;
112 struct osmo_sockaddr saddr;
113};
114
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100115struct ns2_sns_bind {
116 struct llist_head list;
117 struct gprs_ns2_vc_bind *bind;
118};
119
Alexander Couzens71128672021-06-05 18:44:01 +0200120struct ns2_sns_elems {
121 struct gprs_ns_ie_ip4_elem *ip4;
122 unsigned int num_ip4;
123 struct gprs_ns_ie_ip6_elem *ip6;
124 unsigned int num_ip6;
125};
126
Alexander Couzens6a161492020-07-12 13:45:50 +0200127struct ns2_sns_state {
128 struct gprs_ns2_nse *nse;
129
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200130 /* containing the address family AF_* */
131 int family;
Harald Welte4f127462021-03-02 20:49:10 +0100132 enum ns2_sns_role role; /* local role: BSS or SGSN */
Alexander Couzens6a161492020-07-12 13:45:50 +0200133
Alexander Couzense769f522020-12-07 07:37:07 +0100134 /* holds the list of initial SNS endpoints */
135 struct llist_head sns_endpoints;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100136 /* list of used struct ns2_sns_bind */
137 struct llist_head binds;
138 /* pointer to the bind which was used to initiate the SNS connection */
139 struct ns2_sns_bind *initial_bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100140 /* prevent recursive reselection */
141 bool reselection_running;
142
143 /* The current initial SNS endpoints.
144 * The initial connection will be moved into the NSE
145 * if configured via SNS. Otherwise it will be removed
146 * in configured state. */
147 struct sns_endpoint *initial;
Alexander Couzens6a161492020-07-12 13:45:50 +0200148 /* all SNS PDU will be sent over this nsvc */
149 struct gprs_ns2_vc *sns_nsvc;
Alexander Couzens90ee9632020-12-07 06:18:32 +0100150 /* timer N */
151 int N;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100152 /* true if at least one nsvc is alive */
153 bool alive;
Alexander Couzens6a161492020-07-12 13:45:50 +0200154
155 /* local configuration to send to the remote end */
Alexander Couzens71128672021-06-05 18:44:01 +0200156 struct ns2_sns_elems local;
Alexander Couzens6a161492020-07-12 13:45:50 +0200157
Alexander Couzens71128672021-06-05 18:44:01 +0200158 /* remote configuration as received */
159 struct ns2_sns_elems remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200160
161 /* local configuration about our capabilities in terms of connections to
162 * remote (SGSN) side */
163 size_t num_max_nsvcs;
164 size_t num_max_ip4_remote;
165 size_t num_max_ip6_remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200166};
167
168static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi)
169{
170 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
171 return gss->nse;
172}
173
174/* helper function to compute the sum of all (data or signaling) weights */
Alexander Couzens62310552021-06-06 02:43:14 +0200175static int ip4_weight_sum(const struct ns2_sns_elems *elems, bool data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200176{
177 unsigned int i;
178 int weight_sum = 0;
179
Alexander Couzens62310552021-06-06 02:43:14 +0200180 for (i = 0; i < elems->num_ip4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200181 if (data_weight)
Alexander Couzens62310552021-06-06 02:43:14 +0200182 weight_sum += elems->ip4[i].data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200183 else
Alexander Couzens62310552021-06-06 02:43:14 +0200184 weight_sum += elems->ip4[i].sig_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200185 }
186 return weight_sum;
187}
Alexander Couzens62310552021-06-06 02:43:14 +0200188#define ip4_weight_sum_data(elems) ip4_weight_sum(elems, true)
189#define ip4_weight_sum_sig(elems) ip4_weight_sum(elems, false)
Alexander Couzens6a161492020-07-12 13:45:50 +0200190
191/* helper function to compute the sum of all (data or signaling) weights */
Alexander Couzens62310552021-06-06 02:43:14 +0200192static int ip6_weight_sum(const struct ns2_sns_elems *elems, bool data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200193{
194 unsigned int i;
195 int weight_sum = 0;
196
Alexander Couzens62310552021-06-06 02:43:14 +0200197 for (i = 0; i < elems->num_ip6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200198 if (data_weight)
Alexander Couzens62310552021-06-06 02:43:14 +0200199 weight_sum += elems->ip6[i].data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200200 else
Alexander Couzens62310552021-06-06 02:43:14 +0200201 weight_sum += elems->ip6[i].sig_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200202 }
203 return weight_sum;
204}
Alexander Couzens62310552021-06-06 02:43:14 +0200205#define ip6_weight_sum_data(elems) ip6_weight_sum(elems, true)
206#define ip6_weight_sum_sig(elems) ip6_weight_sum(elems, false)
Alexander Couzens6a161492020-07-12 13:45:50 +0200207
Alexander Couzens019da4b2021-06-06 02:48:18 +0200208static int ip46_weight_sum(const struct ns2_sns_elems *elems, bool data_weight)
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100209{
Alexander Couzens019da4b2021-06-06 02:48:18 +0200210 return ip4_weight_sum(elems, data_weight) +
211 ip6_weight_sum(elems, data_weight);
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100212}
Alexander Couzens019da4b2021-06-06 02:48:18 +0200213#define ip46_weight_sum_data(elems) ip46_weight_sum(elems, true)
214#define ip46_weight_sum_sig(elems) ip46_weight_sum(elems, false)
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100215
Alexander Couzens6a161492020-07-12 13:45:50 +0200216static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse,
217 const struct gprs_ns_ie_ip4_elem *ip4)
218{
219 struct osmo_sockaddr sa;
220 /* copy over. Both data structures use network byte order */
221 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
222 sa.u.sin.sin_port = ip4->udp_port;
223 sa.u.sin.sin_family = AF_INET;
224
Alexander Couzens38b19e82020-09-23 23:56:37 +0200225 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200226}
227
228static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse,
229 const struct gprs_ns_ie_ip6_elem *ip6)
230{
231 struct osmo_sockaddr sa;
232 /* copy over. Both data structures use network byte order */
233 sa.u.sin6.sin6_addr = ip6->ip_addr;
234 sa.u.sin6.sin6_port = ip6->udp_port;
235 sa.u.sin6.sin6_family = AF_INET;
236
Alexander Couzens38b19e82020-09-23 23:56:37 +0200237 return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa);
Alexander Couzens6a161492020-07-12 13:45:50 +0200238}
239
Alexander Couzens125298f2020-10-11 21:22:42 +0200240/*! Return the initial SNS remote socket address
241 * \param nse NS Entity
242 * \return address of the initial SNS connection; NULL in case of error
243 */
244const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse)
245{
246 struct ns2_sns_state *gss;
247
248 if (!nse->bss_sns_fi)
249 return NULL;
250
251 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100252 return &gss->initial->saddr;
Alexander Couzens125298f2020-10-11 21:22:42 +0200253}
254
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100255/*! called when a nsvc is beeing freed or the nsvc became dead */
256void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200257{
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100258 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +0200259 struct gprs_ns2_vc *tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100260 struct osmo_fsm_inst *fi = nse->bss_sns_fi;
Alexander Couzens6a161492020-07-12 13:45:50 +0200261 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +0200262
263 if (!fi)
264 return;
265
266 gss = (struct ns2_sns_state *) fi->priv;
267 if (nsvc != gss->sns_nsvc)
268 return;
269
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100270 gss->sns_nsvc = NULL;
271 if (gss->alive) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200272 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100273 if (ns2_vc_is_unblocked(tmp)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200274 gss->sns_nsvc = tmp;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100275 return;
276 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200277 }
278 } else {
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100279 /* the SNS is waiting for its first NS-VC to come up
280 * choose any other nsvc */
281 llist_for_each_entry(tmp, &nse->nsvc, list) {
282 if (nsvc != tmp) {
283 gss->sns_nsvc = tmp;
284 return;
285 }
286 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200287 }
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100288
Alexander Couzens67725e22021-02-15 02:37:03 +0100289 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzens6a161492020-07-12 13:45:50 +0200290}
291
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200292static void ns2_clear_elems(struct ns2_sns_elems *elems)
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100293{
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200294 TALLOC_FREE(elems->ip4);
295 TALLOC_FREE(elems->ip6);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100296
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200297 elems->num_ip4 = 0;
298 elems->num_ip6 = 0;
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100299}
300
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100301static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote,
302 uint8_t sig_weight, uint8_t data_weight)
Alexander Couzens6a161492020-07-12 13:45:50 +0200303{
304 struct gprs_ns2_inst *nsi = nse->nsi;
305 struct gprs_ns2_vc *nsvc;
306 struct gprs_ns2_vc_bind *bind;
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100307
308 /* for every bind, create a connection if bind type == IP */
309 llist_for_each_entry(bind, &nsi->binding, list) {
310 if (bind->ll != GPRS_NS2_LL_UDP)
311 continue;
312 /* ignore failed connection */
313 nsvc = gprs_ns2_ip_connect_inactive(bind,
314 remote,
315 nse, 0);
316 if (!nsvc) {
317 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n");
318 continue;
319 }
320
321 nsvc->sig_weight = sig_weight;
322 nsvc->data_weight = data_weight;
323 }
324}
325
326static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi,
327 struct gprs_ns2_nse *nse,
328 const struct gprs_ns_ie_ip4_elem *ip4)
329{
Alexander Couzensc068d862020-10-12 04:11:51 +0200330 struct osmo_sockaddr remote = { };
Alexander Couzens6a161492020-07-12 13:45:50 +0200331 /* copy over. Both data structures use network byte order */
332 remote.u.sin.sin_family = AF_INET;
333 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
334 remote.u.sin.sin_port = ip4->udp_port;
335
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100336 ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200337}
338
339static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi,
340 struct gprs_ns2_nse *nse,
341 const struct gprs_ns_ie_ip6_elem *ip6)
342{
Alexander Couzens6a161492020-07-12 13:45:50 +0200343 struct osmo_sockaddr remote = {};
344 /* copy over. Both data structures use network byte order */
345 remote.u.sin6.sin6_family = AF_INET6;
346 remote.u.sin6.sin6_addr = ip6->ip_addr;
347 remote.u.sin6.sin6_port = ip6->udp_port;
348
Daniel Willmanncf8371a2021-01-16 15:13:51 +0100349 ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight);
Alexander Couzens6a161492020-07-12 13:45:50 +0200350}
351
Harald Weltee8c61062021-03-24 13:16:27 +0100352static struct gprs_ns2_vc *nsvc_for_bind_and_remote(struct gprs_ns2_nse *nse,
353 struct gprs_ns2_vc_bind *bind,
354 const struct osmo_sockaddr *remote)
355{
356 struct gprs_ns2_vc *nsvc;
357
358 llist_for_each_entry(nsvc, &nse->nsvc, list) {
359 if (nsvc->bind != bind)
360 continue;
361
362 if (!osmo_sockaddr_cmp(remote, gprs_ns2_ip_vc_remote(nsvc)))
363 return nsvc;
364 }
365 return NULL;
366}
Alexander Couzens6a161492020-07-12 13:45:50 +0200367
368static int create_missing_nsvcs(struct osmo_fsm_inst *fi)
369{
370 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
371 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
372 struct gprs_ns2_vc *nsvc;
Harald Welte3053bbb2021-03-24 13:22:18 +0100373 struct ns2_sns_bind *sbind;
Alexander Couzens6a161492020-07-12 13:45:50 +0200374 struct osmo_sockaddr remote = { };
375 unsigned int i;
376
Harald Weltee8c61062021-03-24 13:16:27 +0100377 /* iterate over all remote IPv4 endpoints */
Alexander Couzens71128672021-06-05 18:44:01 +0200378 for (i = 0; i < gss->remote.num_ip4; i++) {
379 const struct gprs_ns_ie_ip4_elem *ip4 = &gss->remote.ip4[i];
Alexander Couzens6a161492020-07-12 13:45:50 +0200380
381 remote.u.sin.sin_family = AF_INET;
382 remote.u.sin.sin_addr.s_addr = ip4->ip_addr;
383 remote.u.sin.sin_port = ip4->udp_port;
384
Harald Welte3053bbb2021-03-24 13:22:18 +0100385 /* iterate over all local binds within this SNS */
386 llist_for_each_entry(sbind, &gss->binds, list) {
387 struct gprs_ns2_vc_bind *bind = sbind->bind;
388
Harald Weltee8c61062021-03-24 13:16:27 +0100389 /* we only care about UDP binds */
Daniel Willmann967e2c12021-01-14 16:58:17 +0100390 if (bind->ll != GPRS_NS2_LL_UDP)
391 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200392
Harald Weltee8c61062021-03-24 13:16:27 +0100393 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
394 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200395 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
396 if (!nsvc) {
397 /* TODO: add to a list to send back a NS-STATUS */
398 continue;
399 }
400 }
401
402 /* update data / signalling weight */
403 nsvc->data_weight = ip4->data_weight;
404 nsvc->sig_weight = ip4->sig_weight;
405 nsvc->sns_only = false;
406 }
407 }
408
Harald Weltee8c61062021-03-24 13:16:27 +0100409 /* iterate over all remote IPv4 endpoints */
Alexander Couzens71128672021-06-05 18:44:01 +0200410 for (i = 0; i < gss->remote.num_ip6; i++) {
411 const struct gprs_ns_ie_ip6_elem *ip6 = &gss->remote.ip6[i];
Alexander Couzens6a161492020-07-12 13:45:50 +0200412
413 remote.u.sin6.sin6_family = AF_INET6;
414 remote.u.sin6.sin6_addr = ip6->ip_addr;
415 remote.u.sin6.sin6_port = ip6->udp_port;
416
Harald Welte3053bbb2021-03-24 13:22:18 +0100417 /* iterate over all local binds within this SNS */
418 llist_for_each_entry(sbind, &gss->binds, list) {
419 struct gprs_ns2_vc_bind *bind = sbind->bind;
420
Daniel Willmann967e2c12021-01-14 16:58:17 +0100421 if (bind->ll != GPRS_NS2_LL_UDP)
422 continue;
Alexander Couzens6a161492020-07-12 13:45:50 +0200423
Harald Weltee8c61062021-03-24 13:16:27 +0100424 /* we only care about UDP binds */
425 nsvc = nsvc_for_bind_and_remote(nse, bind, &remote);
426 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200427 nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0);
428 if (!nsvc) {
429 /* TODO: add to a list to send back a NS-STATUS */
430 continue;
431 }
432 }
433
434 /* update data / signalling weight */
435 nsvc->data_weight = ip6->data_weight;
436 nsvc->sig_weight = ip6->sig_weight;
437 nsvc->sns_only = false;
438 }
439 }
440
441
442 return 0;
443}
444
445/* Add a given remote IPv4 element to gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200446static int add_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
447 const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +0200448{
Alexander Couzens6a161492020-07-12 13:45:50 +0200449 /* check for duplicates */
Alexander Couzenscc56ddc2021-06-06 03:33:35 +0200450 for (unsigned int i = 0; i < elems->num_ip4; i++) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200451 if (memcmp(&elems->ip4[i], ip4, sizeof(*ip4)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200452 continue;
Alexander Couzensd3507e82021-06-06 03:32:32 +0200453 return -1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200454 }
455
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200456 elems->ip4 = talloc_realloc(gss, elems->ip4, struct gprs_ns_ie_ip4_elem,
457 elems->num_ip4+1);
458 elems->ip4[elems->num_ip4] = *ip4;
459 elems->num_ip4 += 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200460 return 0;
461}
462
463/* Remove a given remote IPv4 element from gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200464static int remove_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
465 const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +0200466{
467 unsigned int i;
468
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200469 for (i = 0; i < elems->num_ip4; i++) {
470 if (memcmp(&elems->ip4[i], ip4, sizeof(*ip4)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200471 continue;
472 /* all array elements < i remain as they are; all > i are shifted left by one */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200473 memmove(&elems->ip4[i], &elems->ip4[i+1], elems->num_ip4-i-1);
474 elems->num_ip4 -= 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200475 return 0;
476 }
477 return -1;
478}
479
480/* update the weights for specified remote IPv4 */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200481static int update_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
482 const struct gprs_ns_ie_ip4_elem *ip4)
Alexander Couzens6a161492020-07-12 13:45:50 +0200483{
484 unsigned int i;
485
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200486 for (i = 0; i < elems->num_ip4; i++) {
487 if (elems->ip4[i].ip_addr != ip4->ip_addr ||
488 elems->ip4[i].udp_port != ip4->udp_port)
Alexander Couzens6a161492020-07-12 13:45:50 +0200489 continue;
490
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200491 elems->ip4[i].sig_weight = ip4->sig_weight;
492 elems->ip4[i].data_weight = ip4->data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200493 return 0;
494 }
495 return -1;
496}
497
498/* Add a given remote IPv6 element to gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200499static int add_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
500 const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200501{
Alexander Couzenscc56ddc2021-06-06 03:33:35 +0200502 /* check for duplicates */
503 for (unsigned int i = 0; i < elems->num_ip6; i++) {
504 if (memcmp(&elems->ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
505 elems->ip6[i].udp_port != ip6->udp_port)
506 continue;
507 return -1;
508 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200509
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200510 elems->ip6 = talloc_realloc(gss, elems->ip6, struct gprs_ns_ie_ip6_elem,
511 elems->num_ip6+1);
512 elems->ip6[elems->num_ip6] = *ip6;
513 elems->num_ip6 += 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200514 return 0;
515}
516
517/* Remove a given remote IPv6 element from gprs_sns_state */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200518static int remove_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
519 const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200520{
521 unsigned int i;
522
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200523 for (i = 0; i < elems->num_ip6; i++) {
524 if (memcmp(&elems->ip6[i], ip6, sizeof(*ip6)))
Alexander Couzens6a161492020-07-12 13:45:50 +0200525 continue;
526 /* all array elements < i remain as they are; all > i are shifted left by one */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200527 memmove(&elems->ip6[i], &elems->ip6[i+1], elems->num_ip6-i-1);
528 elems->num_ip6 -= 1;
Alexander Couzens6a161492020-07-12 13:45:50 +0200529 return 0;
530 }
531 return -1;
532}
533
534/* update the weights for specified remote IPv6 */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200535static int update_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems,
536 const struct gprs_ns_ie_ip6_elem *ip6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200537{
538 unsigned int i;
539
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200540 for (i = 0; i < elems->num_ip6; i++) {
541 if (memcmp(&elems->ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) ||
542 elems->ip6[i].udp_port != ip6->udp_port)
Alexander Couzens6a161492020-07-12 13:45:50 +0200543 continue;
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200544 elems->ip6[i].sig_weight = ip6->sig_weight;
545 elems->ip6[i].data_weight = ip6->data_weight;
Alexander Couzens6a161492020-07-12 13:45:50 +0200546 return 0;
547 }
548 return -1;
549}
550
551static 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)
552{
553 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
554 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
555 struct gprs_ns2_vc *nsvc;
556 struct osmo_sockaddr sa = {};
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200557 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200558 uint8_t new_signal;
559 uint8_t new_data;
560
561 /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the
562 * signalling weights of all the peer IP endpoints configured for this NSE is
563 * equal to zero or if the resulting sum of the data weights of all the peer IP
564 * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an
565 * SNS-ACK PDU with a cause code of "Invalid weights". */
566
567 if (ip4) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200568 if (update_ip4_elem(gss, &gss->remote, ip4))
Alexander Couzens6a161492020-07-12 13:45:50 +0200569 return -NS_CAUSE_UNKN_IP_EP;
570
571 /* copy over. Both data structures use network byte order */
572 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
573 sa.u.sin.sin_port = ip4->udp_port;
574 sa.u.sin.sin_family = AF_INET;
575 new_signal = ip4->sig_weight;
576 new_data = ip4->data_weight;
577 } else if (ip6) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200578 if (update_ip6_elem(gss, &gss->remote, ip6))
Alexander Couzens6a161492020-07-12 13:45:50 +0200579 return -NS_CAUSE_UNKN_IP_EP;
580
581 /* copy over. Both data structures use network byte order */
582 sa.u.sin6.sin6_addr = ip6->ip_addr;
583 sa.u.sin6.sin6_port = ip6->udp_port;
584 sa.u.sin6.sin6_family = AF_INET6;
585 new_signal = ip6->sig_weight;
586 new_data = ip6->data_weight;
587 } else {
588 OSMO_ASSERT(false);
589 }
590
591 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200592 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200593 /* all nsvc in NSE should be IP/UDP nsvc */
594 OSMO_ASSERT(remote);
595
596 if (osmo_sockaddr_cmp(&sa, remote))
597 continue;
598
599 LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n",
600 gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data,
601 nsvc->sig_weight, new_signal);
602
603 nsvc->data_weight = new_data;
604 nsvc->sig_weight = new_signal;
605 }
606
607 return 0;
608}
609
610static int do_sns_delete(struct osmo_fsm_inst *fi,
611 const struct gprs_ns_ie_ip4_elem *ip4,
612 const struct gprs_ns_ie_ip6_elem *ip6)
613{
614 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
615 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
616 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200617 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200618 struct osmo_sockaddr sa = {};
619
620 if (ip4) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200621 if (remove_ip4_elem(gss, &gss->remote, ip4) < 0)
Alexander Couzens6a161492020-07-12 13:45:50 +0200622 return -NS_CAUSE_UNKN_IP_EP;
623 /* copy over. Both data structures use network byte order */
624 sa.u.sin.sin_addr.s_addr = ip4->ip_addr;
625 sa.u.sin.sin_port = ip4->udp_port;
626 sa.u.sin.sin_family = AF_INET;
627 } else if (ip6) {
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200628 if (remove_ip6_elem(gss, &gss->remote, ip6))
Alexander Couzens6a161492020-07-12 13:45:50 +0200629 return -NS_CAUSE_UNKN_IP_EP;
630
631 /* copy over. Both data structures use network byte order */
632 sa.u.sin6.sin6_addr = ip6->ip_addr;
633 sa.u.sin6.sin6_port = ip6->udp_port;
634 sa.u.sin6.sin6_family = AF_INET6;
635 } else {
636 OSMO_ASSERT(false);
637 }
638
639 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200640 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200641 /* all nsvc in NSE should be IP/UDP nsvc */
642 OSMO_ASSERT(remote);
643 if (osmo_sockaddr_cmp(&sa, remote))
644 continue;
645
646 LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc));
647 gprs_ns2_free_nsvc(nsvc);
648 }
649
650 return 0;
651}
652
653static int do_sns_add(struct osmo_fsm_inst *fi,
654 const struct gprs_ns_ie_ip4_elem *ip4,
655 const struct gprs_ns_ie_ip6_elem *ip6)
656{
657 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
658 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
659 struct gprs_ns2_vc *nsvc;
660 int rc = 0;
661
662 /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints
663 * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send
664 * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200665 switch (gss->family) {
666 case AF_INET:
Alexander Couzensd3507e82021-06-06 03:32:32 +0200667 if (gss->remote.num_ip4 >= gss->num_max_ip4_remote)
668 return -NS_CAUSE_INVAL_NR_NS_VC;
669 /* TODO: log message duplicate */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200670 rc = add_ip4_elem(gss, &gss->remote, ip4);
Alexander Couzens6a161492020-07-12 13:45:50 +0200671 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200672 case AF_INET6:
Alexander Couzensd3507e82021-06-06 03:32:32 +0200673 if (gss->remote.num_ip6 >= gss->num_max_ip6_remote)
674 return -NS_CAUSE_INVAL_NR_NS_VC;
675 /* TODO: log message duplicate */
Alexander Couzensc2ba25e2021-06-06 02:26:40 +0200676 rc = add_ip6_elem(gss, &gss->remote, ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200677 break;
678 default:
679 /* the gss->ip is initialized with the bss */
680 OSMO_ASSERT(false);
681 }
682
683 if (rc)
Alexander Couzensd3507e82021-06-06 03:32:32 +0200684 return -NS_CAUSE_PROTO_ERR_UNSPEC;
Alexander Couzens6a161492020-07-12 13:45:50 +0200685
686 /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the
687 * NSE shall send an SNS-ACK PDU with the cause code "Protocol error -
688 * unspecified" */
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200689 switch (gss->family) {
690 case AF_INET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200691 nsvc = nsvc_by_ip4_elem(nse, ip4);
692 if (nsvc) {
693 /* the nsvc should be already in sync with the ip4 / ip6 elements */
694 return -NS_CAUSE_PROTO_ERR_UNSPEC;
695 }
696
697 /* TODO: failure case */
698 ns2_nsvc_create_ip4(fi, nse, ip4);
699 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200700 case AF_INET6:
Alexander Couzens6a161492020-07-12 13:45:50 +0200701 nsvc = nsvc_by_ip6_elem(nse, ip6);
702 if (nsvc) {
703 /* the nsvc should be already in sync with the ip4 / ip6 elements */
704 return -NS_CAUSE_PROTO_ERR_UNSPEC;
705 }
706
707 /* TODO: failure case */
708 ns2_nsvc_create_ip6(fi, nse, ip6);
709 break;
710 }
711
712 gprs_ns2_start_alive_all_nsvcs(nse);
713
714 return 0;
715}
716
717
Harald Welte694dad52021-03-23 15:22:16 +0100718static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200719{
Harald Weltef61a9152021-03-02 22:20:17 +0100720 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
721 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzense769f522020-12-07 07:37:07 +0100722 /* empty state - SNS Select will start by ns2_sns_st_all_action() */
Alexander Couzens6a161492020-07-12 13:45:50 +0200723}
724
Harald Welte694dad52021-03-23 15:22:16 +0100725static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200726{
Harald Weltef61a9152021-03-02 22:20:17 +0100727 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200728 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
729 struct gprs_ns2_inst *nsi = nse->nsi;
730 struct tlv_parsed *tp = NULL;
731
Harald Weltef61a9152021-03-02 22:20:17 +0100732 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
733
Alexander Couzens6a161492020-07-12 13:45:50 +0200734 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100735 case GPRS_SNS_EV_RX_SIZE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200736 tp = data;
737 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
738 LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n",
739 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
740 /* TODO: What to do? */
741 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100742 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200743 nsi->timeout[NS_TOUT_TSNS_PROV], 2);
744 }
745 break;
746 default:
747 OSMO_ASSERT(0);
748 }
749}
750
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200751static int ns2_sns_count_num_local_ep(struct osmo_fsm_inst *fi, int ip_proto)
Harald Welte01fa6a32021-03-04 19:49:38 +0100752{
753 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
754 struct ns2_sns_bind *sbind;
755 int count = 0;
756
757 llist_for_each_entry(sbind, &gss->binds, list) {
758 const struct osmo_sockaddr *sa = gprs_ns2_ip_bind_sockaddr(sbind->bind);
759 if (!sa)
760 continue;
761
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200762 switch (ip_proto) {
763 case AF_INET:
Harald Welte01fa6a32021-03-04 19:49:38 +0100764 if (sa->u.sas.ss_family == AF_INET)
765 count++;
766 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200767 case AF_INET6:
Harald Welte01fa6a32021-03-04 19:49:38 +0100768 if (sa->u.sas.ss_family == AF_INET6)
769 count++;
770 break;
771 }
772 }
773 return count;
774}
775
Harald Welte24920e22021-03-04 13:03:27 +0100776static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi)
Alexander Couzens6a161492020-07-12 13:45:50 +0200777{
778 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzense769f522020-12-07 07:37:07 +0100779 struct gprs_ns_ie_ip4_elem *ip4_elems;
780 struct gprs_ns_ie_ip6_elem *ip6_elems;
781 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100782 struct ns2_sns_bind *sbind;
Harald Welte4f127462021-03-02 20:49:10 +0100783 const struct osmo_sockaddr *remote;
Alexander Couzense769f522020-12-07 07:37:07 +0100784 const struct osmo_sockaddr *sa;
785 struct osmo_sockaddr local;
786 int count;
Alexander Couzens6a161492020-07-12 13:45:50 +0200787
Alexander Couzensd2c6c492021-06-06 01:57:52 +0200788 ns2_clear_elems(&gss->local);
Alexander Couzens7a7b20b2021-01-18 10:47:33 +0100789
Alexander Couzense769f522020-12-07 07:37:07 +0100790 /* no initial available */
Harald Welte4f127462021-03-02 20:49:10 +0100791 if (gss->role == GPRS_SNS_ROLE_BSS) {
792 if (!gss->initial)
793 return;
794 remote = &gss->initial->saddr;
795 } else
796 remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc);
Alexander Couzense769f522020-12-07 07:37:07 +0100797
798 /* count how many bindings are available (only UDP binds) */
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100799 count = llist_count(&gss->binds);
Alexander Couzense769f522020-12-07 07:37:07 +0100800 if (count == 0) {
Harald Welte05992872021-03-04 15:49:21 +0100801 LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n");
Alexander Couzense769f522020-12-07 07:37:07 +0100802 return;
803 }
804
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200805 switch (gss->family) {
806 case AF_INET:
Alexander Couzens71128672021-06-05 18:44:01 +0200807 ip4_elems = talloc_realloc(fi, gss->local.ip4, struct gprs_ns_ie_ip4_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100808 if (!ip4_elems)
809 return;
810
Alexander Couzens71128672021-06-05 18:44:01 +0200811 gss->local.ip4 = ip4_elems;
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100812 llist_for_each_entry(sbind, &gss->binds, list) {
813 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100814 sa = gprs_ns2_ip_bind_sockaddr(bind);
815 if (!sa)
816 continue;
817
818 if (sa->u.sas.ss_family != AF_INET)
819 continue;
820
821 /* check if this is an specific bind */
822 if (sa->u.sin.sin_addr.s_addr == 0) {
823 if (osmo_sockaddr_local_ip(&local, remote))
824 continue;
825
826 ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr;
827 } else {
828 ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr;
829 }
830
831 ip4_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100832 ip4_elems->sig_weight = bind->sns_sig_weight;
833 ip4_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100834 ip4_elems++;
835 }
836
Alexander Couzens71128672021-06-05 18:44:01 +0200837 gss->local.num_ip4 = count;
838 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->local.num_ip4, 8);
Alexander Couzense769f522020-12-07 07:37:07 +0100839 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200840 case AF_INET6:
Alexander Couzense769f522020-12-07 07:37:07 +0100841 /* IPv6 */
Alexander Couzens71128672021-06-05 18:44:01 +0200842 ip6_elems = talloc_realloc(fi, gss->local.ip6, struct gprs_ns_ie_ip6_elem, count);
Alexander Couzense769f522020-12-07 07:37:07 +0100843 if (!ip6_elems)
844 return;
845
Alexander Couzens71128672021-06-05 18:44:01 +0200846 gss->local.ip6 = ip6_elems;
Alexander Couzense769f522020-12-07 07:37:07 +0100847
Alexander Couzens6b9d2322021-02-12 03:17:59 +0100848 llist_for_each_entry(sbind, &gss->binds, list) {
849 bind = sbind->bind;
Alexander Couzense769f522020-12-07 07:37:07 +0100850 sa = gprs_ns2_ip_bind_sockaddr(bind);
851 if (!sa)
852 continue;
853
854 if (sa->u.sas.ss_family != AF_INET6)
855 continue;
856
857 /* check if this is an specific bind */
858 if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) {
859 if (osmo_sockaddr_local_ip(&local, remote))
860 continue;
861
862 ip6_elems->ip_addr = local.u.sin6.sin6_addr;
863 } else {
864 ip6_elems->ip_addr = sa->u.sin6.sin6_addr;
865 }
866
867 ip6_elems->udp_port = sa->u.sin.sin_port;
Alexander Couzensc4704762021-02-08 23:13:12 +0100868 ip6_elems->sig_weight = bind->sns_sig_weight;
869 ip6_elems->data_weight = bind->sns_data_weight;
Alexander Couzense769f522020-12-07 07:37:07 +0100870
871 ip6_elems++;
872 }
Alexander Couzens71128672021-06-05 18:44:01 +0200873 gss->local.num_ip6 = count;
874 gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->local.num_ip6, 8);
Alexander Couzense769f522020-12-07 07:37:07 +0100875 break;
876 }
Harald Welte24920e22021-03-04 13:03:27 +0100877}
878
Alexander Couzens6608ce92021-04-26 20:39:46 +0200879static void ns2_sns_choose_next_bind(struct ns2_sns_state *gss)
880{
881 /* take the first bind or take the next bind */
882 if (!gss->initial_bind || gss->initial_bind->list.next == &gss->binds)
883 gss->initial_bind = llist_first_entry_or_null(&gss->binds, struct ns2_sns_bind, list);
884 else
885 gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list);
886}
887
Harald Welte24920e22021-03-04 13:03:27 +0100888/* setup all dynamic SNS settings, create a new nsvc and send the SIZE */
Harald Welte694dad52021-03-23 15:22:16 +0100889static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Harald Welte24920e22021-03-04 13:03:27 +0100890{
891 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
892
Harald Weltef61a9152021-03-02 22:20:17 +0100893 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
894
Harald Welte24920e22021-03-04 13:03:27 +0100895 /* on a generic failure, the timer callback will recover */
896 if (old_state != GPRS_SNS_ST_UNCONFIGURED)
897 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE);
Harald Welte694dad52021-03-23 15:22:16 +0100898 if (old_state != GPRS_SNS_ST_BSS_SIZE)
Harald Welte24920e22021-03-04 13:03:27 +0100899 gss->N = 0;
900
901 gss->alive = false;
902
903 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens6608ce92021-04-26 20:39:46 +0200904 ns2_sns_choose_next_bind(gss);
Harald Welte24920e22021-03-04 13:03:27 +0100905
906 /* setup the NSVC */
907 if (!gss->sns_nsvc) {
908 struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind;
909 struct osmo_sockaddr *remote = &gss->initial->saddr;
910 gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote);
911 if (!gss->sns_nsvc)
912 return;
Harald Weltec962a2e2021-03-05 08:09:08 +0100913 /* A pre-configured endpoint shall not be used for NSE data or signalling traffic
914 * (with the exception of Size and Configuration procedures) unless it is configured
915 * by the SGSN using the auto-configuration procedures */
Harald Welte24920e22021-03-04 13:03:27 +0100916 gss->sns_nsvc->sns_only = true;
917 }
918
Alexander Couzens6a161492020-07-12 13:45:50 +0200919 if (gss->num_max_ip4_remote > 0)
Alexander Couzens71128672021-06-05 18:44:01 +0200920 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->local.num_ip4, -1);
Alexander Couzens6a161492020-07-12 13:45:50 +0200921 else
Alexander Couzens71128672021-06-05 18:44:01 +0200922 ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->local.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200923}
924
Harald Welte694dad52021-03-23 15:22:16 +0100925static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +0200926{
Harald Weltef61a9152021-03-02 22:20:17 +0100927 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens3df58862021-02-05 17:18:08 +0100928 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Harald Weltef61a9152021-03-02 22:20:17 +0100929 struct tlv_parsed *tp = NULL;
930
931 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
Alexander Couzens6a161492020-07-12 13:45:50 +0200932
933 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +0100934 case GPRS_SNS_EV_RX_CONFIG_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200935 tp = (struct tlv_parsed *) data;
936 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
937 LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n",
938 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
939 /* TODO: What to do? */
940 } else {
Harald Welte694dad52021-03-23 15:22:16 +0100941 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 +0200942 }
943 break;
944 default:
945 OSMO_ASSERT(0);
946 }
947}
948
Harald Welte694dad52021-03-23 15:22:16 +0100949static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens6a161492020-07-12 13:45:50 +0200950{
951 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens790a9632021-02-05 17:18:39 +0100952
Harald Weltef61a9152021-03-02 22:20:17 +0100953 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
954
Harald Welte694dad52021-03-23 15:22:16 +0100955 if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS)
Alexander Couzens790a9632021-02-05 17:18:39 +0100956 gss->N = 0;
957
Alexander Couzens6a161492020-07-12 13:45:50 +0200958 /* Transmit SNS-CONFIG */
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200959 switch (gss->family) {
960 case AF_INET:
Alexander Couzens6a161492020-07-12 13:45:50 +0200961 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzens71128672021-06-05 18:44:01 +0200962 gss->local.ip4, gss->local.num_ip4,
Alexander Couzense78207f2020-12-07 06:19:29 +0100963 NULL, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200964 break;
Alexander Couzens077ce5a2021-06-06 01:32:45 +0200965 case AF_INET6:
Alexander Couzens6a161492020-07-12 13:45:50 +0200966 ns2_tx_sns_config(gss->sns_nsvc, true,
Alexander Couzense78207f2020-12-07 06:19:29 +0100967 NULL, 0,
Alexander Couzens71128672021-06-05 18:44:01 +0200968 gss->local.ip6, gss->local.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +0200969 break;
970 }
971}
972
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100973/* calculate the timeout of the configured state. the configured
974 * state will fail if not at least one NS-VC is alive within X second.
975 */
976static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi)
977{
978 int secs;
979 struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi;
980 secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES];
981 secs += nsi->timeout[NS_TOUT_TNS_TEST];
982
983 return secs;
984}
Alexander Couzens6a161492020-07-12 13:45:50 +0200985
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100986/* append the remote endpoints from the parsed TLV array to the ns2_sns_state */
987static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +0200988{
989 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200990
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100991 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
992 const struct gprs_ns_ie_ip4_elem *v4_list;
993 unsigned int num_v4;
994 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
995 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Alexander Couzens6a161492020-07-12 13:45:50 +0200996
Alexander Couzens71128672021-06-05 18:44:01 +0200997 if (num_v4 && gss->remote.ip6)
Harald Weltec1c7e4a2021-03-02 20:47:29 +0100998 return -NS_CAUSE_INVAL_NR_IPv4_EP;
Alexander Couzens6a161492020-07-12 13:45:50 +0200999
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001000 /* realloc to the new size */
Alexander Couzens71128672021-06-05 18:44:01 +02001001 gss->remote.ip4 = talloc_realloc(gss, gss->remote.ip4,
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001002 struct gprs_ns_ie_ip4_elem,
Alexander Couzens71128672021-06-05 18:44:01 +02001003 gss->remote.num_ip4 + num_v4);
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001004 /* append the new entries to the end of the list */
Alexander Couzens71128672021-06-05 18:44:01 +02001005 memcpy(&gss->remote.ip4[gss->remote.num_ip4], v4_list, num_v4*sizeof(*v4_list));
1006 gss->remote.num_ip4 += num_v4;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001007
1008 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n",
Alexander Couzens71128672021-06-05 18:44:01 +02001009 gss->remote.num_ip4);
Alexander Couzens6a161492020-07-12 13:45:50 +02001010 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001011
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001012 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1013 const struct gprs_ns_ie_ip6_elem *v6_list;
1014 unsigned int num_v6;
1015 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1016 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
1017
Alexander Couzens71128672021-06-05 18:44:01 +02001018 if (num_v6 && gss->remote.ip4)
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001019 return -NS_CAUSE_INVAL_NR_IPv6_EP;
1020
1021 /* realloc to the new size */
Alexander Couzens71128672021-06-05 18:44:01 +02001022 gss->remote.ip6 = talloc_realloc(gss, gss->remote.ip6,
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001023 struct gprs_ns_ie_ip6_elem,
Alexander Couzens71128672021-06-05 18:44:01 +02001024 gss->remote.num_ip6 + num_v6);
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001025 /* append the new entries to the end of the list */
Alexander Couzens71128672021-06-05 18:44:01 +02001026 memcpy(&gss->remote.ip6[gss->remote.num_ip6], v6_list, num_v6*sizeof(*v6_list));
1027 gss->remote.num_ip6 += num_v6;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001028
Alexander Couzens71128672021-06-05 18:44:01 +02001029 LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %d entries\n",
1030 gss->remote.num_ip6);
Alexander Couzens6a161492020-07-12 13:45:50 +02001031 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001032
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001033 return 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001034}
1035
Harald Welte694dad52021-03-23 15:22:16 +01001036static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
Alexander Couzens790a9632021-02-05 17:18:39 +01001037{
1038 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1039
Harald Weltef61a9152021-03-02 22:20:17 +01001040 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1041
Harald Welte694dad52021-03-23 15:22:16 +01001042 if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN)
Alexander Couzens790a9632021-02-05 17:18:39 +01001043 gss->N = 0;
1044}
1045
Harald Welte694dad52021-03-23 15:22:16 +01001046static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
Alexander Couzens6a161492020-07-12 13:45:50 +02001047{
1048 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001049 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1050 uint8_t cause;
1051 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001052
Harald Weltef61a9152021-03-02 22:20:17 +01001053 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS);
1054
Alexander Couzens6a161492020-07-12 13:45:50 +02001055 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001056 case GPRS_SNS_EV_RX_CONFIG_END:
1057 case GPRS_SNS_EV_RX_CONFIG:
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001058 rc = ns_sns_append_remote_eps(fi, data);
1059 if (rc < 0) {
1060 cause = -rc;
1061 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1062 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1063 return;
Alexander Couzens6a161492020-07-12 13:45:50 +02001064 }
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001065 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
1066 /* check if sum of data / sig weights == 0 */
Alexander Couzens019da4b2021-06-06 02:48:18 +02001067 if (ip46_weight_sum_data(&gss->remote) == 0 || ip46_weight_sum_sig(&gss->remote) == 0) {
Harald Weltec1c7e4a2021-03-02 20:47:29 +01001068 cause = NS_CAUSE_INVAL_WEIGH;
1069 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
1070 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
1071 return;
1072 }
1073 create_missing_nsvcs(fi);
1074 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1075 /* start the test procedure on ALL NSVCs! */
1076 gprs_ns2_start_alive_all_nsvcs(nse);
1077 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0);
1078 } else {
1079 /* just send CONFIG-ACK */
1080 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
1081 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001082 }
1083 break;
1084 default:
1085 OSMO_ASSERT(0);
1086 }
1087}
1088
Alexander Couzens67725e22021-02-15 02:37:03 +01001089/* called when receiving GPRS_SNS_EV_RX_ADD in state configure */
Alexander Couzens6a161492020-07-12 13:45:50 +02001090static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi,
1091 struct ns2_sns_state *gss,
1092 struct tlv_parsed *tp)
1093{
1094 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1095 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1096 int num_v4 = 0, num_v6 = 0;
1097 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001098 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001099 int rc = 0;
1100
1101 /* TODO: refactor EV_ADD/CHANGE/REMOVE by
1102 * check uniqueness within the lists (no doublicate entries)
1103 * check not-known-by-us and sent back a list of unknown/known values
1104 * (abnormal behaviour according to 48.016)
1105 */
1106
1107 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
Alexander Couzens077ce5a2021-06-06 01:32:45 +02001108 if (gss->family == AF_INET) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001109 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1110 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1111 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1112 return;
1113 }
1114
1115 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1116 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001117 for (i = 0; i < num_v4; i++) {
1118 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001119 rc = do_sns_add(fi, &v4_list[i], NULL);
1120 if (rc < 0) {
1121 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001122 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001123 do_sns_delete(fi, &v4_list[j], NULL);
1124 cause = -rc;
1125 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1126 break;
1127 }
1128 }
1129 } else { /* IPv6 */
1130 if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1131 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1132 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1133 return;
1134 }
1135
1136 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1137 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001138 for (i = 0; i < num_v6; i++) {
1139 unsigned int j;
Alexander Couzens6a161492020-07-12 13:45:50 +02001140 rc = do_sns_add(fi, NULL, &v6_list[i]);
1141 if (rc < 0) {
1142 /* rollback/undo to restore previous state */
Harald Welte7da6ace2020-09-18 09:48:05 +02001143 for (j = 0; j < i; j++)
Alexander Couzens6a161492020-07-12 13:45:50 +02001144 do_sns_delete(fi, NULL, &v6_list[j]);
1145 cause = -rc;
1146 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1147 break;
1148 }
1149 }
1150 }
1151
1152 /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */
1153 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1154}
1155
1156static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi,
1157 struct ns2_sns_state *gss,
1158 struct tlv_parsed *tp)
1159{
1160 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1161 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1162 int num_v4 = 0, num_v6 = 0;
1163 uint8_t trans_id, cause = 0xff;
Harald Welte7da6ace2020-09-18 09:48:05 +02001164 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001165 int rc = 0;
1166
1167 /* TODO: split up delete into v4 + v6
1168 * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa
1169 * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time
1170 */
1171 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
Alexander Couzens077ce5a2021-06-06 01:32:45 +02001172 if (gss->family == AF_INET) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001173 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1174 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1175 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001176 for ( i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001177 rc = do_sns_delete(fi, &v4_list[i], NULL);
1178 if (rc < 0) {
1179 cause = -rc;
1180 /* continue to delete others */
1181 }
1182 }
1183 if (cause != 0xff) {
1184 /* TODO: create list of not-deleted and return it */
1185 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1186 return;
1187 }
1188
1189 } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) {
1190 /* delete all NS-VCs for given IPv4 address */
1191 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1192 struct gprs_ns_ie_ip4_elem *ip4_remote;
1193 uint32_t ip_addr = *(uint32_t *)(ie+1);
1194 if (ie[0] != 0x01) { /* Address Type != IPv4 */
1195 cause = NS_CAUSE_UNKN_IP_ADDR;
1196 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1197 return;
1198 }
1199 /* make a copy as do_sns_delete() will change the array underneath us */
Alexander Couzens71128672021-06-05 18:44:01 +02001200 ip4_remote = talloc_memdup(fi, gss->remote.ip4,
1201 gss->remote.num_ip4 * sizeof(*v4_list));
1202 for (i = 0; i < gss->remote.num_ip4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001203 if (ip4_remote[i].ip_addr == ip_addr) {
1204 rc = do_sns_delete(fi, &ip4_remote[i], NULL);
1205 if (rc < 0) {
1206 cause = -rc;
1207 /* continue to delete others */
1208 }
1209 }
1210 }
1211 talloc_free(ip4_remote);
1212 if (cause != 0xff) {
1213 /* TODO: create list of not-deleted and return it */
1214 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1215 return;
1216 }
1217 } else {
1218 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1219 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1220 return;
1221 }
1222 } else { /* IPv6 */
1223 if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1224 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1225 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001226 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001227 rc = do_sns_delete(fi, NULL, &v6_list[i]);
1228 if (rc < 0) {
1229 cause = -rc;
1230 /* continue to delete others */
1231 }
1232 }
1233 if (cause != 0xff) {
1234 /* TODO: create list of not-deleted and return it */
1235 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1236 return;
1237 }
1238 } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) {
1239 /* delete all NS-VCs for given IPv4 address */
1240 const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR);
1241 struct gprs_ns_ie_ip6_elem *ip6_remote;
1242 struct in6_addr ip6_addr;
Harald Welte7da6ace2020-09-18 09:48:05 +02001243 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001244 if (ie[0] != 0x02) { /* Address Type != IPv6 */
1245 cause = NS_CAUSE_UNKN_IP_ADDR;
1246 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1247 return;
1248 }
1249 memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr));
1250 /* make a copy as do_sns_delete() will change the array underneath us */
Alexander Couzens71128672021-06-05 18:44:01 +02001251 ip6_remote = talloc_memdup(fi, gss->remote.ip6,
1252 gss->remote.num_ip6 * sizeof(*v4_list));
1253 for (i = 0; i < gss->remote.num_ip6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001254 if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) {
1255 rc = do_sns_delete(fi, NULL, &ip6_remote[i]);
1256 if (rc < 0) {
1257 cause = -rc;
1258 /* continue to delete others */
1259 }
1260 }
1261 }
1262
1263 talloc_free(ip6_remote);
1264 if (cause != 0xff) {
1265 /* TODO: create list of not-deleted and return it */
1266 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1267 return;
1268 }
1269 } else {
1270 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
1271 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1272 return;
1273 }
1274 }
1275 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1276}
1277
1278static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi,
1279 struct ns2_sns_state *gss,
1280 struct tlv_parsed *tp)
1281{
1282 const struct gprs_ns_ie_ip4_elem *v4_list = NULL;
1283 const struct gprs_ns_ie_ip6_elem *v6_list = NULL;
1284 int num_v4 = 0, num_v6 = 0;
1285 uint8_t trans_id, cause = 0xff;
1286 int rc = 0;
Harald Welte7da6ace2020-09-18 09:48:05 +02001287 unsigned int i;
Alexander Couzens6a161492020-07-12 13:45:50 +02001288
1289 trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID);
1290 if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) {
1291 v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST);
1292 num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001293 for (i = 0; i < num_v4; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001294 rc = do_sns_change_weight(fi, &v4_list[i], NULL);
1295 if (rc < 0) {
1296 cause = -rc;
1297 /* continue to others */
1298 }
1299 }
1300 if (cause != 0xff) {
1301 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1302 return;
1303 }
1304 } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
1305 v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST);
1306 num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list);
Harald Welte7da6ace2020-09-18 09:48:05 +02001307 for (i = 0; i < num_v6; i++) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001308 rc = do_sns_change_weight(fi, NULL, &v6_list[i]);
1309 if (rc < 0) {
1310 cause = -rc;
1311 /* continue to others */
1312 }
1313 }
1314 if (cause != 0xff) {
1315 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1316 return;
1317 }
1318 } else {
1319 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
1320 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0);
1321 return;
1322 }
1323 ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6);
1324}
1325
1326static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1327{
1328 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1329 struct tlv_parsed *tp = data;
1330
1331 switch (event) {
Alexander Couzens67725e22021-02-15 02:37:03 +01001332 case GPRS_SNS_EV_RX_ADD:
Alexander Couzens6a161492020-07-12 13:45:50 +02001333 ns2_sns_st_configured_add(fi, gss, tp);
1334 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001335 case GPRS_SNS_EV_RX_DELETE:
Alexander Couzens6a161492020-07-12 13:45:50 +02001336 ns2_sns_st_configured_delete(fi, gss, tp);
1337 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001338 case GPRS_SNS_EV_RX_CHANGE_WEIGHT:
Alexander Couzens6a161492020-07-12 13:45:50 +02001339 ns2_sns_st_configured_change(fi, gss, tp);
1340 break;
Alexander Couzens67725e22021-02-15 02:37:03 +01001341 case GPRS_SNS_EV_REQ_NSVC_ALIVE:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001342 osmo_timer_del(&fi->timer);
1343 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001344 }
1345}
1346
1347static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
1348{
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001349 struct gprs_ns2_vc *nsvc;
1350 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001351 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzenscdb2baa2021-04-01 15:29:16 +02001352 /* NS-VC status updates are only parsed in ST_CONFIGURED.
1353 * Do an initial check if there are any nsvc alive atm */
1354 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1355 if (ns2_vc_is_unblocked(nsvc)) {
1356 gss->alive = true;
1357 osmo_timer_del(&fi->timer);
1358 break;
1359 }
1360 }
1361
Alexander Couzens53e70092021-04-06 15:45:47 +02001362 /* remove the initial NSVC if the NSVC isn't part of the configuration */
1363 if (gss->sns_nsvc->sns_only)
1364 gprs_ns2_free_nsvc(gss->sns_nsvc);
1365
Alexander Couzens138b96f2021-01-25 16:23:29 +01001366 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED);
Alexander Couzens6a161492020-07-12 13:45:50 +02001367}
1368
1369static const struct osmo_fsm_state ns2_sns_bss_states[] = {
1370 [GPRS_SNS_ST_UNCONFIGURED] = {
Alexander Couzense769f522020-12-07 07:37:07 +01001371 .in_event_mask = 0, /* handled by all_state_action */
Alexander Couzens0a7c5ee2021-04-10 18:20:21 +02001372 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
1373 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001374 .name = "UNCONFIGURED",
Harald Welte694dad52021-03-23 15:22:16 +01001375 .action = ns2_sns_st_bss_unconfigured,
Alexander Couzens6a161492020-07-12 13:45:50 +02001376 },
Harald Welte694dad52021-03-23 15:22:16 +01001377 [GPRS_SNS_ST_BSS_SIZE] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001378 .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK),
Alexander Couzens6a161492020-07-12 13:45:50 +02001379 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001380 S(GPRS_SNS_ST_BSS_SIZE) |
1381 S(GPRS_SNS_ST_BSS_CONFIG_BSS),
1382 .name = "BSS_SIZE",
1383 .action = ns2_sns_st_bss_size,
1384 .onenter = ns2_sns_st_bss_size_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001385 },
Harald Welte694dad52021-03-23 15:22:16 +01001386 [GPRS_SNS_ST_BSS_CONFIG_BSS] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001387 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
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_BSS) |
1390 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
1391 S(GPRS_SNS_ST_BSS_SIZE),
1392 .name = "BSS_CONFIG_BSS",
1393 .action = ns2_sns_st_bss_config_bss,
1394 .onenter = ns2_sns_st_bss_config_bss_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001395 },
Harald Welte694dad52021-03-23 15:22:16 +01001396 [GPRS_SNS_ST_BSS_CONFIG_SGSN] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001397 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
1398 S(GPRS_SNS_EV_RX_CONFIG_END),
Alexander Couzens6a161492020-07-12 13:45:50 +02001399 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001400 S(GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens6a161492020-07-12 13:45:50 +02001401 S(GPRS_SNS_ST_CONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001402 S(GPRS_SNS_ST_BSS_SIZE),
1403 .name = "BSS_CONFIG_SGSN",
1404 .action = ns2_sns_st_bss_config_sgsn,
1405 .onenter = ns2_sns_st_bss_config_sgsn_onenter,
Alexander Couzens6a161492020-07-12 13:45:50 +02001406 },
1407 [GPRS_SNS_ST_CONFIGURED] = {
Alexander Couzens67725e22021-02-15 02:37:03 +01001408 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
1409 S(GPRS_SNS_EV_RX_DELETE) |
1410 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
1411 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
Alexander Couzense03d8632020-12-06 03:31:44 +01001412 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
Harald Welte694dad52021-03-23 15:22:16 +01001413 S(GPRS_SNS_ST_BSS_SIZE),
Alexander Couzens6a161492020-07-12 13:45:50 +02001414 .name = "CONFIGURED",
1415 .action = ns2_sns_st_configured,
1416 .onenter = ns2_sns_st_configured_onenter,
1417 },
1418};
1419
1420static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi)
1421{
Alexander Couzens90ee9632020-12-07 06:18:32 +01001422 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
Alexander Couzens6a161492020-07-12 13:45:50 +02001423 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1424 struct gprs_ns2_inst *nsi = nse->nsi;
1425
Alexander Couzens90ee9632020-12-07 06:18:32 +01001426 gss->N++;
Alexander Couzens6a161492020-07-12 13:45:50 +02001427 switch (fi->T) {
1428 case 1:
Alexander Couzensa367d082020-12-21 14:06:24 +01001429 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) {
1430 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei);
Alexander Couzens67725e22021-02-15 02:37:03 +01001431 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001432 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001433 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 +01001434 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001435 break;
1436 case 2:
Alexander Couzensa367d082020-12-21 14:06:24 +01001437 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
Alexander Couzens3df58862021-02-05 17:18:08 +01001438 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 +01001439 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensa367d082020-12-21 14:06:24 +01001440 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001441 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 +01001442 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001443 break;
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001444 case 3:
Alexander Couzens3df58862021-02-05 17:18:08 +01001445 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
1446 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 +01001447 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens3df58862021-02-05 17:18:08 +01001448 } else {
Harald Welte694dad52021-03-23 15:22:16 +01001449 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 +01001450 }
1451 break;
1452 case 4:
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001453 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 +01001454 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001455 break;
Alexander Couzens6a161492020-07-12 13:45:50 +02001456 }
1457 return 0;
1458}
1459
Harald Welte9e37bf42021-03-02 20:48:31 +01001460/* common allstate-action for both roles */
Alexander Couzens6a161492020-07-12 13:45:50 +02001461static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1462{
1463 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001464 struct ns2_sns_bind *sbind;
1465 struct gprs_ns2_vc *nsvc, *nsvc2;
Alexander Couzens6a161492020-07-12 13:45:50 +02001466
Alexander Couzense769f522020-12-07 07:37:07 +01001467 switch (event) {
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001468 case GPRS_SNS_EV_REQ_ADD_BIND:
1469 sbind = data;
1470 switch (fi->state) {
1471 case GPRS_SNS_ST_UNCONFIGURED:
Alexander Couzens67725e22021-02-15 02:37:03 +01001472 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001473 break;
Harald Welte694dad52021-03-23 15:22:16 +01001474 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001475 /* TODO: add the ip4 element to the list */
1476 break;
Harald Welte694dad52021-03-23 15:22:16 +01001477 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1478 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001479 case GPRS_SNS_ST_CONFIGURED:
1480 /* TODO: add to SNS-IP procedure queue & add nsvc() */
1481 break;
1482 }
1483 break;
1484 case GPRS_SNS_EV_REQ_DELETE_BIND:
1485 sbind = data;
1486 switch (fi->state) {
1487 case GPRS_SNS_ST_UNCONFIGURED:
1488 break;
Harald Welte694dad52021-03-23 15:22:16 +01001489 case GPRS_SNS_ST_BSS_SIZE:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001490 /* TODO: remove the ip4 element from 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;
Harald Welte694dad52021-03-23 15:22:16 +01001497 case GPRS_SNS_ST_BSS_CONFIG_BSS:
1498 case GPRS_SNS_ST_BSS_CONFIG_SGSN:
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001499 case GPRS_SNS_ST_CONFIGURED:
1500 /* TODO: do an delete SNS-IP procedure */
1501 /* TODO: remove the ip4 element to the list */
1502 llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) {
1503 if (nsvc->bind == sbind->bind) {
1504 gprs_ns2_free_nsvc(nsvc);
1505 }
1506 }
1507 break;
1508 }
1509 /* if this is the last bind, the free_nsvc() will trigger a reselection */
1510 talloc_free(sbind);
1511 break;
Alexander Couzense769f522020-12-07 07:37:07 +01001512 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001513}
1514
Alexander Couzens31d52e12021-06-05 20:04:04 +02001515/* validate the bss configuration (sns endpoint and binds)
1516 * - no endpoints -> invalid
1517 * - no binds -> invalid
1518 * - only v4 sns endpoints, only v6 binds -> invalid
1519 * - only v4 sns endpoints, but v4 sig weights == 0 -> invalid ...
1520 */
1521static int ns2_sns_bss_valid_configuration(struct ns2_sns_state *gss)
1522{
1523 struct ns2_sns_bind *sbind;
1524 struct sns_endpoint *endpoint;
1525 const struct osmo_sockaddr *addr;
1526 int v4_sig = 0, v4_data = 0, v6_sig = 0, v6_data = 0;
1527 bool v4_endpoints = false;
1528 bool v6_endpoints = false;
1529
1530 if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds))
1531 return 0;
1532
1533 llist_for_each_entry(sbind, &gss->binds, list) {
1534 addr = gprs_ns2_ip_bind_sockaddr(sbind->bind);
1535 if (!addr)
1536 continue;
1537 switch (addr->u.sa.sa_family) {
1538 case AF_INET:
1539 v4_sig += sbind->bind->sns_sig_weight;
1540 v4_data += sbind->bind->sns_data_weight;
1541 break;
1542 case AF_INET6:
1543 v6_sig += sbind->bind->sns_sig_weight;
1544 v6_data += sbind->bind->sns_data_weight;
1545 break;
1546 }
1547 }
1548
1549 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
1550 switch (endpoint->saddr.u.sa.sa_family) {
1551 case AF_INET:
1552 v4_endpoints = true;
1553 break;
1554 case AF_INET6:
1555 v6_endpoints = true;
1556 break;
1557 }
1558 }
1559
1560 return (v4_endpoints && v4_sig && v4_data) || (v6_endpoints && v6_sig && v6_data);
1561}
1562
Harald Welte9e37bf42021-03-02 20:48:31 +01001563/* allstate-action for BSS role */
1564static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1565{
1566 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
1567 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
1568
1569 /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */
1570 switch (event) {
1571 case GPRS_SNS_EV_REQ_NO_NSVC:
1572 /* ignore reselection running */
1573 if (gss->reselection_running)
1574 break;
1575
1576 LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei);
1577 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
1578 break;
1579 case GPRS_SNS_EV_REQ_SELECT_ENDPOINT:
1580 /* tear down previous state
1581 * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */
1582 gss->reselection_running = true;
1583 gprs_ns2_free_nsvcs(nse);
Alexander Couzensd2c6c492021-06-06 01:57:52 +02001584 ns2_clear_elems(&gss->local);
1585 ns2_clear_elems(&gss->remote);
Harald Welte9e37bf42021-03-02 20:48:31 +01001586
1587 /* Choose the next sns endpoint. */
Alexander Couzens31d52e12021-06-05 20:04:04 +02001588 if (!ns2_sns_bss_valid_configuration(gss)) {
Harald Welte9e37bf42021-03-02 20:48:31 +01001589 gss->initial = NULL;
1590 ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS);
1591 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3);
1592 return;
1593 } else if (!gss->initial) {
1594 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1595 } else if (gss->initial->list.next == &gss->sns_endpoints) {
1596 /* last entry, continue with first */
1597 gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list);
1598 } else {
1599 /* next element is an entry */
1600 gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list);
1601 }
1602
Alexander Couzens68ab9c42021-06-06 03:03:40 +02001603 gss->family = gss->initial->saddr.u.sa.sa_family;
Harald Welte9e37bf42021-03-02 20:48:31 +01001604 gss->reselection_running = false;
Harald Welte694dad52021-03-23 15:22:16 +01001605 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 +01001606 break;
1607 default:
1608 ns2_sns_st_all_action(fi, event, data);
1609 break;
1610 }
1611}
1612
Alexander Couzens6a161492020-07-12 13:45:50 +02001613static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1614 .name = "GPRS-NS2-SNS-BSS",
1615 .states = ns2_sns_bss_states,
1616 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001617 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1618 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001619 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1620 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001621 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001622 .cleanup = NULL,
1623 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001624 .event_names = gprs_sns_event_names,
1625 .pre_term = NULL,
1626 .log_subsys = DLNS,
1627};
1628
Harald Welte5bef2cc2020-09-18 22:33:24 +02001629/*! Allocate an IP-SNS FSM for the BSS side.
1630 * \param[in] nse NS Entity in which the FSM runs
1631 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001632 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001633struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1634 const char *id)
1635{
1636 struct osmo_fsm_inst *fi;
1637 struct ns2_sns_state *gss;
1638
1639 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1640 if (!fi)
1641 return fi;
1642
1643 gss = talloc_zero(fi, struct ns2_sns_state);
1644 if (!gss)
1645 goto err;
1646
1647 fi->priv = gss;
1648 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001649 gss->role = GPRS_SNS_ROLE_BSS;
Harald Welte24f4df52021-03-04 18:02:54 +01001650 /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */
1651 gss->num_max_ip4_remote = 8192;
1652 gss->num_max_ip6_remote = 8192;
Alexander Couzense769f522020-12-07 07:37:07 +01001653 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001654 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001655
1656 return fi;
1657err:
1658 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1659 return NULL;
1660}
1661
Harald Welte5bef2cc2020-09-18 22:33:24 +02001662/*! main entry point for receiving SNS messages from the network.
1663 * \param[in] nsvc NS-VC on which the message was received
1664 * \param[in] msg message buffer of the IP-SNS message
1665 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001666 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001667int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001668{
1669 struct gprs_ns2_nse *nse = nsvc->nse;
1670 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1671 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001672 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001673 struct osmo_fsm_inst *fi;
Alexander Couzens7619ed42021-03-24 17:44:03 +01001674 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001675
1676 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001677 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1678 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001679 rc = -EINVAL;
1680 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +02001681 }
1682
Alexander Couzens6a161492020-07-12 13:45:50 +02001683 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1684 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001685 gss = (struct ns2_sns_state *) fi->priv;
1686 if (!gss->sns_nsvc)
1687 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001688
Harald Weltef2949742021-01-20 14:54:14 +01001689 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1690 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1691
Alexander Couzens6a161492020-07-12 13:45:50 +02001692 switch (nsh->pdu_type) {
1693 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001694 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001695 break;
1696 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001697 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001698 break;
1699 case SNS_PDUT_CONFIG:
1700 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001701 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001702 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001703 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001704 break;
1705 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001706 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001707 break;
1708 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001709 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001710 break;
1711 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001712 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001713 break;
1714 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001715 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001716 break;
1717 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001718 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001719 break;
1720 default:
Harald Weltef2949742021-01-20 14:54:14 +01001721 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1722 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001723 rc = -EINVAL;
Alexander Couzens6a161492020-07-12 13:45:50 +02001724 }
1725
Alexander Couzens7619ed42021-03-24 17:44:03 +01001726out:
1727 msgb_free(msg);
1728
1729 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001730}
1731
1732#include <osmocom/vty/vty.h>
1733#include <osmocom/vty/misc.h>
1734
Harald Welte1262c4f2021-01-19 20:58:33 +01001735static 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 +02001736{
1737 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001738 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001739 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1740}
1741
Harald Welte1262c4f2021-01-19 20:58:33 +01001742static 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 +02001743{
1744 char ip_addr[INET6_ADDRSTRLEN] = {};
1745 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1746 strcpy(ip_addr, "Invalid IPv6");
1747
Harald Welte1262c4f2021-01-19 20:58:33 +01001748 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001749 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1750}
1751
Harald Welte5bef2cc2020-09-18 22:33:24 +02001752/*! Dump the IP-SNS state to a vty.
1753 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001754 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001755 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1756 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001757void 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 +02001758{
1759 struct ns2_sns_state *gss;
1760 unsigned int i;
1761
1762 if (!nse->bss_sns_fi)
1763 return;
1764
Harald Welte1262c4f2021-01-19 20:58:33 +01001765 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001766 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1767
Harald Welte1262c4f2021-01-19 20:58:33 +01001768 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1769 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001770
Alexander Couzens71128672021-06-05 18:44:01 +02001771 if (gss->local.num_ip4 && gss->remote.num_ip4) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001772 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001773 for (i = 0; i < gss->local.num_ip4; i++)
1774 vty_dump_sns_ip4(vty, prefix, &gss->local.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001775
Harald Welte1262c4f2021-01-19 20:58:33 +01001776 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001777 for (i = 0; i < gss->remote.num_ip4; i++)
1778 vty_dump_sns_ip4(vty, prefix, &gss->remote.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001779 }
1780
Alexander Couzens71128672021-06-05 18:44:01 +02001781 if (gss->local.num_ip6 && gss->remote.num_ip6) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001782 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001783 for (i = 0; i < gss->local.num_ip6; i++)
1784 vty_dump_sns_ip6(vty, prefix, &gss->local.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001785
Harald Welte1262c4f2021-01-19 20:58:33 +01001786 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001787 for (i = 0; i < gss->remote.num_ip6; i++)
1788 vty_dump_sns_ip6(vty, prefix, &gss->remote.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001789 }
1790}
1791
Alexander Couzens412bc342020-11-19 05:24:37 +01001792/*! write IP-SNS to a vty
1793 * \param[in] vty VTY to which the state shall be printed
1794 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001795void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001796{
1797 struct ns2_sns_state *gss;
1798 struct osmo_sockaddr_str addr_str;
1799 struct sns_endpoint *endpoint;
1800
1801 if (!nse->bss_sns_fi)
1802 return;
1803
1804 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1805 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001806 /* It's unlikely that an error happens, but let's better be safe. */
1807 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1808 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001809 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001810 }
1811}
1812
Alexander Couzense769f522020-12-07 07:37:07 +01001813static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1814 const struct osmo_sockaddr *saddr)
1815{
1816 struct sns_endpoint *endpoint;
1817
1818 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1819 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1820 return endpoint;
1821 }
1822
1823 return NULL;
1824}
1825
1826/*! gprs_ns2_sns_add_endpoint
1827 * \param[in] nse
1828 * \param[in] sockaddr
1829 * \return
1830 */
1831int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1832 const struct osmo_sockaddr *saddr)
1833{
1834 struct ns2_sns_state *gss;
1835 struct sns_endpoint *endpoint;
1836 bool do_selection = false;
1837
1838 if (nse->ll != GPRS_NS2_LL_UDP) {
1839 return -EINVAL;
1840 }
1841
Alexander Couzens138b96f2021-01-25 16:23:29 +01001842 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001843 return -EINVAL;
1844 }
1845
1846 gss = nse->bss_sns_fi->priv;
1847
1848 if (ns2_get_sns_endpoint(gss, saddr))
1849 return -EADDRINUSE;
1850
1851 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1852 if (!endpoint)
1853 return -ENOMEM;
1854
1855 endpoint->saddr = *saddr;
1856 if (llist_empty(&gss->sns_endpoints))
1857 do_selection = true;
1858
1859 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1860 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001861 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001862
1863 return 0;
1864}
1865
1866/*! gprs_ns2_sns_del_endpoint
1867 * \param[in] nse
1868 * \param[in] sockaddr
1869 * \return 0 on success, otherwise < 0
1870 */
1871int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1872 const struct osmo_sockaddr *saddr)
1873{
1874 struct ns2_sns_state *gss;
1875 struct sns_endpoint *endpoint;
1876
1877 if (nse->ll != GPRS_NS2_LL_UDP) {
1878 return -EINVAL;
1879 }
1880
Alexander Couzens138b96f2021-01-25 16:23:29 +01001881 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001882 return -EINVAL;
1883 }
1884
1885 gss = nse->bss_sns_fi->priv;
1886 endpoint = ns2_get_sns_endpoint(gss, saddr);
1887 if (!endpoint)
1888 return -ENOENT;
1889
1890 /* if this is an unused SNS endpoint it's done */
1891 if (gss->initial != endpoint) {
1892 llist_del(&endpoint->list);
1893 talloc_free(endpoint);
1894 return 0;
1895 }
1896
Alexander Couzens67725e22021-02-15 02:37:03 +01001897 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001898 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001899 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001900 "Closing all NS-VC and restart SNS-SIZE procedure"
1901 "with a remaining SNS endpoint.\n");
1902
1903 /* Continue with the next endpoint in the list.
1904 * Special case if the endpoint is at the start or end of the list */
1905 if (endpoint->list.prev == &gss->sns_endpoints ||
1906 endpoint->list.next == &gss->sns_endpoints)
1907 gss->initial = NULL;
1908 else
1909 gss->initial = llist_entry(endpoint->list.next->prev,
1910 struct sns_endpoint,
1911 list);
1912
1913 llist_del(&endpoint->list);
1914 gprs_ns2_free_nsvcs(nse);
1915 talloc_free(endpoint);
1916
1917 return 0;
1918}
1919
1920/*! gprs_ns2_sns_count
1921 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1922 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1923 */
1924int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1925{
1926 struct ns2_sns_state *gss;
1927 struct sns_endpoint *endpoint;
1928 int count = 0;
1929
1930 if (nse->ll != GPRS_NS2_LL_UDP) {
1931 return -EINVAL;
1932 }
1933
Alexander Couzens138b96f2021-01-25 16:23:29 +01001934 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001935 return -EINVAL;
1936 }
1937
1938 gss = nse->bss_sns_fi->priv;
1939 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1940 count++;
1941
1942 return count;
1943}
1944
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001945void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1946{
1947 struct ns2_sns_state *gss;
1948 struct gprs_ns2_vc *tmp;
1949
1950 if (!nse->bss_sns_fi)
1951 return;
1952
1953 gss = nse->bss_sns_fi->priv;
1954 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1955 return;
1956
1957 if (alive == gss->alive)
1958 return;
1959
1960 /* check if this is the current SNS NS-VC */
1961 if (nsvc == gss->sns_nsvc) {
1962 /* only replace the SNS NS-VC if there are other alive NS-VC.
1963 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1964 * and couldn't confirm yet if the NS-VC comes up */
1965 if (gss->alive && !alive)
1966 ns2_sns_replace_nsvc(nsvc);
1967 }
1968
1969 if (alive) {
1970 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001971 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001972 } else {
1973 /* is there at least another alive nsvc? */
1974 llist_for_each_entry(tmp, &nse->nsvc, list) {
1975 if (ns2_vc_is_unblocked(tmp))
1976 return;
1977 }
1978
1979 /* all NS-VC have failed */
1980 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001981 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001982 }
1983}
1984
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001985int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1986 struct gprs_ns2_vc_bind *bind)
1987{
1988 struct ns2_sns_state *gss;
1989 struct ns2_sns_bind *tmp;
1990
1991 OSMO_ASSERT(nse->bss_sns_fi);
1992 gss = nse->bss_sns_fi->priv;
1993
1994 if (!gprs_ns2_is_ip_bind(bind)) {
1995 return -EINVAL;
1996 }
1997
1998 if (!llist_empty(&gss->binds)) {
1999 llist_for_each_entry(tmp, &gss->binds, list) {
2000 if (tmp->bind == bind)
2001 return -EALREADY;
2002 }
2003 }
2004
2005 tmp = talloc_zero(gss, struct ns2_sns_bind);
2006 if (!tmp)
2007 return -ENOMEM;
2008 tmp->bind = bind;
2009 llist_add_tail(&tmp->list, &gss->binds);
2010
2011 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
2012 return 0;
2013}
2014
2015/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
2016int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
2017 struct gprs_ns2_vc_bind *bind)
2018{
2019 struct ns2_sns_state *gss;
2020 struct ns2_sns_bind *tmp, *tmp2;
2021 bool found = false;
2022
2023 if (!nse->bss_sns_fi)
2024 return -EINVAL;
2025
2026 gss = nse->bss_sns_fi->priv;
2027 if (gss->initial_bind && gss->initial_bind->bind == bind) {
2028 if (gss->initial_bind->list.prev == &gss->binds)
2029 gss->initial_bind = NULL;
2030 else
2031 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
2032 }
2033
2034 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
2035 if (tmp->bind == bind) {
2036 llist_del(&tmp->list);
2037 found = true;
Alexander Couzensa35c2962021-04-19 03:30:15 +02002038 break;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002039 }
2040 }
2041
2042 if (!found)
2043 return -ENOENT;
2044
2045 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
2046 return 0;
2047}
2048
Alexander Couzens71128672021-06-05 18:44:01 +02002049/* Update SNS weights for a bind (local endpoint).
2050 * \param[in] bind the bind which has been updated
Alexander Couzensc4704762021-02-08 23:13:12 +01002051 */
2052void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
2053{
2054 /* TODO: implement weights after binds per sns implemented */
2055}
2056
Harald Welte4f127462021-03-02 20:49:10 +01002057
2058
2059
2060/***********************************************************************
2061 * SGSN role
2062 ***********************************************************************/
2063
2064static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2065{
2066 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2067 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2068 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
2069}
2070
2071/* We're waiting for inbound SNS-CONFIG from the BSS */
2072static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2073{
2074 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2075 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2076 struct gprs_ns2_inst *nsi = nse->nsi;
2077 uint8_t cause;
2078 int rc;
2079
2080 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2081
2082 switch (event) {
2083 case GPRS_SNS_EV_RX_CONFIG:
2084 case GPRS_SNS_EV_RX_CONFIG_END:
2085 rc = ns_sns_append_remote_eps(fi, data);
2086 if (rc < 0) {
2087 cause = -rc;
2088 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2089 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2090 return;
2091 }
2092 /* only change state if last CONFIG was received */
2093 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
2094 /* ensure sum of data weight / sig weights is > 0 */
Alexander Couzens019da4b2021-06-06 02:48:18 +02002095 if (ip46_weight_sum_data(&gss->remote) == 0 || ip46_weight_sum_sig(&gss->remote) == 0) {
Harald Welte4f127462021-03-02 20:49:10 +01002096 cause = NS_CAUSE_INVAL_WEIGH;
2097 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2098 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2099 break;
2100 }
2101 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2102 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2103 } else {
2104 /* just send CONFIG-ACK */
2105 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2106 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2107 }
2108 break;
2109 }
2110}
2111
2112static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2113{
2114 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2115 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2116
Harald Welte4f127462021-03-02 20:49:10 +01002117 /* transmit SGSN-oriented SNS-CONFIG */
Alexander Couzens71128672021-06-05 18:44:01 +02002118 ns2_tx_sns_config(gss->sns_nsvc, true, gss->local.ip4, gss->local.num_ip4,
2119 gss->local.ip6, gss->local.num_ip6);
Harald Welte4f127462021-03-02 20:49:10 +01002120}
2121
2122/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2123static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2124{
2125 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2126 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2127 struct tlv_parsed *tp = NULL;
2128
2129 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2130
2131 switch (event) {
2132 case GPRS_SNS_EV_RX_CONFIG_ACK:
2133 tp = data;
2134 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2135 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2136 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2137 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2138 break;
2139 }
2140 /* we currently only send one SNS-CONFIG with END FLAG */
2141 if (true) {
2142 create_missing_nsvcs(fi);
2143 /* start the test procedure on ALL NSVCs! */
2144 gprs_ns2_start_alive_all_nsvcs(nse);
2145 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2146 }
2147 break;
2148 }
2149}
2150
2151/* SGSN-side SNS state machine */
2152static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2153 [GPRS_SNS_ST_UNCONFIGURED] = {
2154 .in_event_mask = 0, /* handled by all_state_action */
2155 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2156 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2157 .name = "UNCONFIGURED",
2158 .action = ns2_sns_st_sgsn_unconfigured,
2159 },
2160 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
2161 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
2162 S(GPRS_SNS_EV_RX_CONFIG_END),
2163 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2164 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2165 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2166 .name = "SGSN_WAIT_CONFIG",
2167 .action = ns2_sns_st_sgsn_wait_config,
2168 },
2169 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
2170 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
2171 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2172 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2173 S(GPRS_SNS_ST_CONFIGURED),
2174 .name = "SGSN_WAIT_CONFIG_ACK",
2175 .action = ns2_sns_st_sgsn_wait_config_ack,
2176 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2177 },
2178 [GPRS_SNS_ST_CONFIGURED] = {
2179 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
2180 S(GPRS_SNS_EV_RX_DELETE) |
2181 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
2182 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
2183 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2184 .name = "CONFIGURED",
2185 /* shared with BSS side; once configured there's no difference */
2186 .action = ns2_sns_st_configured,
2187 .onenter = ns2_sns_st_configured_onenter,
2188 },
2189};
2190
2191static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2192{
2193 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2194 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2195 struct gprs_ns2_inst *nsi = nse->nsi;
2196
2197 gss->N++;
2198 switch (fi->T) {
2199 case 3:
2200 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2201 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2202 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2203 } else {
2204 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2205 }
2206 break;
2207 case 4:
2208 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2209 break;
2210 }
2211 return 0;
2212}
2213
2214
2215/* allstate-action for SGSN role */
2216static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2217{
2218 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2219 struct tlv_parsed *tp = NULL;
Harald Welte01fa6a32021-03-04 19:49:38 +01002220 size_t num_local_eps, num_remote_eps;
Harald Welte4f127462021-03-02 20:49:10 +01002221 uint8_t flag;
Harald Weltea2c5af52021-03-04 17:59:35 +01002222 uint8_t cause;
Harald Welte4f127462021-03-02 20:49:10 +01002223
2224 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2225
2226 switch (event) {
2227 case GPRS_SNS_EV_RX_SIZE:
2228 tp = (struct tlv_parsed *) data;
Harald Weltea2c5af52021-03-04 17:59:35 +01002229 /* check for mandatory / conditional IEs */
2230 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) ||
2231 !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) {
2232 cause = NS_CAUSE_MISSING_ESSENT_IE;
2233 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2234 break;
2235 }
2236 if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) &&
2237 !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) {
2238 cause = NS_CAUSE_MISSING_ESSENT_IE;
Harald Welte4f127462021-03-02 20:49:10 +01002239 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2240 break;
2241 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002242 if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2))
2243 gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR);
2244 if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2))
2245 gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR);
2246 /* decide if we go for IPv4 or IPv6 */
Alexander Couzens077ce5a2021-06-06 01:32:45 +02002247 if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, AF_INET6)) {
2248 gss->family = AF_INET6;
Harald Welte2d807b62021-03-24 01:57:30 +01002249 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002250 num_local_eps = gss->local.num_ip6;
Harald Welte01fa6a32021-03-04 19:49:38 +01002251 num_remote_eps = gss->num_max_ip6_remote;
Alexander Couzens077ce5a2021-06-06 01:32:45 +02002252 } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, AF_INET)) {
2253 gss->family = AF_INET;
Harald Welte2d807b62021-03-24 01:57:30 +01002254 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002255 num_local_eps = gss->local.num_ip4;
Harald Welte01fa6a32021-03-04 19:49:38 +01002256 num_remote_eps = gss->num_max_ip4_remote;
2257 } else {
Alexander Couzens71128672021-06-05 18:44:01 +02002258 if (gss->local.num_ip4 && !gss->num_max_ip4_remote)
Harald Welte01fa6a32021-03-04 19:49:38 +01002259 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
2260 else
2261 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
2262 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2263 break;
2264 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002265 /* ensure number of NS-VCs is sufficient for full mesh */
2266 gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC);
2267 if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) {
2268 LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, "
2269 "but BSS supports only %zu maximum NS-VCs\n", num_local_eps,
2270 num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs);
2271 cause = NS_CAUSE_INVAL_NR_NS_VC;
2272 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2273 break;
2274 }
2275 /* perform state reset, if requested */
Harald Welte4f127462021-03-02 20:49:10 +01002276 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2277 if (flag & 1) {
2278 struct gprs_ns2_vc *nsvc, *nsvc2;
2279 /* clear all state */
Harald Welte46eb7642021-03-04 17:49:59 +01002280 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
Harald Welte4f127462021-03-02 20:49:10 +01002281 gss->N = 0;
Alexander Couzensd2c6c492021-06-06 01:57:52 +02002282 ns2_clear_elems(&gss->local);
2283 ns2_clear_elems(&gss->remote);
Harald Welte4f127462021-03-02 20:49:10 +01002284 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2285 if (nsvc == gss->sns_nsvc) {
2286 /* keep the NSVC we need for SNS, but unconfigure it */
2287 nsvc->sig_weight = 0;
2288 nsvc->data_weight = 0;
2289 ns2_vc_force_unconfigured(nsvc);
2290 } else {
2291 /* free all other NS-VCs */
2292 gprs_ns2_free_nsvc(nsvc);
2293 }
2294 }
Harald Welte2d807b62021-03-24 01:57:30 +01002295 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte4f127462021-03-02 20:49:10 +01002296 }
2297 /* send SIZE_ACK */
2298 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2299 /* only wait for SNS-CONFIG in case of Reset flag */
2300 if (flag & 1)
2301 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2302 break;
2303 default:
2304 ns2_sns_st_all_action(fi, event, data);
2305 break;
2306 }
2307}
2308
2309static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2310 .name = "GPRS-NS2-SNS-SGSN",
2311 .states = ns2_sns_sgsn_states,
2312 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
2313 .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) |
2314 S(GPRS_SNS_EV_REQ_NO_NSVC) |
2315 S(GPRS_SNS_EV_REQ_ADD_BIND) |
2316 S(GPRS_SNS_EV_REQ_DELETE_BIND),
2317 .allstate_action = ns2_sns_st_all_action_sgsn,
2318 .cleanup = NULL,
2319 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2320 .event_names = gprs_sns_event_names,
2321 .pre_term = NULL,
2322 .log_subsys = DLNS,
2323};
2324
2325/*! Allocate an IP-SNS FSM for the SGSN side.
2326 * \param[in] nse NS Entity in which the FSM runs
2327 * \param[in] id string identifier
2328 * \returns FSM instance on success; NULL on error */
2329struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2330{
2331 struct osmo_fsm_inst *fi;
2332 struct ns2_sns_state *gss;
2333
2334 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2335 if (!fi)
2336 return fi;
2337
2338 gss = talloc_zero(fi, struct ns2_sns_state);
2339 if (!gss)
2340 goto err;
2341
2342 fi->priv = gss;
2343 gss->nse = nse;
2344 gss->role = GPRS_SNS_ROLE_SGSN;
2345 INIT_LLIST_HEAD(&gss->sns_endpoints);
2346 INIT_LLIST_HEAD(&gss->binds);
2347
2348 return fi;
2349err:
2350 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2351 return NULL;
2352}
2353
2354
2355
2356
Alexander Couzens6a161492020-07-12 13:45:50 +02002357/* initialize osmo_ctx on main tread */
2358static __attribute__((constructor)) void on_dso_load_ctx(void)
2359{
2360 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002361 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002362}