blob: 3f090541220cfe0a77ca5841eae450f67ac4f7e4 [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
1603 gss->reselection_running = false;
Harald Welte694dad52021-03-23 15:22:16 +01001604 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 +01001605 break;
1606 default:
1607 ns2_sns_st_all_action(fi, event, data);
1608 break;
1609 }
1610}
1611
Alexander Couzens6a161492020-07-12 13:45:50 +02001612static struct osmo_fsm gprs_ns2_sns_bss_fsm = {
1613 .name = "GPRS-NS2-SNS-BSS",
1614 .states = ns2_sns_bss_states,
1615 .num_states = ARRAY_SIZE(ns2_sns_bss_states),
Alexander Couzens67725e22021-02-15 02:37:03 +01001616 .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) |
1617 S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) |
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001618 S(GPRS_SNS_EV_REQ_ADD_BIND) |
1619 S(GPRS_SNS_EV_REQ_DELETE_BIND),
Harald Welte9e37bf42021-03-02 20:48:31 +01001620 .allstate_action = ns2_sns_st_all_action_bss,
Alexander Couzens6a161492020-07-12 13:45:50 +02001621 .cleanup = NULL,
1622 .timer_cb = ns2_sns_fsm_bss_timer_cb,
Alexander Couzens6a161492020-07-12 13:45:50 +02001623 .event_names = gprs_sns_event_names,
1624 .pre_term = NULL,
1625 .log_subsys = DLNS,
1626};
1627
Harald Welte5bef2cc2020-09-18 22:33:24 +02001628/*! Allocate an IP-SNS FSM for the BSS side.
1629 * \param[in] nse NS Entity in which the FSM runs
1630 * \param[in] id string identifier
Alexander Couzens23aec352021-02-15 05:05:15 +01001631 * \returns FSM instance on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001632struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse,
1633 const char *id)
1634{
1635 struct osmo_fsm_inst *fi;
1636 struct ns2_sns_state *gss;
1637
1638 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id);
1639 if (!fi)
1640 return fi;
1641
1642 gss = talloc_zero(fi, struct ns2_sns_state);
1643 if (!gss)
1644 goto err;
1645
1646 fi->priv = gss;
1647 gss->nse = nse;
Harald Welte4f127462021-03-02 20:49:10 +01001648 gss->role = GPRS_SNS_ROLE_BSS;
Harald Welte24f4df52021-03-04 18:02:54 +01001649 /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */
1650 gss->num_max_ip4_remote = 8192;
1651 gss->num_max_ip6_remote = 8192;
Alexander Couzense769f522020-12-07 07:37:07 +01001652 INIT_LLIST_HEAD(&gss->sns_endpoints);
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001653 INIT_LLIST_HEAD(&gss->binds);
Alexander Couzens6a161492020-07-12 13:45:50 +02001654
1655 return fi;
1656err:
1657 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
1658 return NULL;
1659}
1660
Harald Welte5bef2cc2020-09-18 22:33:24 +02001661/*! main entry point for receiving SNS messages from the network.
1662 * \param[in] nsvc NS-VC on which the message was received
1663 * \param[in] msg message buffer of the IP-SNS message
1664 * \param[in] tp parsed TLV structure of message
Alexander Couzens23aec352021-02-15 05:05:15 +01001665 * \returns 0 on success; negative on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001666int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp)
Alexander Couzens6a161492020-07-12 13:45:50 +02001667{
1668 struct gprs_ns2_nse *nse = nsvc->nse;
1669 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1670 uint16_t nsei = nsvc->nse->nsei;
Harald Welte4f127462021-03-02 20:49:10 +01001671 struct ns2_sns_state *gss;
Alexander Couzens6a161492020-07-12 13:45:50 +02001672 struct osmo_fsm_inst *fi;
Alexander Couzens7619ed42021-03-24 17:44:03 +01001673 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +02001674
1675 if (!nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +01001676 LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n",
1677 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001678 rc = -EINVAL;
1679 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +02001680 }
1681
Alexander Couzens6a161492020-07-12 13:45:50 +02001682 /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */
1683 fi = nse->bss_sns_fi;
Harald Welte4f127462021-03-02 20:49:10 +01001684 gss = (struct ns2_sns_state *) fi->priv;
1685 if (!gss->sns_nsvc)
1686 gss->sns_nsvc = nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001687
Harald Weltef2949742021-01-20 14:54:14 +01001688 LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei,
1689 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
1690
Alexander Couzens6a161492020-07-12 13:45:50 +02001691 switch (nsh->pdu_type) {
1692 case SNS_PDUT_SIZE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001693 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001694 break;
1695 case SNS_PDUT_SIZE_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001696 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001697 break;
1698 case SNS_PDUT_CONFIG:
1699 if (nsh->data[0] & 0x01)
Alexander Couzens67725e22021-02-15 02:37:03 +01001700 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001701 else
Alexander Couzens67725e22021-02-15 02:37:03 +01001702 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001703 break;
1704 case SNS_PDUT_CONFIG_ACK:
Alexander Couzens67725e22021-02-15 02:37:03 +01001705 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001706 break;
1707 case SNS_PDUT_ADD:
Alexander Couzens67725e22021-02-15 02:37:03 +01001708 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001709 break;
1710 case SNS_PDUT_DELETE:
Alexander Couzens67725e22021-02-15 02:37:03 +01001711 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001712 break;
1713 case SNS_PDUT_CHANGE_WEIGHT:
Alexander Couzens67725e22021-02-15 02:37:03 +01001714 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001715 break;
1716 case SNS_PDUT_ACK:
Harald Welteb9f23872021-03-02 20:48:54 +01001717 osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001718 break;
1719 default:
Harald Weltef2949742021-01-20 14:54:14 +01001720 LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei,
1721 get_value_string(gprs_ns_pdu_strings, nsh->pdu_type));
Alexander Couzens7619ed42021-03-24 17:44:03 +01001722 rc = -EINVAL;
Alexander Couzens6a161492020-07-12 13:45:50 +02001723 }
1724
Alexander Couzens7619ed42021-03-24 17:44:03 +01001725out:
1726 msgb_free(msg);
1727
1728 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +02001729}
1730
1731#include <osmocom/vty/vty.h>
1732#include <osmocom/vty/misc.h>
1733
Harald Welte1262c4f2021-01-19 20:58:33 +01001734static 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 +02001735{
1736 struct in_addr in = { .s_addr = ip4->ip_addr };
Harald Welte1262c4f2021-01-19 20:58:33 +01001737 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001738 inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE);
1739}
1740
Harald Welte1262c4f2021-01-19 20:58:33 +01001741static 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 +02001742{
1743 char ip_addr[INET6_ADDRSTRLEN] = {};
1744 if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN)))
1745 strcpy(ip_addr, "Invalid IPv6");
1746
Harald Welte1262c4f2021-01-19 20:58:33 +01001747 vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix,
Alexander Couzens6a161492020-07-12 13:45:50 +02001748 ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE);
1749}
1750
Harald Welte5bef2cc2020-09-18 22:33:24 +02001751/*! Dump the IP-SNS state to a vty.
1752 * \param[in] vty VTY to which the state shall be printed
Harald Welte1262c4f2021-01-19 20:58:33 +01001753 * \param[in] prefix prefix to print at start of each line (typically indenting)
Harald Welte5bef2cc2020-09-18 22:33:24 +02001754 * \param[in] nse NS Entity whose IP-SNS state shall be printed
1755 * \param[in] stats Whether or not statistics shall also be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001756void 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 +02001757{
1758 struct ns2_sns_state *gss;
1759 unsigned int i;
1760
1761 if (!nse->bss_sns_fi)
1762 return;
1763
Harald Welte1262c4f2021-01-19 20:58:33 +01001764 vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001765 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1766
Harald Welte1262c4f2021-01-19 20:58:33 +01001767 vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s",
1768 prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001769
Alexander Couzens71128672021-06-05 18:44:01 +02001770 if (gss->local.num_ip4 && gss->remote.num_ip4) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001771 vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001772 for (i = 0; i < gss->local.num_ip4; i++)
1773 vty_dump_sns_ip4(vty, prefix, &gss->local.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001774
Harald Welte1262c4f2021-01-19 20:58:33 +01001775 vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001776 for (i = 0; i < gss->remote.num_ip4; i++)
1777 vty_dump_sns_ip4(vty, prefix, &gss->remote.ip4[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001778 }
1779
Alexander Couzens71128672021-06-05 18:44:01 +02001780 if (gss->local.num_ip6 && gss->remote.num_ip6) {
Harald Welte1262c4f2021-01-19 20:58:33 +01001781 vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001782 for (i = 0; i < gss->local.num_ip6; i++)
1783 vty_dump_sns_ip6(vty, prefix, &gss->local.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001784
Harald Welte1262c4f2021-01-19 20:58:33 +01001785 vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE);
Alexander Couzens71128672021-06-05 18:44:01 +02001786 for (i = 0; i < gss->remote.num_ip6; i++)
1787 vty_dump_sns_ip6(vty, prefix, &gss->remote.ip6[i]);
Alexander Couzens6a161492020-07-12 13:45:50 +02001788 }
1789}
1790
Alexander Couzens412bc342020-11-19 05:24:37 +01001791/*! write IP-SNS to a vty
1792 * \param[in] vty VTY to which the state shall be printed
1793 * \param[in] nse NS Entity whose IP-SNS state shall be printed */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001794void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse)
Alexander Couzens412bc342020-11-19 05:24:37 +01001795{
1796 struct ns2_sns_state *gss;
1797 struct osmo_sockaddr_str addr_str;
1798 struct sns_endpoint *endpoint;
1799
1800 if (!nse->bss_sns_fi)
1801 return;
1802
1803 gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv;
1804 llist_for_each_entry(endpoint, &gss->sns_endpoints, list) {
Vadim Yanitskiyd8b70032021-01-05 14:24:09 +01001805 /* It's unlikely that an error happens, but let's better be safe. */
1806 if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0)
1807 addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" };
Alexander Couzens5fa431c2021-02-08 23:21:54 +01001808 vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE);
Alexander Couzens412bc342020-11-19 05:24:37 +01001809 }
1810}
1811
Alexander Couzense769f522020-12-07 07:37:07 +01001812static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state,
1813 const struct osmo_sockaddr *saddr)
1814{
1815 struct sns_endpoint *endpoint;
1816
1817 llist_for_each_entry(endpoint, &state->sns_endpoints, list) {
1818 if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr))
1819 return endpoint;
1820 }
1821
1822 return NULL;
1823}
1824
1825/*! gprs_ns2_sns_add_endpoint
1826 * \param[in] nse
1827 * \param[in] sockaddr
1828 * \return
1829 */
1830int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse,
1831 const struct osmo_sockaddr *saddr)
1832{
1833 struct ns2_sns_state *gss;
1834 struct sns_endpoint *endpoint;
1835 bool do_selection = false;
1836
1837 if (nse->ll != GPRS_NS2_LL_UDP) {
1838 return -EINVAL;
1839 }
1840
Alexander Couzens138b96f2021-01-25 16:23:29 +01001841 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001842 return -EINVAL;
1843 }
1844
1845 gss = nse->bss_sns_fi->priv;
1846
1847 if (ns2_get_sns_endpoint(gss, saddr))
1848 return -EADDRINUSE;
1849
1850 endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint);
1851 if (!endpoint)
1852 return -ENOMEM;
1853
1854 endpoint->saddr = *saddr;
1855 if (llist_empty(&gss->sns_endpoints))
1856 do_selection = true;
1857
1858 llist_add_tail(&endpoint->list, &gss->sns_endpoints);
1859 if (do_selection)
Alexander Couzens67725e22021-02-15 02:37:03 +01001860 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL);
Alexander Couzense769f522020-12-07 07:37:07 +01001861
1862 return 0;
1863}
1864
1865/*! gprs_ns2_sns_del_endpoint
1866 * \param[in] nse
1867 * \param[in] sockaddr
1868 * \return 0 on success, otherwise < 0
1869 */
1870int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse,
1871 const struct osmo_sockaddr *saddr)
1872{
1873 struct ns2_sns_state *gss;
1874 struct sns_endpoint *endpoint;
1875
1876 if (nse->ll != GPRS_NS2_LL_UDP) {
1877 return -EINVAL;
1878 }
1879
Alexander Couzens138b96f2021-01-25 16:23:29 +01001880 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001881 return -EINVAL;
1882 }
1883
1884 gss = nse->bss_sns_fi->priv;
1885 endpoint = ns2_get_sns_endpoint(gss, saddr);
1886 if (!endpoint)
1887 return -ENOENT;
1888
1889 /* if this is an unused SNS endpoint it's done */
1890 if (gss->initial != endpoint) {
1891 llist_del(&endpoint->list);
1892 talloc_free(endpoint);
1893 return 0;
1894 }
1895
Alexander Couzens67725e22021-02-15 02:37:03 +01001896 /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC
Alexander Couzense769f522020-12-07 07:37:07 +01001897 * and restart SNS SIZE procedure which selects a new initial */
Harald Weltef2949742021-01-20 14:54:14 +01001898 LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed."
Alexander Couzense769f522020-12-07 07:37:07 +01001899 "Closing all NS-VC and restart SNS-SIZE procedure"
1900 "with a remaining SNS endpoint.\n");
1901
1902 /* Continue with the next endpoint in the list.
1903 * Special case if the endpoint is at the start or end of the list */
1904 if (endpoint->list.prev == &gss->sns_endpoints ||
1905 endpoint->list.next == &gss->sns_endpoints)
1906 gss->initial = NULL;
1907 else
1908 gss->initial = llist_entry(endpoint->list.next->prev,
1909 struct sns_endpoint,
1910 list);
1911
1912 llist_del(&endpoint->list);
1913 gprs_ns2_free_nsvcs(nse);
1914 talloc_free(endpoint);
1915
1916 return 0;
1917}
1918
1919/*! gprs_ns2_sns_count
1920 * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed
1921 * \return the count of endpoints or < 0 if NSE doesn't contain sns.
1922 */
1923int gprs_ns2_sns_count(struct gprs_ns2_nse *nse)
1924{
1925 struct ns2_sns_state *gss;
1926 struct sns_endpoint *endpoint;
1927 int count = 0;
1928
1929 if (nse->ll != GPRS_NS2_LL_UDP) {
1930 return -EINVAL;
1931 }
1932
Alexander Couzens138b96f2021-01-25 16:23:29 +01001933 if (nse->dialect != GPRS_NS2_DIALECT_SNS) {
Alexander Couzense769f522020-12-07 07:37:07 +01001934 return -EINVAL;
1935 }
1936
1937 gss = nse->bss_sns_fi->priv;
1938 llist_for_each_entry(endpoint, &gss->sns_endpoints, list)
1939 count++;
1940
1941 return count;
1942}
1943
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001944void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive)
1945{
1946 struct ns2_sns_state *gss;
1947 struct gprs_ns2_vc *tmp;
1948
1949 if (!nse->bss_sns_fi)
1950 return;
1951
1952 gss = nse->bss_sns_fi->priv;
1953 if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED)
1954 return;
1955
1956 if (alive == gss->alive)
1957 return;
1958
1959 /* check if this is the current SNS NS-VC */
1960 if (nsvc == gss->sns_nsvc) {
1961 /* only replace the SNS NS-VC if there are other alive NS-VC.
1962 * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED
1963 * and couldn't confirm yet if the NS-VC comes up */
1964 if (gss->alive && !alive)
1965 ns2_sns_replace_nsvc(nsvc);
1966 }
1967
1968 if (alive) {
1969 gss->alive = true;
Alexander Couzens67725e22021-02-15 02:37:03 +01001970 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001971 } else {
1972 /* is there at least another alive nsvc? */
1973 llist_for_each_entry(tmp, &nse->nsvc, list) {
1974 if (ns2_vc_is_unblocked(tmp))
1975 return;
1976 }
1977
1978 /* all NS-VC have failed */
1979 gss->alive = false;
Alexander Couzens67725e22021-02-15 02:37:03 +01001980 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL);
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001981 }
1982}
1983
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001984int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse,
1985 struct gprs_ns2_vc_bind *bind)
1986{
1987 struct ns2_sns_state *gss;
1988 struct ns2_sns_bind *tmp;
1989
1990 OSMO_ASSERT(nse->bss_sns_fi);
1991 gss = nse->bss_sns_fi->priv;
1992
1993 if (!gprs_ns2_is_ip_bind(bind)) {
1994 return -EINVAL;
1995 }
1996
1997 if (!llist_empty(&gss->binds)) {
1998 llist_for_each_entry(tmp, &gss->binds, list) {
1999 if (tmp->bind == bind)
2000 return -EALREADY;
2001 }
2002 }
2003
2004 tmp = talloc_zero(gss, struct ns2_sns_bind);
2005 if (!tmp)
2006 return -ENOMEM;
2007 tmp->bind = bind;
2008 llist_add_tail(&tmp->list, &gss->binds);
2009
2010 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp);
2011 return 0;
2012}
2013
2014/* Remove a bind from the SNS. All assosiated NSVC must be removed. */
2015int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse,
2016 struct gprs_ns2_vc_bind *bind)
2017{
2018 struct ns2_sns_state *gss;
2019 struct ns2_sns_bind *tmp, *tmp2;
2020 bool found = false;
2021
2022 if (!nse->bss_sns_fi)
2023 return -EINVAL;
2024
2025 gss = nse->bss_sns_fi->priv;
2026 if (gss->initial_bind && gss->initial_bind->bind == bind) {
2027 if (gss->initial_bind->list.prev == &gss->binds)
2028 gss->initial_bind = NULL;
2029 else
2030 gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list);
2031 }
2032
2033 llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) {
2034 if (tmp->bind == bind) {
2035 llist_del(&tmp->list);
2036 found = true;
Alexander Couzensa35c2962021-04-19 03:30:15 +02002037 break;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01002038 }
2039 }
2040
2041 if (!found)
2042 return -ENOENT;
2043
2044 osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp);
2045 return 0;
2046}
2047
Alexander Couzens71128672021-06-05 18:44:01 +02002048/* Update SNS weights for a bind (local endpoint).
2049 * \param[in] bind the bind which has been updated
Alexander Couzensc4704762021-02-08 23:13:12 +01002050 */
2051void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind)
2052{
2053 /* TODO: implement weights after binds per sns implemented */
2054}
2055
Harald Welte4f127462021-03-02 20:49:10 +01002056
2057
2058
2059/***********************************************************************
2060 * SGSN role
2061 ***********************************************************************/
2062
2063static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2064{
2065 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2066 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2067 /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */
2068}
2069
2070/* We're waiting for inbound SNS-CONFIG from the BSS */
2071static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2072{
2073 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2074 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2075 struct gprs_ns2_inst *nsi = nse->nsi;
2076 uint8_t cause;
2077 int rc;
2078
2079 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2080
2081 switch (event) {
2082 case GPRS_SNS_EV_RX_CONFIG:
2083 case GPRS_SNS_EV_RX_CONFIG_END:
2084 rc = ns_sns_append_remote_eps(fi, data);
2085 if (rc < 0) {
2086 cause = -rc;
2087 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2088 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2089 return;
2090 }
2091 /* only change state if last CONFIG was received */
2092 if (event == GPRS_SNS_EV_RX_CONFIG_END) {
2093 /* ensure sum of data weight / sig weights is > 0 */
Alexander Couzens019da4b2021-06-06 02:48:18 +02002094 if (ip46_weight_sum_data(&gss->remote) == 0 || ip46_weight_sum_sig(&gss->remote) == 0) {
Harald Welte4f127462021-03-02 20:49:10 +01002095 cause = NS_CAUSE_INVAL_WEIGH;
2096 ns2_tx_sns_config_ack(gss->sns_nsvc, &cause);
2097 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2098 break;
2099 }
2100 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2101 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2102 } else {
2103 /* just send CONFIG-ACK */
2104 ns2_tx_sns_config_ack(gss->sns_nsvc, NULL);
2105 osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0);
2106 }
2107 break;
2108 }
2109}
2110
2111static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)
2112{
2113 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2114 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2115
Harald Welte4f127462021-03-02 20:49:10 +01002116 /* transmit SGSN-oriented SNS-CONFIG */
Alexander Couzens71128672021-06-05 18:44:01 +02002117 ns2_tx_sns_config(gss->sns_nsvc, true, gss->local.ip4, gss->local.num_ip4,
2118 gss->local.ip6, gss->local.num_ip6);
Harald Welte4f127462021-03-02 20:49:10 +01002119}
2120
2121/* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */
2122static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2123{
2124 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2125 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2126 struct tlv_parsed *tp = NULL;
2127
2128 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2129
2130 switch (event) {
2131 case GPRS_SNS_EV_RX_CONFIG_ACK:
2132 tp = data;
2133 if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) {
2134 LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n",
2135 gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE)));
2136 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
2137 break;
2138 }
2139 /* we currently only send one SNS-CONFIG with END FLAG */
2140 if (true) {
2141 create_missing_nsvcs(fi);
2142 /* start the test procedure on ALL NSVCs! */
2143 gprs_ns2_start_alive_all_nsvcs(nse);
2144 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4);
2145 }
2146 break;
2147 }
2148}
2149
2150/* SGSN-side SNS state machine */
2151static const struct osmo_fsm_state ns2_sns_sgsn_states[] = {
2152 [GPRS_SNS_ST_UNCONFIGURED] = {
2153 .in_event_mask = 0, /* handled by all_state_action */
2154 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2155 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG),
2156 .name = "UNCONFIGURED",
2157 .action = ns2_sns_st_sgsn_unconfigured,
2158 },
2159 [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = {
2160 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) |
2161 S(GPRS_SNS_EV_RX_CONFIG_END),
2162 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2163 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) |
2164 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK),
2165 .name = "SGSN_WAIT_CONFIG",
2166 .action = ns2_sns_st_sgsn_wait_config,
2167 },
2168 [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = {
2169 .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK),
2170 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) |
2171 S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) |
2172 S(GPRS_SNS_ST_CONFIGURED),
2173 .name = "SGSN_WAIT_CONFIG_ACK",
2174 .action = ns2_sns_st_sgsn_wait_config_ack,
2175 .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter,
2176 },
2177 [GPRS_SNS_ST_CONFIGURED] = {
2178 .in_event_mask = S(GPRS_SNS_EV_RX_ADD) |
2179 S(GPRS_SNS_EV_RX_DELETE) |
2180 S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) |
2181 S(GPRS_SNS_EV_REQ_NSVC_ALIVE),
2182 .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED),
2183 .name = "CONFIGURED",
2184 /* shared with BSS side; once configured there's no difference */
2185 .action = ns2_sns_st_configured,
2186 .onenter = ns2_sns_st_configured_onenter,
2187 },
2188};
2189
2190static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi)
2191{
2192 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2193 struct gprs_ns2_nse *nse = nse_inst_from_fi(fi);
2194 struct gprs_ns2_inst *nsi = nse->nsi;
2195
2196 gss->N++;
2197 switch (fi->T) {
2198 case 3:
2199 if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) {
2200 LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei);
2201 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2202 } else {
2203 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3);
2204 }
2205 break;
2206 case 4:
2207 LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei);
2208 break;
2209 }
2210 return 0;
2211}
2212
2213
2214/* allstate-action for SGSN role */
2215static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data)
2216{
2217 struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv;
2218 struct tlv_parsed *tp = NULL;
Harald Welte01fa6a32021-03-04 19:49:38 +01002219 size_t num_local_eps, num_remote_eps;
Harald Welte4f127462021-03-02 20:49:10 +01002220 uint8_t flag;
Harald Weltea2c5af52021-03-04 17:59:35 +01002221 uint8_t cause;
Harald Welte4f127462021-03-02 20:49:10 +01002222
2223 OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN);
2224
2225 switch (event) {
2226 case GPRS_SNS_EV_RX_SIZE:
2227 tp = (struct tlv_parsed *) data;
Harald Weltea2c5af52021-03-04 17:59:35 +01002228 /* check for mandatory / conditional IEs */
2229 if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) ||
2230 !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) {
2231 cause = NS_CAUSE_MISSING_ESSENT_IE;
2232 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2233 break;
2234 }
2235 if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) &&
2236 !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) {
2237 cause = NS_CAUSE_MISSING_ESSENT_IE;
Harald Welte4f127462021-03-02 20:49:10 +01002238 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2239 break;
2240 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002241 if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2))
2242 gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR);
2243 if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2))
2244 gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR);
2245 /* decide if we go for IPv4 or IPv6 */
Alexander Couzens077ce5a2021-06-06 01:32:45 +02002246 if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, AF_INET6)) {
2247 gss->family = AF_INET6;
Harald Welte2d807b62021-03-24 01:57:30 +01002248 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002249 num_local_eps = gss->local.num_ip6;
Harald Welte01fa6a32021-03-04 19:49:38 +01002250 num_remote_eps = gss->num_max_ip6_remote;
Alexander Couzens077ce5a2021-06-06 01:32:45 +02002251 } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, AF_INET)) {
2252 gss->family = AF_INET;
Harald Welte2d807b62021-03-24 01:57:30 +01002253 ns2_sns_compute_local_ep_from_binds(fi);
Alexander Couzens71128672021-06-05 18:44:01 +02002254 num_local_eps = gss->local.num_ip4;
Harald Welte01fa6a32021-03-04 19:49:38 +01002255 num_remote_eps = gss->num_max_ip4_remote;
2256 } else {
Alexander Couzens71128672021-06-05 18:44:01 +02002257 if (gss->local.num_ip4 && !gss->num_max_ip4_remote)
Harald Welte01fa6a32021-03-04 19:49:38 +01002258 cause = NS_CAUSE_INVAL_NR_IPv4_EP;
2259 else
2260 cause = NS_CAUSE_INVAL_NR_IPv6_EP;
2261 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2262 break;
2263 }
Harald Welte01fa6a32021-03-04 19:49:38 +01002264 /* ensure number of NS-VCs is sufficient for full mesh */
2265 gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC);
2266 if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) {
2267 LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, "
2268 "but BSS supports only %zu maximum NS-VCs\n", num_local_eps,
2269 num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs);
2270 cause = NS_CAUSE_INVAL_NR_NS_VC;
2271 ns2_tx_sns_size_ack(gss->sns_nsvc, &cause);
2272 break;
2273 }
2274 /* perform state reset, if requested */
Harald Welte4f127462021-03-02 20:49:10 +01002275 flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG);
2276 if (flag & 1) {
2277 struct gprs_ns2_vc *nsvc, *nsvc2;
2278 /* clear all state */
Harald Welte46eb7642021-03-04 17:49:59 +01002279 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0);
Harald Welte4f127462021-03-02 20:49:10 +01002280 gss->N = 0;
Alexander Couzensd2c6c492021-06-06 01:57:52 +02002281 ns2_clear_elems(&gss->local);
2282 ns2_clear_elems(&gss->remote);
Harald Welte4f127462021-03-02 20:49:10 +01002283 llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) {
2284 if (nsvc == gss->sns_nsvc) {
2285 /* keep the NSVC we need for SNS, but unconfigure it */
2286 nsvc->sig_weight = 0;
2287 nsvc->data_weight = 0;
2288 ns2_vc_force_unconfigured(nsvc);
2289 } else {
2290 /* free all other NS-VCs */
2291 gprs_ns2_free_nsvc(nsvc);
2292 }
2293 }
Harald Welte2d807b62021-03-24 01:57:30 +01002294 ns2_sns_compute_local_ep_from_binds(fi);
Harald Welte4f127462021-03-02 20:49:10 +01002295 }
2296 /* send SIZE_ACK */
2297 ns2_tx_sns_size_ack(gss->sns_nsvc, NULL);
2298 /* only wait for SNS-CONFIG in case of Reset flag */
2299 if (flag & 1)
2300 osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0);
2301 break;
2302 default:
2303 ns2_sns_st_all_action(fi, event, data);
2304 break;
2305 }
2306}
2307
2308static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = {
2309 .name = "GPRS-NS2-SNS-SGSN",
2310 .states = ns2_sns_sgsn_states,
2311 .num_states = ARRAY_SIZE(ns2_sns_sgsn_states),
2312 .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) |
2313 S(GPRS_SNS_EV_REQ_NO_NSVC) |
2314 S(GPRS_SNS_EV_REQ_ADD_BIND) |
2315 S(GPRS_SNS_EV_REQ_DELETE_BIND),
2316 .allstate_action = ns2_sns_st_all_action_sgsn,
2317 .cleanup = NULL,
2318 .timer_cb = ns2_sns_fsm_sgsn_timer_cb,
2319 .event_names = gprs_sns_event_names,
2320 .pre_term = NULL,
2321 .log_subsys = DLNS,
2322};
2323
2324/*! Allocate an IP-SNS FSM for the SGSN side.
2325 * \param[in] nse NS Entity in which the FSM runs
2326 * \param[in] id string identifier
2327 * \returns FSM instance on success; NULL on error */
2328struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id)
2329{
2330 struct osmo_fsm_inst *fi;
2331 struct ns2_sns_state *gss;
2332
2333 fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id);
2334 if (!fi)
2335 return fi;
2336
2337 gss = talloc_zero(fi, struct ns2_sns_state);
2338 if (!gss)
2339 goto err;
2340
2341 fi->priv = gss;
2342 gss->nse = nse;
2343 gss->role = GPRS_SNS_ROLE_SGSN;
2344 INIT_LLIST_HEAD(&gss->sns_endpoints);
2345 INIT_LLIST_HEAD(&gss->binds);
2346
2347 return fi;
2348err:
2349 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
2350 return NULL;
2351}
2352
2353
2354
2355
Alexander Couzens6a161492020-07-12 13:45:50 +02002356/* initialize osmo_ctx on main tread */
2357static __attribute__((constructor)) void on_dso_load_ctx(void)
2358{
2359 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0);
Harald Welte4f127462021-03-02 20:49:10 +01002360 OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02002361}