blob: 813bd584c26c8e93e3b19587efa7af3338527766 [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \file gprs_ns2.c
2 * GPRS Networks Service (NS) messages on the Gb interface.
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
6/* (C) 2009-2018 by Harald Welte <laforge@gnumonks.org>
7 * (C) 2016-2017,2020 sysmocom - s.f.m.c. GmbH
8 * Author: Alexander Couzens <lynxis@fe80.eu>
9 *
10 *
11 * All Rights Reserved
12 *
13 * SPDX-License-Identifier: GPL-2.0+
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 *
28 */
29
30/*! \addtogroup libgb
31 * @{
32 *
33 * GPRS Networks Service (NS) messages on the Gb interface
34 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
35 *
36 * Some introduction into NS: NS is used typically on top of frame relay,
37 * but in the ip.access world it is encapsulated in UDP packets. It serves
38 * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't
39 * do much, apart from providing congestion notification and status indication.
40 *
41 * Terms:
42 *
43 * NS Network Service
44 * NSVC NS Virtual Connection
45 * NSEI NS Entity Identifier
46 * NSVL NS Virtual Link
47 * NSVLI NS Virtual Link Identifier
48 * BVC BSSGP Virtual Connection
49 * BVCI BSSGP Virtual Connection Identifier
50 * NSVCG NS Virtual Connection Goup
51 * Blocked NS-VC cannot be used for user traffic
52 * Alive Ability of a NS-VC to provide communication
53 *
54 * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will
55 * therefore identify the BSSGP virtual connection by a BVCI passed down to NS.
56 * NS then has to figure out which NSVC's are responsible for this BVCI.
57 * Those mappings are administratively configured.
58 *
59 * This implementation has the following limitations:
Alexander Couzens6a161492020-07-12 13:45:50 +020060 * - NSVCI 65535 and 65534 are reserved for internal use
Alexander Couzens6a161492020-07-12 13:45:50 +020061 * - There are no BLOCK and UNBLOCK timers (yet?)
62 *
63 * \file gprs_ns2.c */
64
65#include <stdlib.h>
66#include <unistd.h>
67#include <errno.h>
68#include <stdint.h>
69
70#include <sys/types.h>
71#include <sys/socket.h>
72#include <arpa/inet.h>
73
74#include <osmocom/core/fsm.h>
Daniel Willmann751977b2020-12-02 18:59:44 +010075#include <osmocom/core/logging.h>
Alexander Couzens6a161492020-07-12 13:45:50 +020076#include <osmocom/core/msgb.h>
77#include <osmocom/core/rate_ctr.h>
78#include <osmocom/core/socket.h>
79#include <osmocom/core/sockaddr_str.h>
80#include <osmocom/core/stats.h>
81#include <osmocom/core/stat_item.h>
82#include <osmocom/core/talloc.h>
83#include <osmocom/gprs/gprs_msgb.h>
84#include <osmocom/gsm/prim.h>
85#include <osmocom/gsm/tlv.h>
86
87#include "gprs_ns2_internal.h"
88
89#define ns_set_state(ns_, st_) ns_set_state_with_log(ns_, st_, false, __FILE__, __LINE__)
90#define ns_set_remote_state(ns_, st_) ns_set_state_with_log(ns_, st_, true, __FILE__, __LINE__)
91#define ns_mark_blocked(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_BLOCKED)
92#define ns_mark_unblocked(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_BLOCKED));
93#define ns_mark_alive(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_ALIVE)
94#define ns_mark_dead(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_ALIVE));
95
96/* HACK: The NS_IE_IP_ADDR does not follow any known TLV rules.
97 * Since it's a hard ABI break to implement 16 bit tag with fixed length entries to workaround it,
98 * the parser will be called with ns_att_tlvdef1 and if it's failed with ns_att_tlvdef2.
99 * The TLV parser depends on 8bit tag in many places.
100 * The NS_IE_IP_ADDR is only valid for SNS_ACK SNS_ADD and SNS_DELETE.
101 */
102static const struct tlv_definition ns_att_tlvdef1 = {
103 .def = {
104 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
105 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
106 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
107 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
108 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
109 [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 },
110 [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 },
111 [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 },
112 [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 },
113 [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 },
114 [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 },
115 /* NS_IE_IP_ADDR in the IPv4 version */
116 [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 5 },
117 },
118};
119
120static const struct tlv_definition ns_att_tlvdef2 = {
121 .def = {
122 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
123 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
124 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
125 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
126 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
127 [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 },
128 [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 },
129 [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 },
130 [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 },
131 [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 },
132 [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 },
133 /* NS_IE_IP_ADDR in the IPv6 version */
134 [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 17 },
135 },
136};
137
138
139/* Section 10.3.2, Table 13 */
Alexander Couzensb3b837c2020-10-27 15:12:25 +0100140const struct value_string gprs_ns2_cause_strs[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200141 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
142 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
143 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
144 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
145 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
146 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
147 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
148 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
149 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
150 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
151 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
152 { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" },
153 { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" },
154 { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" },
155 { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" },
156 { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" },
157 { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" },
158 { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" },
159 { 0, NULL }
160};
161
Alexander Couzens6a161492020-07-12 13:45:50 +0200162static const struct rate_ctr_desc nsvc_ctr_description[] = {
163 { "packets:in", "Packets at NS Level ( In)" },
164 { "packets:out","Packets at NS Level (Out)" },
165 { "bytes:in", "Bytes at NS Level ( In)" },
166 { "bytes:out", "Bytes at NS Level (Out)" },
167 { "blocked", "NS-VC Block count " },
168 { "dead", "NS-VC gone dead count " },
169 { "replaced", "NS-VC replaced other count" },
170 { "nsei-chg", "NS-VC changed NSEI count " },
171 { "inv-nsvci", "NS-VCI was invalid count " },
172 { "inv-nsei", "NSEI was invalid count " },
173 { "lost:alive", "ALIVE ACK missing count " },
174 { "lost:reset", "RESET ACK missing count " },
175};
176
177static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
178 .group_name_prefix = "ns:nsvc",
179 .group_description = "NSVC Peer Statistics",
180 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
181 .ctr_desc = nsvc_ctr_description,
182 .class_id = OSMO_STATS_CLASS_PEER,
183};
184
185
186static const struct osmo_stat_item_desc nsvc_stat_description[] = {
187 { "alive.delay", "ALIVE response time ", "ms", 16, 0 },
188};
189
190static const struct osmo_stat_item_group_desc nsvc_statg_desc = {
191 .group_name_prefix = "ns.nsvc",
192 .group_description = "NSVC Peer Statistics",
193 .num_items = ARRAY_SIZE(nsvc_stat_description),
194 .item_desc = nsvc_stat_description,
195 .class_id = OSMO_STATS_CLASS_PEER,
196};
197
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100198const struct value_string gprs_ns2_aff_cause_prim_strs[] = {
199 { NS_AFF_CAUSE_VC_FAILURE, "NSVC failure" },
200 { NS_AFF_CAUSE_VC_RECOVERY, "NSVC recovery" },
201 { NS_AFF_CAUSE_FAILURE, "NSE failure" },
202 { NS_AFF_CAUSE_RECOVERY, "NSE recovery" },
203 { NS_AFF_CAUSE_SNS_CONFIGURED, "NSE SNS configured" },
204 { NS_AFF_CAUSE_SNS_FAILURE, "NSE SNS failure" },
205 { 0, NULL }
206};
207
Alexander Couzens0ab028c2020-11-04 02:41:44 +0100208const struct value_string gprs_ns2_prim_strs[] = {
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100209 { PRIM_NS_UNIT_DATA, "UNIT DATA" },
210 { PRIM_NS_CONGESTION, "CONGESTION" },
211 { PRIM_NS_STATUS, "STATUS" },
212 { 0, NULL }
213};
214
Harald Weltea24e7ee2020-11-29 17:38:48 +0100215const struct value_string gprs_ns2_lltype_strs[] = {
216 { GPRS_NS2_LL_UDP, "UDP" },
217 { GPRS_NS2_LL_FR_GRE, "FR_GRE" },
218 { GPRS_NS2_LL_FR, "FR" },
219 { 0, NULL }
220};
221
Harald Welte5bef2cc2020-09-18 22:33:24 +0200222/*! string-format a given NS-VC into a user-supplied buffer.
223 * \param[in] buf user-allocated output buffer
224 * \param[in] buf_len size of user-allocated output buffer in bytes
225 * \param[in] nsvc NS-VC to be string-formatted
226 * \return pointer to buf on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200227char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc)
228{
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200229 const struct osmo_sockaddr *local;
230 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200231 struct osmo_sockaddr_str local_str;
232 struct osmo_sockaddr_str remote_str;
233
234 if (!buf_len)
Harald Welte92ad0292020-09-18 22:34:24 +0200235 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200236
Alexander Couzensaac90162020-11-19 02:44:04 +0100237 switch (nsvc->nse->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100238 case GPRS_NS2_LL_UDP:
Alexander Couzens6a161492020-07-12 13:45:50 +0200239 if (!gprs_ns2_is_ip_bind(nsvc->bind)) {
240 buf[0] = '\0';
241 return buf;
242 }
243
244 local = gprs_ns2_ip_bind_sockaddr(nsvc->bind);
Alexander Couzensc4229a42020-10-11 20:58:04 +0200245 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200246 if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas))
247 strcpy(local_str.ip, "invalid");
248 if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas))
249 strcpy(remote_str.ip, "invalid");
250
251 if (nsvc->nsvci_is_valid)
252 snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u",
253 local_str.ip, local_str.port,
254 nsvc->nsvci,
255 remote_str.ip, remote_str.port);
256 else
257 snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u",
258 local_str.ip, local_str.port,
259 remote_str.ip, remote_str.port);
260 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100261 case GPRS_NS2_LL_FR_GRE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200262 snprintf(buf, buf_len, "frgre)");
263 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100264 case GPRS_NS2_LL_FR:
Alexander Couzens841817e2020-11-19 00:41:29 +0100265 snprintf(buf, buf_len, "fr)netif: %s dlci: %u", gprs_ns2_fr_bind_netif(nsvc->bind),
266 gprs_ns2_fr_nsvc_dlci(nsvc));
267 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200268 default:
Harald Welte6188e002020-12-01 17:20:07 +0100269 snprintf(buf, buf_len, "unknown)");
Alexander Couzens6a161492020-07-12 13:45:50 +0200270 break;
271 }
272
273 buf[buf_len - 1] = '\0';
274
275 return buf;
276}
277
278/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
279#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
280
Harald Welte5bef2cc2020-09-18 22:33:24 +0200281/*! string-format a given NS-VC to a thread-local static buffer.
282 * \param[in] nsvc NS-VC to be string-formatted
283 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200284const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
285{
286 static __thread char buf[NS2_LL_MAX_STR];
287 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
288}
289
Harald Welte5bef2cc2020-09-18 22:33:24 +0200290/*! string-format a given NS-VC to a dynamically allocated string.
291 * \param[in] ctx talloc context from which to allocate
292 * \param[in] nsvc NS-VC to be string-formatted
293 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200294char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
295{
296 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
297 if (!buf)
298 return buf;
299 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
300}
301
Daniel Willmannf1286542020-11-03 23:03:33 +0100302/*! Return the current state name of a given NS-VC to a thread-local static buffer.
303 * \param[in] nsvc NS-VC to return the state of
304 * \return pointer to the string on success; NULL on error */
305const char *gprs_ns2_nsvc_state_name(struct gprs_ns2_vc *nsvc)
306{
307 return osmo_fsm_inst_state_name(nsvc->fi);
308}
309
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100310/* select a signalling NSVC and respect sig_counter
311 * param[out] reset_counter - all counter has to be resetted to their signal weight
312 * return the chosen nsvc or NULL
313 */
314static struct gprs_ns2_vc *ns2_load_sharing_signal(struct gprs_ns2_nse *nse)
315{
316 struct gprs_ns2_vc *nsvc = NULL, *last = NULL, *tmp;
317
318 llist_for_each_entry(tmp, &nse->nsvc, list) {
319 if (tmp->sig_weight == 0)
320 continue;
321 if (!gprs_ns2_vc_is_unblocked(tmp))
322 continue;
323 if (tmp->sig_counter == 0) {
324 last = tmp;
325 continue;
326 }
327
328 tmp->sig_counter--;
329 nsvc = tmp;
330 break;
331 }
332
333 /* all counter were zero, but there are valid nsvc */
334 if (!nsvc && last) {
335 llist_for_each_entry(tmp, &nse->nsvc, list) {
336 tmp->sig_counter = tmp->sig_weight;
337 }
338
339 last->sig_counter--;
340 return last;
341 } else {
342 return nsvc;
343 }
344}
345
346/* 4.4.1 Load Sharing function for the Frame Relay Sub-Network */
347static struct gprs_ns2_vc *ns2_load_sharing_modulor(
348 struct gprs_ns2_nse *nse,
349 uint16_t bvci,
350 uint32_t load_selector)
351{
352 struct gprs_ns2_vc *tmp;
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100353 uint32_t mod = (bvci + load_selector) % nse->nsvc_count;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100354 uint32_t i = 0;
355
356 llist_for_each_entry(tmp, &nse->nsvc, list) {
357 if (!gprs_ns2_vc_is_unblocked(tmp))
358 continue;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100359 if (i == mod)
360 return tmp;
361 i++;
362 }
363
364 return NULL;
365}
366
367/* pick the first available data NSVC - no load sharing */
368struct gprs_ns2_vc *ns2_load_sharing_first(struct gprs_ns2_nse *nse)
369{
370 struct gprs_ns2_vc *nsvc = NULL, *tmp;
371
372 llist_for_each_entry(tmp, &nse->nsvc, list) {
373 if (!gprs_ns2_vc_is_unblocked(tmp))
374 continue;
375 if (tmp->data_weight == 0)
376 continue;
377
378 nsvc = tmp;
379 break;
380 }
381
382 return nsvc;
383}
384
385
386static struct gprs_ns2_vc *ns2_load_sharing(
387 struct gprs_ns2_nse *nse,
388 uint16_t bvci,
389 uint32_t link_selector)
390{
391 struct gprs_ns2_vc *nsvc = NULL;
392
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100393 switch (nse->ll) {
394 case GPRS_NS2_LL_FR:
395 nsvc = ns2_load_sharing_modulor(nse, bvci, link_selector);
396 break;
397 case GPRS_NS2_LL_UDP:
398 default:
399 if (bvci == 0) {
400 /* signalling */
401 nsvc = ns2_load_sharing_signal(nse);
402 } else {
403 /* data with load sharing parameter */
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100404 nsvc = ns2_load_sharing_first(nse);
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100405 }
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100406 break;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100407 }
408
409 return nsvc;
410}
411
Harald Welte5bef2cc2020-09-18 22:33:24 +0200412/*! Receive a primitive from the NS User (Gb).
413 * \param[in] nsi NS instance to which the primitive is issued
414 * \param[in] oph The primitive
415 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200416int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
417{
Alexander Couzens6a161492020-07-12 13:45:50 +0200418 /* TODO: implement resource distribution */
419 /* TODO: check for empty PDUs which can be sent to Request/Confirm
420 * the IP endpoint */
421 struct osmo_gprs_ns2_prim *nsp;
422 struct gprs_ns2_nse *nse = NULL;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100423 struct gprs_ns2_vc *nsvc = NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200424 uint16_t bvci, nsei;
425 uint8_t sducontrol = 0;
426
427 if (oph->sap != SAP_NS)
428 return -EINVAL;
429
430 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
431
432 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != PRIM_NS_UNIT_DATA)
433 return -EINVAL;
434
435 if (!oph->msg)
436 return -EINVAL;
437
438 bvci = nsp->bvci;
439 nsei = nsp->nsei;
440
441 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
442 if (!nse)
443 return -EINVAL;
444
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100445 nsvc = ns2_load_sharing(nse, bvci, nsp->u.unitdata.link_selector);
Alexander Couzens6a161492020-07-12 13:45:50 +0200446
447 /* TODO: send a status primitive back */
448 if (!nsvc)
449 return 0;
450
451 if (nsp->u.unitdata.change == NS_ENDPOINT_REQUEST_CHANGE)
452 sducontrol = 1;
453 else if (nsp->u.unitdata.change == NS_ENDPOINT_CONFIRM_CHANGE)
454 sducontrol = 2;
455
456 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
457}
458
Harald Welte5bef2cc2020-09-18 22:33:24 +0200459/*! Send a STATUS.ind primitive to the specified NS instance user.
460 * \param[in] nsi NS instance on which we operate
461 * \param[in] nsei NSEI to which the statue relates
462 * \param[in] bvci BVCI to which the status relates
463 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200464void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100465 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200466 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200467 enum gprs_ns2_affecting_cause cause)
468{
Daniel Willmann15c09a82020-11-03 23:05:43 +0100469 char nsvc_str[NS2_LL_MAX_STR];
Alexander Couzens6a161492020-07-12 13:45:50 +0200470 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200471 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200472 nsp.bvci = bvci;
473 nsp.u.status.cause = cause;
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100474 nsp.u.status.transfer = ns2_count_transfer_cap(nse, bvci);
Alexander Couzensda0a2852020-10-01 23:24:07 +0200475 nsp.u.status.first = nse->first;
476 nsp.u.status.persistent = nse->persistent;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100477 if (nsvc)
478 nsp.u.status.nsvc = gprs_ns2_ll_str_buf(nsvc_str, sizeof(nsvc_str), nsvc);
479
Alexander Couzens6a161492020-07-12 13:45:50 +0200480 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS,
481 PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200482 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200483}
484
Harald Welte5bef2cc2020-09-18 22:33:24 +0200485/*! Allocate a NS-VC within the given bind + NSE.
486 * \param[in] bind The 'bind' on which we operate
487 * \param[in] nse The NS Entity on which we operate
488 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
489 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzensd923cff2020-12-01 01:03:52 +0100490struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater,
491 enum gprs_ns2_vc_mode vc_mode)
Alexander Couzens6a161492020-07-12 13:45:50 +0200492{
Daniel Willmannbb899052021-01-16 14:02:45 +0100493 /* Sanity check */
494 OSMO_ASSERT(bind->ll == nse->ll);
495
Alexander Couzens6a161492020-07-12 13:45:50 +0200496 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
497
498 if (!nsvc)
499 return NULL;
500
501 nsvc->bind = bind;
502 nsvc->nse = nse;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100503 nsvc->mode = vc_mode;
Alexander Couzens6a161492020-07-12 13:45:50 +0200504 nsvc->sig_weight = 1;
505 nsvc->data_weight = 1;
506
507 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx);
508 if (!nsvc->ctrg) {
509 goto err;
510 }
511 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx);
512 if (!nsvc->statg)
513 goto err_group;
514 if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater))
515 goto err_statg;
516
517 bind->nsi->rate_ctr_idx++;
518
519 llist_add(&nsvc->list, &nse->nsvc);
520 llist_add(&nsvc->blist, &bind->nsvc);
521
522 return nsvc;
523
524err_statg:
525 osmo_stat_item_group_free(nsvc->statg);
526err_group:
527 rate_ctr_group_free(nsvc->ctrg);
528err:
529 talloc_free(nsvc);
530
531 return NULL;
532}
533
Harald Welte5bef2cc2020-09-18 22:33:24 +0200534/*! Destroy/release given NS-VC.
535 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200536void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
537{
538 if (!nsvc)
539 return;
540
Daniel Willmann15c09a82020-11-03 23:05:43 +0100541 ns2_prim_status_ind(nsvc->nse, nsvc, 0, NS_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200542
543 llist_del(&nsvc->list);
544 llist_del(&nsvc->blist);
545
546 /* notify nse this nsvc is unavailable */
547 ns2_nse_notify_unblocked(nsvc, false);
548
549 /* check if sns is using this VC */
550 ns2_sns_free_nsvc(nsvc);
551 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
552
553 /* let the driver/bind clean up it's internal state */
554 if (nsvc->priv && nsvc->bind->free_vc)
555 nsvc->bind->free_vc(nsvc);
556
557 osmo_stat_item_group_free(nsvc->statg);
558 rate_ctr_group_free(nsvc->ctrg);
559
560 talloc_free(nsvc);
561}
562
Alexander Couzens47558792020-12-06 03:16:11 +0100563/*! Destroy/release all NS-VC of given NSE
564 * \param[in] nse NSE
565 */
566void gprs_ns2_free_nsvcs(struct gprs_ns2_nse *nse)
567{
568 struct gprs_ns2_vc *nsvc, *tmp;
569
570 if (!nse)
571 return;
572
573 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
574 gprs_ns2_free_nsvc(nsvc);
575 }
576}
577
Harald Welte5bef2cc2020-09-18 22:33:24 +0200578/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200579struct msgb *gprs_ns2_msgb_alloc(void)
580{
581 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
582 "GPRS/NS");
583 if (!msg) {
584 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
585 NS_ALLOC_SIZE);
586 }
587 return msg;
588}
589
Harald Welte5bef2cc2020-09-18 22:33:24 +0200590/*! Create a status message to be sent over a new connection.
591 * \param[in] orig_msg the original message
592 * \param[in] tp TLVP parsed of the original message
593 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
594 * \param[in] cause Cause for the rejection
595 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200596static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
597{
598 struct msgb *msg = gprs_ns2_msgb_alloc();
599 struct gprs_ns_hdr *nsh;
600 bool have_vci = false;
601 uint8_t _cause = cause;
602 uint16_t nsei = 0;
603
604 if (!msg)
605 return -ENOMEM;
606
Harald Welte798efea2020-12-03 16:01:02 +0100607 if (TLVP_PRES_LEN(tp, NS_IE_NSEI, 2)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200608 nsei = tlvp_val16be(tp, NS_IE_NSEI);
609
610 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
611 nsei, gprs_ns2_cause_str(cause));
612 }
613
614 msg->l2h = msgb_put(msg, sizeof(*nsh));
615 nsh = (struct gprs_ns_hdr *) msg->l2h;
616 nsh->pdu_type = NS_PDUT_STATUS;
617
618 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
Harald Welte798efea2020-12-03 16:01:02 +0100619 have_vci = TLVP_PRES_LEN(tp, NS_IE_VCI, 2);
Alexander Couzens6a161492020-07-12 13:45:50 +0200620
621 /* Section 9.2.7.1: Static conditions for NS-VCI */
622 if (cause == NS_CAUSE_NSVC_BLOCKED ||
623 cause == NS_CAUSE_NSVC_UNKNOWN) {
624 if (!have_vci) {
625 msgb_free(msg);
626 return -EINVAL;
627 }
628
629 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
630 }
631
632 /* Section 9.2.7.2: Static conditions for NS PDU */
633 switch (cause) {
634 case NS_CAUSE_SEM_INCORR_PDU:
635 case NS_CAUSE_PDU_INCOMP_PSTATE:
636 case NS_CAUSE_PROTO_ERR_UNSPEC:
637 case NS_CAUSE_INVAL_ESSENT_IE:
638 case NS_CAUSE_MISSING_ESSENT_IE:
639 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
640 orig_msg->l2h);
641 break;
642 default:
643 break;
644 }
645
646 *reject = msg;
647 return 0;
648}
649
Harald Welte5bef2cc2020-09-18 22:33:24 +0200650/*! Resolve a NS Entity based on its NSEI.
651 * \param[in] nsi NS Instance in which we do the look-up
652 * \param[in] nsei NSEI to look up
653 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200654struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
655{
656 struct gprs_ns2_nse *nse;
657
658 llist_for_each_entry(nse, &nsi->nse, list) {
659 if (nse->nsei == nsei)
660 return nse;
661 }
662
663 return NULL;
664}
665
Harald Welte5bef2cc2020-09-18 22:33:24 +0200666/*! Resolve a NS-VC Entity based on its NS-VCI.
667 * \param[in] nsi NS Instance in which we do the look-up
668 * \param[in] nsvci NS-VCI to look up
669 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200670struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
671{
672 struct gprs_ns2_nse *nse;
673 struct gprs_ns2_vc *nsvc;
674
675 llist_for_each_entry(nse, &nsi->nse, list) {
676 llist_for_each_entry(nsvc, &nse->nsvc, list) {
677 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
678 return nsvc;
679 }
680 }
681
682 return NULL;
683}
684
Harald Welte5bef2cc2020-09-18 22:33:24 +0200685/*! Create a NS Entity within given NS instance.
686 * \param[in] nsi NS instance in which to create NS Entity
687 * \param[in] nsei NS Entity Identifier of to-be-created NSE
688 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzensd923cff2020-12-01 01:03:52 +0100689struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei,
690 enum gprs_ns2_ll linklayer, enum gprs_ns2_dialect dialect)
Alexander Couzens6a161492020-07-12 13:45:50 +0200691{
692 struct gprs_ns2_nse *nse;
Alexander Couzens93ad4992020-12-06 03:03:03 +0100693 char sns[16];
Alexander Couzens6a161492020-07-12 13:45:50 +0200694
695 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
696 if (nse) {
697 LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei);
698 return nse;
699 }
700
701 nse = talloc_zero(nsi, struct gprs_ns2_nse);
702 if (!nse)
703 return NULL;
704
Alexander Couzens93ad4992020-12-06 03:03:03 +0100705 if (dialect == NS2_DIALECT_SNS) {
706 snprintf(sns, sizeof(sns), "NSE%05u-SNS", nsei);
707 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, sns);
708 if (!nse->bss_sns_fi) {
709 talloc_free(nse);
710 return NULL;
711 }
712 }
713
Alexander Couzensd923cff2020-12-01 01:03:52 +0100714 nse->dialect = dialect;
Alexander Couzensaac90162020-11-19 02:44:04 +0100715 nse->ll = linklayer;
Alexander Couzens6a161492020-07-12 13:45:50 +0200716 nse->nsei = nsei;
717 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200718 nse->first = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200719 llist_add(&nse->list, &nsi->nse);
720 INIT_LLIST_HEAD(&nse->nsvc);
721
722 return nse;
723}
724
Alexander Couzens05e7f7d2020-10-11 19:51:46 +0200725/*! Return the NSEI
726 * \param[in] nse NS Entity
727 * \return the nsei.
728 */
729uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse)
730{
731 return nse->nsei;
732}
733
Harald Welte5bef2cc2020-09-18 22:33:24 +0200734/*! Destroy given NS Entity.
735 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200736void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
737{
Alexander Couzens6a161492020-07-12 13:45:50 +0200738 if (!nse)
739 return;
740
Alexander Couzensd1cd6502021-01-15 02:16:23 +0100741 nse->alive = false;
Alexander Couzens47558792020-12-06 03:16:11 +0100742 gprs_ns2_free_nsvcs(nse);
Daniel Willmann15c09a82020-11-03 23:05:43 +0100743 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200744
745 llist_del(&nse->list);
746 if (nse->bss_sns_fi)
747 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
748 talloc_free(nse);
749}
750
Alexander Couzens4b6c8af2020-10-11 20:15:25 +0200751void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi)
752{
753 struct gprs_ns2_nse *nse, *ntmp;
754
755 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
756 gprs_ns2_free_nse(nse);
757 }
758}
759
Alexander Couzens6a161492020-07-12 13:45:50 +0200760static inline int ns2_tlv_parse(struct tlv_parsed *dec,
761 const uint8_t *buf, int buf_len, uint8_t lv_tag,
762 uint8_t lv_tag2)
763{
764 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
765 * See comment of ns_att_tlvdef1. */
766 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
767 if (rc < 0)
768 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
769 return rc;
770}
771
772
Harald Welte5bef2cc2020-09-18 22:33:24 +0200773/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
774 * \param[in] bind the bind through which msg was received
775 * \param[in] msg the actual received message
776 * \param[in] logname A name to describe the VC. E.g. ip address pair
777 * \param[out] reject A message filled to be sent back. Only used in failure cases.
778 * \param[out] success A pointer which will be set to the new VC on success
779 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzens6a161492020-07-12 13:45:50 +0200780enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
781 struct msgb *msg,
782 const char *logname,
783 struct msgb **reject,
784 struct gprs_ns2_vc **success)
785{
786 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
787 struct tlv_parsed tp;
788 struct gprs_ns2_vc *nsvc;
789 struct gprs_ns2_nse *nse;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100790 enum gprs_ns2_dialect dialect;
791 enum gprs_ns2_vc_mode vc_mode;
Alexander Couzens6a161492020-07-12 13:45:50 +0200792 uint16_t nsvci;
793 uint16_t nsei;
794
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100795 int rc, tlv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200796
797 if (msg->len < sizeof(struct gprs_ns_hdr))
798 return GPRS_NS2_CS_ERROR;
799
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100800 /* parse the tlv early to allow reject status msg to
801 * work with valid tp.
802 * Ignore the return code until the pdu type is parsed because
803 * an unknown pdu type should be ignored */
804 tlv = ns2_tlv_parse(&tp, nsh->data,
Alexander Couzensbac5b012020-12-09 23:32:22 +0100805 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Alexander Couzensbac5b012020-12-09 23:32:22 +0100806
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100807 switch (nsh->pdu_type) {
808 case NS_PDUT_STATUS:
Alexander Couzens6a161492020-07-12 13:45:50 +0200809 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
810 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
811 "for non-existing NS-VC\n",
812 logname);
813 return GPRS_NS2_CS_SKIPPED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100814 case NS_PDUT_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200815 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
816 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
817 "for non-existing NS-VC\n",
818 logname);
819 return GPRS_NS2_CS_SKIPPED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100820 case NS_PDUT_RESET_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200821 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
822 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
823 "for non-existing NS-VC\n",
824 logname);
825 return GPRS_NS2_CS_SKIPPED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100826 case NS_PDUT_RESET:
827 /* accept PDU RESET when vc_mode matches */
Alexander Couzensd923cff2020-12-01 01:03:52 +0100828 if (bind->accept_ipaccess) {
829 dialect = NS2_DIALECT_IPACCESS;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100830 break;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100831 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200832
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100833 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100834 if (rc < 0)
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100835 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100836 return GPRS_NS2_CS_REJECTED;
837 default:
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200838 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100839 if (rc < 0)
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200840 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200841 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200842 }
843
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100844 if (tlv < 0) {
845 /* TODO: correct behaviour would checking what's wrong.
846 * If it's an essential TLV for the PDU return NS_CAUSE_INVAL_ESSENT_IE.
847 * Otherwise ignore the non-essential TLV. */
848 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
849 "TLV Parse\n", tlv);
850 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PROTO_ERR_UNSPEC);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100851 if (rc < 0)
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100852 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100853 return GPRS_NS2_CS_REJECTED;
854 }
855
Harald Welte798efea2020-12-03 16:01:02 +0100856 if (!TLVP_PRES_LEN(&tp, NS_IE_CAUSE, 1) ||
857 !TLVP_PRES_LEN(&tp, NS_IE_VCI, 2) || !TLVP_PRES_LEN(&tp, NS_IE_NSEI, 2)) {
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200858 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
859 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100860 if (rc < 0)
861 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200862 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200863 }
864
Alexander Couzens6a161492020-07-12 13:45:50 +0200865 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100866 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
867
868 /* find or create NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +0200869 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
870 if (!nse) {
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100871 /* only create nse for udp & ipaccess */
872 if (bind->ll != GPRS_NS2_LL_UDP || dialect != NS2_DIALECT_IPACCESS)
Alexander Couzens6a161492020-07-12 13:45:50 +0200873 return GPRS_NS2_CS_SKIPPED;
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100874
875 if (!bind->nsi->create_nse || !bind->accept_ipaccess)
876 return GPRS_NS2_CS_SKIPPED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200877
Alexander Couzensd923cff2020-12-01 01:03:52 +0100878 nse = gprs_ns2_create_nse(bind->nsi, nsei, bind->ll, dialect);
Alexander Couzens6a161492020-07-12 13:45:50 +0200879 if (!nse) {
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100880 LOGP(DLNS, LOGL_ERROR, "Failed to create NSE(%05u)\n", nsei);
Alexander Couzens6a161492020-07-12 13:45:50 +0200881 return GPRS_NS2_CS_ERROR;
882 }
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100883 } else {
884 /* nsei already known */
885 if (nse->ll != bind->ll) {
886 LOGP(DLNS, LOGL_ERROR, "Received NS-RESET NS-VCI(%05u) with wrong linklayer(%s) for already known NSE(%05u/%s)\n",
887 nsei, gprs_ns2_lltype_str(bind->ll), nse->nsei, gprs_ns2_lltype_str(nse->ll));
888 return GPRS_NS2_CS_SKIPPED;
889 }
890 }
891
892 nsvc = gprs_ns2_nsvc_by_nsvci(bind->nsi, nsvci);
893 if (nsvc) {
894 if (nsvc->persistent) {
895 LOGP(DLNS, LOGL_ERROR, "Received NS-RESET for a persistent NSE(%05u) NS-VCI(%05u) over wrong connection.\n",
896 nsei, nsvci);
897 return GPRS_NS2_CS_SKIPPED;
898 }
899 /* destroy old dynamic nsvc */
900 gprs_ns2_free_nsvc(nsvc);
901 }
902
903 /* do nse persistent check late to be more precise on the error message */
904 if (nse->persistent) {
905 LOGP(DLNS, LOGL_ERROR, "Received NS-RESET for a persistent NSE(%05u) but the unknown NS-VCI(%05u)\n",
906 nsei, nsvci);
907 return GPRS_NS2_CS_SKIPPED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200908 }
909
Alexander Couzensd923cff2020-12-01 01:03:52 +0100910 vc_mode = gprs_ns2_dialect_to_vc_mode(dialect);
911 nsvc = ns2_vc_alloc(bind, nse, false, vc_mode);
Alexander Couzens6a161492020-07-12 13:45:50 +0200912 if (!nsvc)
913 return GPRS_NS2_CS_SKIPPED;
914
Alexander Couzens6a161492020-07-12 13:45:50 +0200915 nsvc->nsvci = nsvci;
916 nsvc->nsvci_is_valid = true;
917
918 *success = nsvc;
919
920 return GPRS_NS2_CS_CREATED;
921}
922
Harald Welte5bef2cc2020-09-18 22:33:24 +0200923/*! Create, and connect an inactive, new IP-based NS-VC
924 * \param[in] bind bind in which the new NS-VC is to be created
925 * \param[in] remote remote address to which to connect
926 * \param[in] nse NS Entity in which the NS-VC is to be created
927 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
928 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200929struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700930 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200931 struct gprs_ns2_nse *nse,
932 uint16_t nsvci)
933{
934 struct gprs_ns2_vc *nsvc;
935
936 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
937 if (!nsvc)
938 return NULL;
939
940 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
941 nsvc->nsvci = nsvci;
942 nsvc->nsvci_is_valid = true;
943 }
944
945 return nsvc;
946}
947
Harald Welte5bef2cc2020-09-18 22:33:24 +0200948/*! Create, connect and activate a new IP-based NS-VC
949 * \param[in] bind bind in which the new NS-VC is to be created
950 * \param[in] remote remote address to which to connect
951 * \param[in] nse NS Entity in which the NS-VC is to be created
952 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
953 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200954struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700955 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200956 struct gprs_ns2_nse *nse,
957 uint16_t nsvci)
958{
959 struct gprs_ns2_vc *nsvc;
960 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
961 if (!nsvc)
962 return NULL;
963
964 gprs_ns2_vc_fsm_start(nsvc);
965
966 return nsvc;
967}
968
Harald Welte5bef2cc2020-09-18 22:33:24 +0200969/*! Create, connect and activate a new IP-based NS-VC
970 * \param[in] bind bind in which the new NS-VC is to be created
971 * \param[in] remote remote address to which to connect
972 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
973 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
974 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200975struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700976 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200977 uint16_t nsei,
Alexander Couzensd923cff2020-12-01 01:03:52 +0100978 uint16_t nsvci,
979 enum gprs_ns2_dialect dialect)
Alexander Couzens6a161492020-07-12 13:45:50 +0200980{
981 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
982
983 if (!nse) {
Alexander Couzensd923cff2020-12-01 01:03:52 +0100984 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP, dialect);
Alexander Couzens6a161492020-07-12 13:45:50 +0200985 if (!nse)
986 return NULL;
987 }
988
989 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
990}
991
Harald Welte5bef2cc2020-09-18 22:33:24 +0200992/*! Find NS-VC for given socket address.
993 * \param[in] nse NS Entity in which to search
994 * \param[in] sockaddr socket address to search for
995 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +0200996struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700997 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +0200998{
999 struct gprs_ns2_vc *nsvc;
Alexander Couzens9a4cf272020-10-11 20:48:04 +02001000 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +02001001
1002 OSMO_ASSERT(nse);
1003 OSMO_ASSERT(sockaddr);
1004
1005 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +02001006 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +02001007 if (!osmo_sockaddr_cmp(sockaddr, remote))
1008 return nsvc;
1009 }
1010
1011 return NULL;
1012}
1013
Alexander Couzens6cb5d5f2020-10-11 23:23:31 +02001014/*!
1015 * Iterate over all nsvc of a NS Entity and call the callback.
1016 * If the callback returns < 0 it aborts the loop and returns the callback return code.
1017 * \param[in] nse NS Entity to iterate over all nsvcs
1018 * \param[in] cb the callback to call
1019 * \param[inout] cb_data the private data of the callback
1020 * \return 0 if the loop completes. If a callback returns < 0 it will returns this value.
1021 */
1022int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data)
1023{
1024 struct gprs_ns2_vc *nsvc, *tmp;
1025 int rc = 0;
1026 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
1027 rc = cb(nsvc, cb_data);
1028 if (rc < 0)
1029 return rc;
1030 }
1031
1032 return 0;
1033}
1034
1035
Alexander Couzens6a161492020-07-12 13:45:50 +02001036
Harald Welte5bef2cc2020-09-18 22:33:24 +02001037/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +02001038 * \param[in] nsvc NS-VC for which the message was received
1039 * \param msg the received message. Ownership is trasnferred, caller must not free it!
1040 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +02001041int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +02001042 struct msgb *msg)
1043{
1044 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1045 struct tlv_parsed tp;
1046 int rc = 0;
1047
Daniel Willmann751977b2020-12-02 18:59:44 +01001048 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
1049 log_set_context(LOG_CTX_GB_NSVC, nsvc);
1050
Alexander Couzens6a161492020-07-12 13:45:50 +02001051 if (msg->len < sizeof(struct gprs_ns_hdr))
1052 return -EINVAL;
1053
1054 switch (nsh->pdu_type) {
1055 case SNS_PDUT_CONFIG:
1056 /* one additional byte ('end flag') before the TLV part starts */
1057 rc = ns2_tlv_parse(&tp, nsh->data+1,
1058 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
1059 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001060 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001061 return rc;
1062 }
1063 /* All sub-network service related message types */
1064 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
1065 break;
1066 case SNS_PDUT_ACK:
1067 case SNS_PDUT_ADD:
1068 case SNS_PDUT_CHANGE_WEIGHT:
1069 case SNS_PDUT_DELETE:
1070 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +02001071 rc = ns2_tlv_parse(&tp, nsh->data+5,
1072 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001073 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001074 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001075 return rc;
1076 }
1077 tp.lv[NS_IE_NSEI].val = nsh->data+2;
1078 tp.lv[NS_IE_NSEI].len = 2;
1079 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
1080 tp.lv[NS_IE_TRANS_ID].len = 1;
1081 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
1082 break;
1083 case SNS_PDUT_CONFIG_ACK:
1084 case SNS_PDUT_SIZE:
1085 case SNS_PDUT_SIZE_ACK:
1086 rc = ns2_tlv_parse(&tp, nsh->data,
1087 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1088 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001089 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001090 return rc;
1091 }
1092 /* All sub-network service related message types */
1093 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
1094 break;
1095
1096 case NS_PDUT_UNITDATA:
1097 rc = gprs_ns2_vc_rx(nsvc, msg, NULL);
1098 break;
1099 default:
1100 rc = ns2_tlv_parse(&tp, nsh->data,
1101 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1102 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001103 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
Alexander Couzens6a161492020-07-12 13:45:50 +02001104 if (nsh->pdu_type != NS_PDUT_STATUS)
1105 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
1106 return rc;
1107 }
1108 rc = gprs_ns2_vc_rx(nsvc, msg, &tp);
1109 break;
1110 }
1111
1112 return rc;
1113}
1114
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001115/* summarize all active data nsvcs */
1116void ns2_nse_data_sum(struct gprs_ns2_nse *nse)
1117{
1118 struct gprs_ns2_vc *nsvc;
Alexander Couzens8a2a1a42020-12-26 18:00:17 +01001119 nse->nsvc_count = 0;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001120
1121 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1122 if (!gprs_ns2_vc_is_unblocked(nsvc))
1123 continue;
Alexander Couzens8a2a1a42020-12-26 18:00:17 +01001124
1125 nse->nsvc_count++;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001126 }
1127}
1128
Harald Welte5bef2cc2020-09-18 22:33:24 +02001129/*! Notify a nse about the change of a NS-VC.
1130 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
1131 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +02001132void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
1133{
1134 struct gprs_ns2_nse *nse = nsvc->nse;
1135 struct gprs_ns2_vc *tmp;
1136
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001137 ns2_nse_data_sum(nse);
1138
Alexander Couzens6a161492020-07-12 13:45:50 +02001139 if (unblocked == nse->alive)
1140 return;
1141
1142 if (unblocked) {
1143 /* this is the first unblocked NSVC on an unavailable NSE */
1144 nse->alive = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +01001145 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +02001146 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +02001147 return;
1148 }
1149
1150 /* check if there are any remaining alive vcs */
1151 llist_for_each_entry(tmp, &nse->nsvc, list) {
1152 if (tmp == nsvc)
1153 continue;
1154
1155 if (gprs_ns2_vc_is_unblocked(tmp)) {
1156 /* there is at least one remaining alive NSVC */
1157 return;
1158 }
1159 }
1160
1161 /* nse became unavailable */
1162 nse->alive = false;
Daniel Willmann15c09a82020-11-03 23:05:43 +01001163 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001164}
1165
1166/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +02001167 * \param[in] ctx a talloc context to allocate NS instance from
Alexander Couzenscce88282020-10-26 00:25:50 +01001168 * \param[in] cb Call-back function for dispatching primitives to the user. The Call-back must free all msgb* given in the primitive.
Harald Welte5bef2cc2020-09-18 22:33:24 +02001169 * \param[in] cb_data transparent user data passed to Call-back
1170 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001171struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
1172{
1173 struct gprs_ns2_inst *nsi;
1174
1175 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
1176 if (!nsi)
1177 return NULL;
1178
1179 nsi->cb = cb;
1180 nsi->cb_data = cb_data;
1181 INIT_LLIST_HEAD(&nsi->binding);
1182 INIT_LLIST_HEAD(&nsi->nse);
1183
1184 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1185 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1186 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1187 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1188 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1189 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1190 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
1191 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
Alexander Couzens90ee9632020-12-07 06:18:32 +01001192 nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES] = 3;
1193 nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES] = 3;
Alexander Couzens6a161492020-07-12 13:45:50 +02001194
1195 return nsi;
1196}
1197
Harald Welte5bef2cc2020-09-18 22:33:24 +02001198/*! Destroy a NS Instance (including all its NSEs, binds, ...).
1199 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001200void gprs_ns2_free(struct gprs_ns2_inst *nsi)
1201{
Alexander Couzens6a161492020-07-12 13:45:50 +02001202 if (!nsi)
1203 return;
1204
Alexander Couzens4b6c8af2020-10-11 20:15:25 +02001205 gprs_ns2_free_nses(nsi);
Alexander Couzens896fcd52020-10-11 19:52:36 +02001206 gprs_ns2_free_binds(nsi);
Alexander Couzens35315042020-10-10 03:28:17 +02001207
1208 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001209}
1210
Harald Welte5bef2cc2020-09-18 22:33:24 +02001211/*! Configure whether a NS Instance should dynamically create NSEs based on incoming traffic.
1212 * \param nsi the instance to modify
1213 * \param create_nse if NSE can be created on receiving package. SGSN set this.
1214 * \return 0 on success; negative on error
Alexander Couzens6a161492020-07-12 13:45:50 +02001215 */
1216int gprs_ns2_dynamic_create_nse(struct gprs_ns2_inst *nsi, bool create_nse)
1217{
1218 nsi->create_nse = create_nse;
1219
1220 return 0;
1221}
1222
Harald Welte5bef2cc2020-09-18 22:33:24 +02001223/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1224 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001225void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1226{
1227 struct gprs_ns2_vc *nsvc;
1228 OSMO_ASSERT(nse);
1229
1230 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1231 if (nsvc->sns_only)
1232 continue;
1233
1234 gprs_ns2_vc_fsm_start(nsvc);
1235 }
1236}
1237
Harald Welte5bef2cc2020-09-18 22:33:24 +02001238/*! Destroy a given bind.
1239 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001240void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1241{
1242 struct gprs_ns2_vc *nsvc, *tmp;
1243 if (!bind)
1244 return;
1245
1246 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1247 gprs_ns2_free_nsvc(nsvc);
1248 }
1249
1250 if (bind->driver->free_bind)
1251 bind->driver->free_bind(bind);
1252
1253 llist_del(&bind->list);
Alexander Couzensaaa55a62020-12-03 06:02:03 +01001254 talloc_free((char *)bind->name);
Alexander Couzens6a161492020-07-12 13:45:50 +02001255 talloc_free(bind);
1256}
Alexander Couzens896fcd52020-10-11 19:52:36 +02001257
1258void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi)
1259{
1260 struct gprs_ns2_vc_bind *bind, *tbind;
1261
1262 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1263 gprs_ns2_free_bind(bind);
1264 }
1265}
Alexander Couzensd923cff2020-12-01 01:03:52 +01001266
Alexander Couzensaaa55a62020-12-03 06:02:03 +01001267/*! Search for a bind with a unique name
1268 * \param[in] nsi NS instance on which we operate
1269 * \param[in] name The unique bind name to search for
1270 * \return the bind or NULL if not found
1271 */
1272struct gprs_ns2_vc_bind *gprs_ns2_bind_by_name(
1273 struct gprs_ns2_inst *nsi, const char *name)
1274{
1275 struct gprs_ns2_vc_bind *bind;
1276
1277 llist_for_each_entry(bind, &nsi->binding, list) {
1278 if (!strcmp(bind->name, name))
1279 return bind;
1280 }
1281
1282 return NULL;
1283}
1284
Alexander Couzensd923cff2020-12-01 01:03:52 +01001285enum gprs_ns2_vc_mode gprs_ns2_dialect_to_vc_mode(
1286 enum gprs_ns2_dialect dialect)
1287{
1288 switch (dialect) {
1289 case NS2_DIALECT_SNS:
1290 case NS2_DIALECT_STATIC_ALIVE:
1291 return NS2_VC_MODE_ALIVE;
1292 case NS2_DIALECT_STATIC_RESETBLOCK:
1293 case NS2_DIALECT_IPACCESS:
1294 return NS2_VC_MODE_BLOCKRESET;
1295 default:
1296 return -1;
1297 }
1298}
1299
Alexander Couzens1c8785d2020-12-17 06:58:53 +01001300static void add_bind_array(struct gprs_ns2_vc_bind **array,
1301 struct gprs_ns2_vc_bind *bind, int size)
1302{
1303 int i;
1304 for (i=0; i < size; i++) {
1305 if (array[i] == bind)
1306 return;
1307 if (!array[i])
1308 break;
1309 }
1310
1311 if (i == size)
1312 return;
1313
1314 array[i] = bind;
1315}
1316
1317/*! calculate the transfer capabilities for a nse
1318 * \param nse the nse to count the transfer capability
1319 * \param bvci a bvci - unused
1320 * \return the transfer capability in mbit. On error < 0.
1321 */
1322int ns2_count_transfer_cap(struct gprs_ns2_nse *nse,
1323 uint16_t bvci)
1324{
1325 struct gprs_ns2_vc *nsvc;
1326 struct gprs_ns2_vc_bind **active_binds;
1327 int i, active_nsvcs = 0, transfer_cap = 0;
1328
1329 /* calculate the transfer capabilities based on the binds.
1330 * A bind has a transfer capability which is shared across all NSVCs.
1331 * Take care the bind cap is not counted twice within a NSE.
1332 * This should be accurate for FR and UDP but not for FR/GRE. */
1333
1334 if (!nse->alive)
1335 return 0;
1336
1337 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1338 if (gprs_ns2_vc_is_unblocked(nsvc))
1339 active_nsvcs++;
1340 }
1341 /* an alive nse should always have active_nsvcs */
1342 OSMO_ASSERT(active_nsvcs);
1343
1344 active_binds = talloc_zero_array(nse, struct gprs_ns2_vc_bind*, active_nsvcs);
1345 if (!active_binds)
1346 return -ENOMEM;
1347
1348 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1349 if (!gprs_ns2_vc_is_unblocked(nsvc))
1350 continue;
1351 add_bind_array(active_binds, nsvc->bind, active_nsvcs);
1352 }
1353
1354 /* TODO: change calcuation for FR/GRE */
1355 for (i = 0; i < active_nsvcs; i++) {
1356 if (active_binds[i])
1357 transfer_cap += active_binds[i]->transfer_capability;
1358 }
1359
1360 talloc_free(active_binds);
1361 return transfer_cap;
1362}
1363
Alexander Couzens6a161492020-07-12 13:45:50 +02001364/*! @} */