blob: 1f7525e0d74b58fb5428d94d6f4d2602773ed8f9 [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[] = {
Harald Welte3d5eaee2021-01-30 21:33:02 +0100163 [NS_CTR_PKTS_IN] = { "packets:in", "Packets at NS Level ( In)" },
164 [NS_CTR_PKTS_OUT] = { "packets:out", "Packets at NS Level (Out)" },
Harald Weltef22ae5a2021-01-30 22:01:28 +0100165 [NS_CTR_PKTS_OUT_DROP] = { "packets:out:drop", "Dropped Packets (Out)" },
Harald Welte3d5eaee2021-01-30 21:33:02 +0100166 [NS_CTR_BYTES_IN] = { "bytes:in", "Bytes at NS Level ( In)" },
167 [NS_CTR_BYTES_OUT] = { "bytes:out", "Bytes at NS Level (Out)" },
Harald Weltef22ae5a2021-01-30 22:01:28 +0100168 [NS_CTR_BYTES_OUT_DROP] = { "bytes:out:drop", "Dropped Bytes (Out)" },
Harald Welte3d5eaee2021-01-30 21:33:02 +0100169 [NS_CTR_BLOCKED] = { "blocked", "NS-VC Block count " },
Harald Welteb40bf8b2021-01-30 22:43:01 +0100170 [NS_CTR_UNBLOCKED] = { "unblocked", "NS-VC Unblock count " },
Harald Welte3d5eaee2021-01-30 21:33:02 +0100171 [NS_CTR_DEAD] = { "dead", "NS-VC gone dead count " },
172 [NS_CTR_REPLACED] = { "replaced", "NS-VC replaced other count" },
173 [NS_CTR_NSEI_CHG] = { "nsei-chg", "NS-VC changed NSEI count " },
174 [NS_CTR_INV_VCI] = { "inv-nsvci", "NS-VCI was invalid count " },
175 [NS_CTR_INV_NSEI] = { "inv-nsei", "NSEI was invalid count " },
176 [NS_CTR_LOST_ALIVE] = { "lost:alive", "ALIVE ACK missing count " },
177 [NS_CTR_LOST_RESET] = { "lost:reset", "RESET ACK missing count " },
Alexander Couzens6a161492020-07-12 13:45:50 +0200178};
179
180static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
181 .group_name_prefix = "ns:nsvc",
182 .group_description = "NSVC Peer Statistics",
183 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
184 .ctr_desc = nsvc_ctr_description,
185 .class_id = OSMO_STATS_CLASS_PEER,
186};
187
188
189static const struct osmo_stat_item_desc nsvc_stat_description[] = {
Harald Welte3d5eaee2021-01-30 21:33:02 +0100190 [NS_STAT_ALIVE_DELAY] = { "alive.delay", "ALIVE response time ", "ms", 16, 0 },
Alexander Couzens6a161492020-07-12 13:45:50 +0200191};
192
193static const struct osmo_stat_item_group_desc nsvc_statg_desc = {
194 .group_name_prefix = "ns.nsvc",
195 .group_description = "NSVC Peer Statistics",
196 .num_items = ARRAY_SIZE(nsvc_stat_description),
197 .item_desc = nsvc_stat_description,
198 .class_id = OSMO_STATS_CLASS_PEER,
199};
200
Harald Welte76346072021-01-31 11:54:02 +0100201const struct osmo_stat_item_desc nsbind_stat_description[] = {
202 [NS2_BIND_STAT_BACKLOG_LEN] = { "tx_backlog_length", "Transmit backlog length", "packets", 16, 0 },
203};
204
205static const struct osmo_stat_item_group_desc nsbind_statg_desc = {
206 .group_name_prefix = "ns.bind",
207 .group_description = "NS Bind Statistics",
208 .num_items = ARRAY_SIZE(nsbind_stat_description),
209 .item_desc = nsbind_stat_description,
210 .class_id = OSMO_STATS_CLASS_PEER,
211};
212
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100213const struct value_string gprs_ns2_aff_cause_prim_strs[] = {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100214 { GPRS_NS2_AFF_CAUSE_VC_FAILURE, "NSVC failure" },
215 { GPRS_NS2_AFF_CAUSE_VC_RECOVERY, "NSVC recovery" },
216 { GPRS_NS2_AFF_CAUSE_FAILURE, "NSE failure" },
217 { GPRS_NS2_AFF_CAUSE_RECOVERY, "NSE recovery" },
218 { GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED, "NSE SNS configured" },
219 { GPRS_NS2_AFF_CAUSE_SNS_FAILURE, "NSE SNS failure" },
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100220 { 0, NULL }
221};
222
Alexander Couzens0ab028c2020-11-04 02:41:44 +0100223const struct value_string gprs_ns2_prim_strs[] = {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100224 { GPRS_NS2_PRIM_UNIT_DATA, "UNIT DATA" },
225 { GPRS_NS2_PRIM_CONGESTION, "CONGESTION" },
226 { GPRS_NS2_PRIM_STATUS, "STATUS" },
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100227 { 0, NULL }
228};
229
Harald Weltea24e7ee2020-11-29 17:38:48 +0100230const struct value_string gprs_ns2_lltype_strs[] = {
231 { GPRS_NS2_LL_UDP, "UDP" },
232 { GPRS_NS2_LL_FR_GRE, "FR_GRE" },
233 { GPRS_NS2_LL_FR, "FR" },
234 { 0, NULL }
235};
236
Harald Welte5bef2cc2020-09-18 22:33:24 +0200237/*! string-format a given NS-VC into a user-supplied buffer.
238 * \param[in] buf user-allocated output buffer
239 * \param[in] buf_len size of user-allocated output buffer in bytes
240 * \param[in] nsvc NS-VC to be string-formatted
241 * \return pointer to buf on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200242char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc)
243{
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200244 const struct osmo_sockaddr *local;
245 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200246 struct osmo_sockaddr_str local_str;
247 struct osmo_sockaddr_str remote_str;
248
249 if (!buf_len)
Harald Welte92ad0292020-09-18 22:34:24 +0200250 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200251
Alexander Couzensaac90162020-11-19 02:44:04 +0100252 switch (nsvc->nse->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100253 case GPRS_NS2_LL_UDP:
Alexander Couzens6a161492020-07-12 13:45:50 +0200254 if (!gprs_ns2_is_ip_bind(nsvc->bind)) {
255 buf[0] = '\0';
256 return buf;
257 }
258
259 local = gprs_ns2_ip_bind_sockaddr(nsvc->bind);
Alexander Couzensc4229a42020-10-11 20:58:04 +0200260 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200261 if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas))
262 strcpy(local_str.ip, "invalid");
263 if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas))
264 strcpy(remote_str.ip, "invalid");
265
266 if (nsvc->nsvci_is_valid)
267 snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u",
268 local_str.ip, local_str.port,
269 nsvc->nsvci,
270 remote_str.ip, remote_str.port);
271 else
272 snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u",
273 local_str.ip, local_str.port,
274 remote_str.ip, remote_str.port);
275 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100276 case GPRS_NS2_LL_FR_GRE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200277 snprintf(buf, buf_len, "frgre)");
278 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100279 case GPRS_NS2_LL_FR:
Alexander Couzens841817e2020-11-19 00:41:29 +0100280 snprintf(buf, buf_len, "fr)netif: %s dlci: %u", gprs_ns2_fr_bind_netif(nsvc->bind),
281 gprs_ns2_fr_nsvc_dlci(nsvc));
282 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200283 default:
Harald Welte6188e002020-12-01 17:20:07 +0100284 snprintf(buf, buf_len, "unknown)");
Alexander Couzens6a161492020-07-12 13:45:50 +0200285 break;
286 }
287
288 buf[buf_len - 1] = '\0';
289
290 return buf;
291}
292
293/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
294#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
295
Harald Welte5bef2cc2020-09-18 22:33:24 +0200296/*! string-format a given NS-VC to a thread-local static buffer.
297 * \param[in] nsvc NS-VC to be string-formatted
298 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200299const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
300{
301 static __thread char buf[NS2_LL_MAX_STR];
302 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
303}
304
Harald Welte5bef2cc2020-09-18 22:33:24 +0200305/*! string-format a given NS-VC to a dynamically allocated string.
306 * \param[in] ctx talloc context from which to allocate
307 * \param[in] nsvc NS-VC to be string-formatted
308 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200309char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
310{
311 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
312 if (!buf)
313 return buf;
314 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
315}
316
Daniel Willmannf1286542020-11-03 23:03:33 +0100317/*! Return the current state name of a given NS-VC to a thread-local static buffer.
318 * \param[in] nsvc NS-VC to return the state of
319 * \return pointer to the string on success; NULL on error */
320const char *gprs_ns2_nsvc_state_name(struct gprs_ns2_vc *nsvc)
321{
322 return osmo_fsm_inst_state_name(nsvc->fi);
323}
324
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100325/* select a signalling NSVC and respect sig_counter
326 * param[out] reset_counter - all counter has to be resetted to their signal weight
327 * return the chosen nsvc or NULL
328 */
329static struct gprs_ns2_vc *ns2_load_sharing_signal(struct gprs_ns2_nse *nse)
330{
331 struct gprs_ns2_vc *nsvc = NULL, *last = NULL, *tmp;
332
333 llist_for_each_entry(tmp, &nse->nsvc, list) {
334 if (tmp->sig_weight == 0)
335 continue;
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100336 if (!ns2_vc_is_unblocked(tmp))
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100337 continue;
338 if (tmp->sig_counter == 0) {
339 last = tmp;
340 continue;
341 }
342
343 tmp->sig_counter--;
344 nsvc = tmp;
345 break;
346 }
347
348 /* all counter were zero, but there are valid nsvc */
349 if (!nsvc && last) {
350 llist_for_each_entry(tmp, &nse->nsvc, list) {
351 tmp->sig_counter = tmp->sig_weight;
352 }
353
354 last->sig_counter--;
355 return last;
356 } else {
357 return nsvc;
358 }
359}
360
361/* 4.4.1 Load Sharing function for the Frame Relay Sub-Network */
362static struct gprs_ns2_vc *ns2_load_sharing_modulor(
363 struct gprs_ns2_nse *nse,
364 uint16_t bvci,
365 uint32_t load_selector)
366{
367 struct gprs_ns2_vc *tmp;
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100368 uint32_t mod = (bvci + load_selector) % nse->nsvc_count;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100369 uint32_t i = 0;
370
371 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100372 if (!ns2_vc_is_unblocked(tmp))
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100373 continue;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100374 if (i == mod)
375 return tmp;
376 i++;
377 }
378
379 return NULL;
380}
381
382/* pick the first available data NSVC - no load sharing */
383struct gprs_ns2_vc *ns2_load_sharing_first(struct gprs_ns2_nse *nse)
384{
385 struct gprs_ns2_vc *nsvc = NULL, *tmp;
386
387 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100388 if (!ns2_vc_is_unblocked(tmp))
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100389 continue;
390 if (tmp->data_weight == 0)
391 continue;
392
393 nsvc = tmp;
394 break;
395 }
396
397 return nsvc;
398}
399
400
401static struct gprs_ns2_vc *ns2_load_sharing(
402 struct gprs_ns2_nse *nse,
403 uint16_t bvci,
404 uint32_t link_selector)
405{
406 struct gprs_ns2_vc *nsvc = NULL;
407
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100408 switch (nse->ll) {
409 case GPRS_NS2_LL_FR:
410 nsvc = ns2_load_sharing_modulor(nse, bvci, link_selector);
411 break;
412 case GPRS_NS2_LL_UDP:
413 default:
414 if (bvci == 0) {
415 /* signalling */
416 nsvc = ns2_load_sharing_signal(nse);
417 } else {
418 /* data with load sharing parameter */
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100419 nsvc = ns2_load_sharing_first(nse);
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100420 }
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100421 break;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100422 }
423
424 return nsvc;
425}
426
Harald Welte5bef2cc2020-09-18 22:33:24 +0200427/*! Receive a primitive from the NS User (Gb).
428 * \param[in] nsi NS instance to which the primitive is issued
429 * \param[in] oph The primitive
430 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200431int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
432{
Alexander Couzens6a161492020-07-12 13:45:50 +0200433 /* TODO: implement resource distribution */
434 /* TODO: check for empty PDUs which can be sent to Request/Confirm
435 * the IP endpoint */
436 struct osmo_gprs_ns2_prim *nsp;
437 struct gprs_ns2_nse *nse = NULL;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100438 struct gprs_ns2_vc *nsvc = NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200439 uint16_t bvci, nsei;
440 uint8_t sducontrol = 0;
441
442 if (oph->sap != SAP_NS)
443 return -EINVAL;
444
445 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
446
Alexander Couzens138b96f2021-01-25 16:23:29 +0100447 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != GPRS_NS2_PRIM_UNIT_DATA)
Alexander Couzens6a161492020-07-12 13:45:50 +0200448 return -EINVAL;
449
450 if (!oph->msg)
451 return -EINVAL;
452
453 bvci = nsp->bvci;
454 nsei = nsp->nsei;
455
456 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
457 if (!nse)
458 return -EINVAL;
459
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100460 nsvc = ns2_load_sharing(nse, bvci, nsp->u.unitdata.link_selector);
Alexander Couzens6a161492020-07-12 13:45:50 +0200461
462 /* TODO: send a status primitive back */
463 if (!nsvc)
464 return 0;
465
Alexander Couzens138b96f2021-01-25 16:23:29 +0100466 if (nsp->u.unitdata.change == GPRS_NS2_ENDPOINT_REQUEST_CHANGE)
Alexander Couzens6a161492020-07-12 13:45:50 +0200467 sducontrol = 1;
Alexander Couzens138b96f2021-01-25 16:23:29 +0100468 else if (nsp->u.unitdata.change == GPRS_NS2_ENDPOINT_CONFIRM_CHANGE)
Alexander Couzens6a161492020-07-12 13:45:50 +0200469 sducontrol = 2;
470
471 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
472}
473
Harald Welte5bef2cc2020-09-18 22:33:24 +0200474/*! Send a STATUS.ind primitive to the specified NS instance user.
475 * \param[in] nsi NS instance on which we operate
476 * \param[in] nsei NSEI to which the statue relates
477 * \param[in] bvci BVCI to which the status relates
478 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200479void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100480 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200481 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200482 enum gprs_ns2_affecting_cause cause)
483{
Daniel Willmann15c09a82020-11-03 23:05:43 +0100484 char nsvc_str[NS2_LL_MAX_STR];
Alexander Couzens6a161492020-07-12 13:45:50 +0200485 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200486 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200487 nsp.bvci = bvci;
488 nsp.u.status.cause = cause;
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100489 nsp.u.status.transfer = ns2_count_transfer_cap(nse, bvci);
Alexander Couzensda0a2852020-10-01 23:24:07 +0200490 nsp.u.status.first = nse->first;
491 nsp.u.status.persistent = nse->persistent;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100492 if (nsvc)
493 nsp.u.status.nsvc = gprs_ns2_ll_str_buf(nsvc_str, sizeof(nsvc_str), nsvc);
494
Alexander Couzens138b96f2021-01-25 16:23:29 +0100495 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_STATUS,
Alexander Couzens6a161492020-07-12 13:45:50 +0200496 PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200497 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200498}
499
Harald Welte5bef2cc2020-09-18 22:33:24 +0200500/*! Allocate a NS-VC within the given bind + NSE.
501 * \param[in] bind The 'bind' on which we operate
502 * \param[in] nse The NS Entity on which we operate
503 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
Harald Welte603f4042020-11-29 17:39:19 +0100504 * \param[in] id - human-readable identifier
Harald Welte5bef2cc2020-09-18 22:33:24 +0200505 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzensd923cff2020-12-01 01:03:52 +0100506struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater,
Harald Welte603f4042020-11-29 17:39:19 +0100507 enum gprs_ns2_vc_mode vc_mode, const char *id)
Alexander Couzens6a161492020-07-12 13:45:50 +0200508{
Daniel Willmannbb899052021-01-16 14:02:45 +0100509 /* Sanity check */
510 OSMO_ASSERT(bind->ll == nse->ll);
511
Alexander Couzens6a161492020-07-12 13:45:50 +0200512 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
513
514 if (!nsvc)
515 return NULL;
516
517 nsvc->bind = bind;
518 nsvc->nse = nse;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100519 nsvc->mode = vc_mode;
Alexander Couzens6a161492020-07-12 13:45:50 +0200520 nsvc->sig_weight = 1;
521 nsvc->data_weight = 1;
522
Harald Welte97ccbf72021-01-31 11:49:19 +0100523 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->nsvc_rate_ctr_idx);
Alexander Couzens6a161492020-07-12 13:45:50 +0200524 if (!nsvc->ctrg) {
525 goto err;
526 }
Harald Welte97ccbf72021-01-31 11:49:19 +0100527 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->nsvc_rate_ctr_idx);
Alexander Couzens6a161492020-07-12 13:45:50 +0200528 if (!nsvc->statg)
529 goto err_group;
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100530 if (!ns2_vc_fsm_alloc(nsvc, id, initiater))
Alexander Couzens6a161492020-07-12 13:45:50 +0200531 goto err_statg;
532
Harald Welte97ccbf72021-01-31 11:49:19 +0100533 bind->nsi->nsvc_rate_ctr_idx++;
Alexander Couzens6a161492020-07-12 13:45:50 +0200534
535 llist_add(&nsvc->list, &nse->nsvc);
536 llist_add(&nsvc->blist, &bind->nsvc);
537
538 return nsvc;
539
540err_statg:
541 osmo_stat_item_group_free(nsvc->statg);
542err_group:
543 rate_ctr_group_free(nsvc->ctrg);
544err:
545 talloc_free(nsvc);
546
547 return NULL;
548}
549
Harald Welte5bef2cc2020-09-18 22:33:24 +0200550/*! Destroy/release given NS-VC.
551 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200552void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
553{
554 if (!nsvc)
555 return;
556
Alexander Couzens138b96f2021-01-25 16:23:29 +0100557 ns2_prim_status_ind(nsvc->nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200558
559 llist_del(&nsvc->list);
560 llist_del(&nsvc->blist);
561
562 /* notify nse this nsvc is unavailable */
563 ns2_nse_notify_unblocked(nsvc, false);
564
565 /* check if sns is using this VC */
566 ns2_sns_free_nsvc(nsvc);
567 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
568
569 /* let the driver/bind clean up it's internal state */
570 if (nsvc->priv && nsvc->bind->free_vc)
571 nsvc->bind->free_vc(nsvc);
572
573 osmo_stat_item_group_free(nsvc->statg);
574 rate_ctr_group_free(nsvc->ctrg);
575
576 talloc_free(nsvc);
577}
578
Alexander Couzens47558792020-12-06 03:16:11 +0100579/*! Destroy/release all NS-VC of given NSE
580 * \param[in] nse NSE
581 */
582void gprs_ns2_free_nsvcs(struct gprs_ns2_nse *nse)
583{
584 struct gprs_ns2_vc *nsvc, *tmp;
585
586 if (!nse)
587 return;
588
589 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
590 gprs_ns2_free_nsvc(nsvc);
591 }
592}
593
Harald Welte5bef2cc2020-09-18 22:33:24 +0200594/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100595struct msgb *ns2_msgb_alloc(void)
Alexander Couzens6a161492020-07-12 13:45:50 +0200596{
597 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
598 "GPRS/NS");
599 if (!msg) {
600 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
601 NS_ALLOC_SIZE);
602 }
603 return msg;
604}
605
Harald Welte5bef2cc2020-09-18 22:33:24 +0200606/*! Create a status message to be sent over a new connection.
607 * \param[in] orig_msg the original message
608 * \param[in] tp TLVP parsed of the original message
609 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
610 * \param[in] cause Cause for the rejection
611 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200612static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
613{
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100614 struct msgb *msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200615 struct gprs_ns_hdr *nsh;
616 bool have_vci = false;
617 uint8_t _cause = cause;
618 uint16_t nsei = 0;
619
620 if (!msg)
621 return -ENOMEM;
622
Harald Welte798efea2020-12-03 16:01:02 +0100623 if (TLVP_PRES_LEN(tp, NS_IE_NSEI, 2)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200624 nsei = tlvp_val16be(tp, NS_IE_NSEI);
625
626 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
627 nsei, gprs_ns2_cause_str(cause));
628 }
629
630 msg->l2h = msgb_put(msg, sizeof(*nsh));
631 nsh = (struct gprs_ns_hdr *) msg->l2h;
632 nsh->pdu_type = NS_PDUT_STATUS;
633
634 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
Harald Welte798efea2020-12-03 16:01:02 +0100635 have_vci = TLVP_PRES_LEN(tp, NS_IE_VCI, 2);
Alexander Couzens6a161492020-07-12 13:45:50 +0200636
637 /* Section 9.2.7.1: Static conditions for NS-VCI */
638 if (cause == NS_CAUSE_NSVC_BLOCKED ||
639 cause == NS_CAUSE_NSVC_UNKNOWN) {
640 if (!have_vci) {
641 msgb_free(msg);
642 return -EINVAL;
643 }
644
645 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
646 }
647
648 /* Section 9.2.7.2: Static conditions for NS PDU */
649 switch (cause) {
650 case NS_CAUSE_SEM_INCORR_PDU:
651 case NS_CAUSE_PDU_INCOMP_PSTATE:
652 case NS_CAUSE_PROTO_ERR_UNSPEC:
653 case NS_CAUSE_INVAL_ESSENT_IE:
654 case NS_CAUSE_MISSING_ESSENT_IE:
655 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
656 orig_msg->l2h);
657 break;
658 default:
659 break;
660 }
661
662 *reject = msg;
663 return 0;
664}
665
Harald Welte5bef2cc2020-09-18 22:33:24 +0200666/*! Resolve a NS Entity based on its NSEI.
667 * \param[in] nsi NS Instance in which we do the look-up
668 * \param[in] nsei NSEI to look up
669 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200670struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
671{
672 struct gprs_ns2_nse *nse;
673
674 llist_for_each_entry(nse, &nsi->nse, list) {
675 if (nse->nsei == nsei)
676 return nse;
677 }
678
679 return NULL;
680}
681
Harald Welte5bef2cc2020-09-18 22:33:24 +0200682/*! Resolve a NS-VC Entity based on its NS-VCI.
683 * \param[in] nsi NS Instance in which we do the look-up
684 * \param[in] nsvci NS-VCI to look up
685 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200686struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
687{
688 struct gprs_ns2_nse *nse;
689 struct gprs_ns2_vc *nsvc;
690
691 llist_for_each_entry(nse, &nsi->nse, list) {
692 llist_for_each_entry(nsvc, &nse->nsvc, list) {
693 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
694 return nsvc;
695 }
696 }
697
698 return NULL;
699}
700
Harald Welte5bef2cc2020-09-18 22:33:24 +0200701/*! Create a NS Entity within given NS instance.
702 * \param[in] nsi NS instance in which to create NS Entity
703 * \param[in] nsei NS Entity Identifier of to-be-created NSE
704 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzensd923cff2020-12-01 01:03:52 +0100705struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei,
706 enum gprs_ns2_ll linklayer, enum gprs_ns2_dialect dialect)
Alexander Couzens6a161492020-07-12 13:45:50 +0200707{
708 struct gprs_ns2_nse *nse;
Alexander Couzens93ad4992020-12-06 03:03:03 +0100709 char sns[16];
Alexander Couzens6a161492020-07-12 13:45:50 +0200710
711 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
712 if (nse) {
Harald Weltef2949742021-01-20 14:54:14 +0100713 LOGNSE(nse, LOGL_ERROR, "Can not create a NSE with already taken NSEI\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200714 return nse;
715 }
716
717 nse = talloc_zero(nsi, struct gprs_ns2_nse);
718 if (!nse)
719 return NULL;
720
Alexander Couzens138b96f2021-01-25 16:23:29 +0100721 if (dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens93ad4992020-12-06 03:03:03 +0100722 snprintf(sns, sizeof(sns), "NSE%05u-SNS", nsei);
723 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, sns);
724 if (!nse->bss_sns_fi) {
725 talloc_free(nse);
726 return NULL;
727 }
728 }
729
Alexander Couzensd923cff2020-12-01 01:03:52 +0100730 nse->dialect = dialect;
Alexander Couzensaac90162020-11-19 02:44:04 +0100731 nse->ll = linklayer;
Alexander Couzens6a161492020-07-12 13:45:50 +0200732 nse->nsei = nsei;
733 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200734 nse->first = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200735 llist_add(&nse->list, &nsi->nse);
736 INIT_LLIST_HEAD(&nse->nsvc);
737
738 return nse;
739}
740
Alexander Couzens05e7f7d2020-10-11 19:51:46 +0200741/*! Return the NSEI
742 * \param[in] nse NS Entity
743 * \return the nsei.
744 */
745uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse)
746{
747 return nse->nsei;
748}
749
Harald Welte5bef2cc2020-09-18 22:33:24 +0200750/*! Destroy given NS Entity.
751 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200752void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
753{
Alexander Couzens6a161492020-07-12 13:45:50 +0200754 if (!nse)
755 return;
756
Alexander Couzensd1cd6502021-01-15 02:16:23 +0100757 nse->alive = false;
Alexander Couzens47558792020-12-06 03:16:11 +0100758 gprs_ns2_free_nsvcs(nse);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100759 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200760
761 llist_del(&nse->list);
762 if (nse->bss_sns_fi)
763 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
764 talloc_free(nse);
765}
766
Alexander Couzens4b6c8af2020-10-11 20:15:25 +0200767void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi)
768{
769 struct gprs_ns2_nse *nse, *ntmp;
770
771 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
772 gprs_ns2_free_nse(nse);
773 }
774}
775
Alexander Couzens6a161492020-07-12 13:45:50 +0200776static inline int ns2_tlv_parse(struct tlv_parsed *dec,
777 const uint8_t *buf, int buf_len, uint8_t lv_tag,
778 uint8_t lv_tag2)
779{
780 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
781 * See comment of ns_att_tlvdef1. */
782 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
783 if (rc < 0)
784 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
785 return rc;
786}
787
788
Harald Welte5bef2cc2020-09-18 22:33:24 +0200789/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
790 * \param[in] bind the bind through which msg was received
791 * \param[in] msg the actual received message
792 * \param[in] logname A name to describe the VC. E.g. ip address pair
793 * \param[out] reject A message filled to be sent back. Only used in failure cases.
794 * \param[out] success A pointer which will be set to the new VC on success
795 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzensba5a9922021-01-25 16:03:23 +0100796enum ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
797 struct msgb *msg,
798 const char *logname,
799 struct msgb **reject,
800 struct gprs_ns2_vc **success)
Alexander Couzens6a161492020-07-12 13:45:50 +0200801{
802 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
803 struct tlv_parsed tp;
804 struct gprs_ns2_vc *nsvc;
805 struct gprs_ns2_nse *nse;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100806 enum gprs_ns2_dialect dialect;
807 enum gprs_ns2_vc_mode vc_mode;
Alexander Couzens6a161492020-07-12 13:45:50 +0200808 uint16_t nsvci;
809 uint16_t nsei;
Harald Welte603f4042020-11-29 17:39:19 +0100810 char idbuf[32];
Alexander Couzens6a161492020-07-12 13:45:50 +0200811
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100812 int rc, tlv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200813
814 if (msg->len < sizeof(struct gprs_ns_hdr))
Alexander Couzensba5a9922021-01-25 16:03:23 +0100815 return NS2_CS_ERROR;
Alexander Couzens6a161492020-07-12 13:45:50 +0200816
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100817 /* parse the tlv early to allow reject status msg to
818 * work with valid tp.
819 * Ignore the return code until the pdu type is parsed because
820 * an unknown pdu type should be ignored */
821 tlv = ns2_tlv_parse(&tp, nsh->data,
Alexander Couzensbac5b012020-12-09 23:32:22 +0100822 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Alexander Couzensbac5b012020-12-09 23:32:22 +0100823
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100824 switch (nsh->pdu_type) {
825 case NS_PDUT_STATUS:
Alexander Couzens6a161492020-07-12 13:45:50 +0200826 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
827 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
828 "for non-existing NS-VC\n",
829 logname);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100830 return NS2_CS_SKIPPED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100831 case NS_PDUT_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200832 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
833 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
834 "for non-existing NS-VC\n",
835 logname);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100836 return NS2_CS_SKIPPED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100837 case NS_PDUT_RESET_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200838 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
839 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
840 "for non-existing NS-VC\n",
841 logname);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100842 return NS2_CS_SKIPPED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100843 case NS_PDUT_RESET:
844 /* accept PDU RESET when vc_mode matches */
Alexander Couzensd923cff2020-12-01 01:03:52 +0100845 if (bind->accept_ipaccess) {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100846 dialect = GPRS_NS2_DIALECT_IPACCESS;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100847 break;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100848 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200849
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100850 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100851 if (rc < 0)
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100852 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100853 return NS2_CS_REJECTED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100854 default:
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200855 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100856 if (rc < 0)
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200857 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100858 return NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200859 }
860
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100861 if (tlv < 0) {
862 /* TODO: correct behaviour would checking what's wrong.
863 * If it's an essential TLV for the PDU return NS_CAUSE_INVAL_ESSENT_IE.
864 * Otherwise ignore the non-essential TLV. */
865 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
866 "TLV Parse\n", tlv);
867 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PROTO_ERR_UNSPEC);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100868 if (rc < 0)
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100869 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100870 return NS2_CS_REJECTED;
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100871 }
872
Harald Welte798efea2020-12-03 16:01:02 +0100873 if (!TLVP_PRES_LEN(&tp, NS_IE_CAUSE, 1) ||
874 !TLVP_PRES_LEN(&tp, NS_IE_VCI, 2) || !TLVP_PRES_LEN(&tp, NS_IE_NSEI, 2)) {
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200875 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
876 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100877 if (rc < 0)
878 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100879 return NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200880 }
881
Alexander Couzens6a161492020-07-12 13:45:50 +0200882 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100883 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
884
885 /* find or create NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +0200886 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
887 if (!nse) {
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100888 /* only create nse for udp & ipaccess */
Alexander Couzens138b96f2021-01-25 16:23:29 +0100889 if (bind->ll != GPRS_NS2_LL_UDP || dialect != GPRS_NS2_DIALECT_IPACCESS)
Alexander Couzensba5a9922021-01-25 16:03:23 +0100890 return NS2_CS_SKIPPED;
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100891
Alexander Couzens2e1a3a92021-01-27 20:44:41 +0100892 if (!bind->accept_ipaccess)
Alexander Couzensba5a9922021-01-25 16:03:23 +0100893 return NS2_CS_SKIPPED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200894
Alexander Couzensd923cff2020-12-01 01:03:52 +0100895 nse = gprs_ns2_create_nse(bind->nsi, nsei, bind->ll, dialect);
Alexander Couzens6a161492020-07-12 13:45:50 +0200896 if (!nse) {
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100897 LOGP(DLNS, LOGL_ERROR, "Failed to create NSE(%05u)\n", nsei);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100898 return NS2_CS_ERROR;
Alexander Couzens6a161492020-07-12 13:45:50 +0200899 }
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100900 } else {
901 /* nsei already known */
902 if (nse->ll != bind->ll) {
Harald Weltef2949742021-01-20 14:54:14 +0100903 LOGNSE(nse, LOGL_ERROR, "Received NS-RESET NS-VCI(%05u) with wrong linklayer(%s)"
904 " for already known NSE(%s)\n", nsvci, gprs_ns2_lltype_str(bind->ll),
905 gprs_ns2_lltype_str(nse->ll));
Alexander Couzensba5a9922021-01-25 16:03:23 +0100906 return NS2_CS_SKIPPED;
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100907 }
908 }
909
910 nsvc = gprs_ns2_nsvc_by_nsvci(bind->nsi, nsvci);
911 if (nsvc) {
912 if (nsvc->persistent) {
Harald Weltef2949742021-01-20 14:54:14 +0100913 LOGNSVC(nsvc, LOGL_ERROR, "Received NS-RESET for a persistent NSE over wrong connection.\n");
Alexander Couzensba5a9922021-01-25 16:03:23 +0100914 return NS2_CS_SKIPPED;
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100915 }
916 /* destroy old dynamic nsvc */
917 gprs_ns2_free_nsvc(nsvc);
918 }
919
920 /* do nse persistent check late to be more precise on the error message */
921 if (nse->persistent) {
Harald Weltef2949742021-01-20 14:54:14 +0100922 LOGNSE(nse, LOGL_ERROR, "Received NS-RESET for a persistent NSE but the unknown "
923 "NS-VCI(%05u)\n", nsvci);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100924 return NS2_CS_SKIPPED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200925 }
926
Harald Welte603f4042020-11-29 17:39:19 +0100927 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100928 vc_mode = ns2_dialect_to_vc_mode(dialect);
Harald Welte603f4042020-11-29 17:39:19 +0100929 snprintf(idbuf, sizeof(idbuf), "%s-NSE%05u-NSVC%05u", gprs_ns2_lltype_str(nse->ll),
930 nse->nsei, nsvci);
931 nsvc = ns2_vc_alloc(bind, nse, false, vc_mode, idbuf);
Alexander Couzens6a161492020-07-12 13:45:50 +0200932 if (!nsvc)
Alexander Couzensba5a9922021-01-25 16:03:23 +0100933 return NS2_CS_SKIPPED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200934
Alexander Couzens6a161492020-07-12 13:45:50 +0200935 nsvc->nsvci = nsvci;
936 nsvc->nsvci_is_valid = true;
937
938 *success = nsvc;
939
Alexander Couzensba5a9922021-01-25 16:03:23 +0100940 return NS2_CS_CREATED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200941}
942
Harald Welte5bef2cc2020-09-18 22:33:24 +0200943/*! Create, and connect an inactive, new IP-based NS-VC
944 * \param[in] bind bind in which the new NS-VC is to be created
945 * \param[in] remote remote address to which to connect
946 * \param[in] nse NS Entity in which the NS-VC is to be created
947 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
948 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200949struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700950 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200951 struct gprs_ns2_nse *nse,
952 uint16_t nsvci)
953{
954 struct gprs_ns2_vc *nsvc;
955
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100956 nsvc = ns2_ip_bind_connect(bind, nse, remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200957 if (!nsvc)
958 return NULL;
959
Alexander Couzens138b96f2021-01-25 16:23:29 +0100960 if (nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200961 nsvc->nsvci = nsvci;
962 nsvc->nsvci_is_valid = true;
963 }
964
965 return nsvc;
966}
967
Harald Welte5bef2cc2020-09-18 22:33:24 +0200968/*! Create, connect and activate a new IP-based NS-VC
969 * \param[in] bind bind in which the new NS-VC is to be created
970 * \param[in] remote remote address to which to connect
971 * \param[in] nse NS Entity in which the NS-VC is to be created
972 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
973 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200974struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700975 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200976 struct gprs_ns2_nse *nse,
977 uint16_t nsvci)
978{
979 struct gprs_ns2_vc *nsvc;
980 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
981 if (!nsvc)
982 return NULL;
983
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100984 ns2_vc_fsm_start(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200985
986 return nsvc;
987}
988
Harald Welte5bef2cc2020-09-18 22:33:24 +0200989/*! Create, connect and activate a new IP-based NS-VC
990 * \param[in] bind bind in which the new NS-VC is to be created
991 * \param[in] remote remote address to which to connect
992 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
993 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
994 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200995struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700996 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200997 uint16_t nsei,
Alexander Couzensd923cff2020-12-01 01:03:52 +0100998 uint16_t nsvci,
999 enum gprs_ns2_dialect dialect)
Alexander Couzens6a161492020-07-12 13:45:50 +02001000{
1001 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
1002
1003 if (!nse) {
Alexander Couzensd923cff2020-12-01 01:03:52 +01001004 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP, dialect);
Alexander Couzens6a161492020-07-12 13:45:50 +02001005 if (!nse)
1006 return NULL;
1007 }
1008
1009 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
1010}
1011
Harald Welte5bef2cc2020-09-18 22:33:24 +02001012/*! Find NS-VC for given socket address.
1013 * \param[in] nse NS Entity in which to search
1014 * \param[in] sockaddr socket address to search for
1015 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +02001016struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +07001017 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +02001018{
1019 struct gprs_ns2_vc *nsvc;
Alexander Couzens9a4cf272020-10-11 20:48:04 +02001020 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +02001021
1022 OSMO_ASSERT(nse);
1023 OSMO_ASSERT(sockaddr);
1024
1025 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +02001026 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +02001027 if (!osmo_sockaddr_cmp(sockaddr, remote))
1028 return nsvc;
1029 }
1030
1031 return NULL;
1032}
1033
Alexander Couzens6cb5d5f2020-10-11 23:23:31 +02001034/*!
1035 * Iterate over all nsvc of a NS Entity and call the callback.
1036 * If the callback returns < 0 it aborts the loop and returns the callback return code.
1037 * \param[in] nse NS Entity to iterate over all nsvcs
1038 * \param[in] cb the callback to call
1039 * \param[inout] cb_data the private data of the callback
1040 * \return 0 if the loop completes. If a callback returns < 0 it will returns this value.
1041 */
1042int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data)
1043{
1044 struct gprs_ns2_vc *nsvc, *tmp;
1045 int rc = 0;
1046 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
1047 rc = cb(nsvc, cb_data);
1048 if (rc < 0)
1049 return rc;
1050 }
1051
1052 return 0;
1053}
1054
1055
Alexander Couzens6a161492020-07-12 13:45:50 +02001056
Harald Welte5bef2cc2020-09-18 22:33:24 +02001057/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +02001058 * \param[in] nsvc NS-VC for which the message was received
1059 * \param msg the received message. Ownership is trasnferred, caller must not free it!
1060 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +02001061int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +02001062 struct msgb *msg)
1063{
1064 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Alexander Couzensba634532021-01-25 13:46:38 +01001065 struct tlv_parsed tp = { };
Alexander Couzens6a161492020-07-12 13:45:50 +02001066 int rc = 0;
1067
Daniel Willmann751977b2020-12-02 18:59:44 +01001068 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
1069 log_set_context(LOG_CTX_GB_NSVC, nsvc);
1070
Harald Weltee5f55f72021-01-30 21:51:15 +01001071 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
1072 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msg->len);
1073
Alexander Couzens6a161492020-07-12 13:45:50 +02001074 if (msg->len < sizeof(struct gprs_ns_hdr))
1075 return -EINVAL;
1076
1077 switch (nsh->pdu_type) {
1078 case SNS_PDUT_CONFIG:
1079 /* one additional byte ('end flag') before the TLV part starts */
1080 rc = ns2_tlv_parse(&tp, nsh->data+1,
1081 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
1082 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001083 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001084 return rc;
1085 }
1086 /* All sub-network service related message types */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001087 rc = ns2_sns_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001088 break;
1089 case SNS_PDUT_ACK:
1090 case SNS_PDUT_ADD:
1091 case SNS_PDUT_CHANGE_WEIGHT:
1092 case SNS_PDUT_DELETE:
1093 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +02001094 rc = ns2_tlv_parse(&tp, nsh->data+5,
1095 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001096 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001097 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001098 return rc;
1099 }
1100 tp.lv[NS_IE_NSEI].val = nsh->data+2;
1101 tp.lv[NS_IE_NSEI].len = 2;
1102 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
1103 tp.lv[NS_IE_TRANS_ID].len = 1;
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001104 rc = ns2_sns_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001105 break;
1106 case SNS_PDUT_CONFIG_ACK:
1107 case SNS_PDUT_SIZE:
1108 case SNS_PDUT_SIZE_ACK:
1109 rc = ns2_tlv_parse(&tp, nsh->data,
1110 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1111 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001112 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001113 return rc;
1114 }
1115 /* All sub-network service related message types */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001116 rc = ns2_sns_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001117 break;
1118
1119 case NS_PDUT_UNITDATA:
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001120 rc = ns2_vc_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001121 break;
1122 default:
1123 rc = ns2_tlv_parse(&tp, nsh->data,
1124 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1125 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001126 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
Alexander Couzens6a161492020-07-12 13:45:50 +02001127 if (nsh->pdu_type != NS_PDUT_STATUS)
1128 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
1129 return rc;
1130 }
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001131 rc = ns2_vc_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001132 break;
1133 }
1134
1135 return rc;
1136}
1137
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001138/* summarize all active data nsvcs */
1139void ns2_nse_data_sum(struct gprs_ns2_nse *nse)
1140{
1141 struct gprs_ns2_vc *nsvc;
Harald Welte3221f872021-01-18 14:58:51 +01001142
Alexander Couzens8a2a1a42020-12-26 18:00:17 +01001143 nse->nsvc_count = 0;
Harald Welte3221f872021-01-18 14:58:51 +01001144 nse->sum_data_weight = 0;
1145 nse->sum_sig_weight = 0;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001146
1147 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001148 if (!ns2_vc_is_unblocked(nsvc))
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001149 continue;
Alexander Couzens8a2a1a42020-12-26 18:00:17 +01001150
1151 nse->nsvc_count++;
Harald Welte3221f872021-01-18 14:58:51 +01001152 nse->sum_data_weight += nsvc->data_weight;
1153 nse->sum_sig_weight += nsvc->sig_weight;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001154 }
1155}
1156
Harald Welte5bef2cc2020-09-18 22:33:24 +02001157/*! Notify a nse about the change of a NS-VC.
1158 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
1159 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +02001160void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
1161{
1162 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +02001163
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001164 ns2_nse_data_sum(nse);
1165
Alexander Couzens6a161492020-07-12 13:45:50 +02001166 if (unblocked == nse->alive)
1167 return;
1168
Harald Welte3221f872021-01-18 14:58:51 +01001169 /* wait until both data_weight and sig_weight are != 0 before declaring NSE as alive */
1170 if (unblocked && nse->sum_data_weight && nse->sum_sig_weight) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001171 nse->alive = true;
Alexander Couzens138b96f2021-01-25 16:23:29 +01001172 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +02001173 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +02001174 return;
1175 }
1176
Harald Welte3221f872021-01-18 14:58:51 +01001177 if (nse->alive && (nse->sum_data_weight == 0 || nse->sum_sig_weight == 0)) {
1178 /* nse became unavailable */
1179 nse->alive = false;
Alexander Couzens138b96f2021-01-25 16:23:29 +01001180 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001181 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001182}
1183
1184/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +02001185 * \param[in] ctx a talloc context to allocate NS instance from
Alexander Couzenscce88282020-10-26 00:25:50 +01001186 * \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 +02001187 * \param[in] cb_data transparent user data passed to Call-back
1188 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001189struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
1190{
1191 struct gprs_ns2_inst *nsi;
1192
1193 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
1194 if (!nsi)
1195 return NULL;
1196
1197 nsi->cb = cb;
1198 nsi->cb_data = cb_data;
1199 INIT_LLIST_HEAD(&nsi->binding);
1200 INIT_LLIST_HEAD(&nsi->nse);
1201
1202 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1203 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1204 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1205 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1206 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1207 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1208 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
1209 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
Alexander Couzens90ee9632020-12-07 06:18:32 +01001210 nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES] = 3;
1211 nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES] = 3;
Alexander Couzens6a161492020-07-12 13:45:50 +02001212
1213 return nsi;
1214}
1215
Harald Welte5bef2cc2020-09-18 22:33:24 +02001216/*! Destroy a NS Instance (including all its NSEs, binds, ...).
1217 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001218void gprs_ns2_free(struct gprs_ns2_inst *nsi)
1219{
Alexander Couzens6a161492020-07-12 13:45:50 +02001220 if (!nsi)
1221 return;
1222
Alexander Couzens4b6c8af2020-10-11 20:15:25 +02001223 gprs_ns2_free_nses(nsi);
Alexander Couzens896fcd52020-10-11 19:52:36 +02001224 gprs_ns2_free_binds(nsi);
Alexander Couzens35315042020-10-10 03:28:17 +02001225
1226 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001227}
1228
Harald Welte5bef2cc2020-09-18 22:33:24 +02001229/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1230 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001231void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1232{
1233 struct gprs_ns2_vc *nsvc;
1234 OSMO_ASSERT(nse);
1235
1236 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1237 if (nsvc->sns_only)
1238 continue;
1239
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001240 ns2_vc_fsm_start(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +02001241 }
1242}
1243
Harald Welte5bef2cc2020-09-18 22:33:24 +02001244/*! Destroy a given bind.
1245 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001246void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1247{
1248 struct gprs_ns2_vc *nsvc, *tmp;
1249 if (!bind)
1250 return;
1251
1252 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1253 gprs_ns2_free_nsvc(nsvc);
1254 }
1255
1256 if (bind->driver->free_bind)
1257 bind->driver->free_bind(bind);
1258
1259 llist_del(&bind->list);
Harald Welte76346072021-01-31 11:54:02 +01001260 osmo_stat_item_group_free(bind->statg);
Alexander Couzensaaa55a62020-12-03 06:02:03 +01001261 talloc_free((char *)bind->name);
Alexander Couzens6a161492020-07-12 13:45:50 +02001262 talloc_free(bind);
1263}
Alexander Couzens896fcd52020-10-11 19:52:36 +02001264
1265void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi)
1266{
1267 struct gprs_ns2_vc_bind *bind, *tbind;
1268
1269 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1270 gprs_ns2_free_bind(bind);
1271 }
1272}
Alexander Couzensd923cff2020-12-01 01:03:52 +01001273
Alexander Couzensaaa55a62020-12-03 06:02:03 +01001274/*! Search for a bind with a unique name
1275 * \param[in] nsi NS instance on which we operate
1276 * \param[in] name The unique bind name to search for
1277 * \return the bind or NULL if not found
1278 */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001279struct gprs_ns2_vc_bind *gprs_ns2_bind_by_name(struct gprs_ns2_inst *nsi, const char *name)
Alexander Couzensaaa55a62020-12-03 06:02:03 +01001280{
1281 struct gprs_ns2_vc_bind *bind;
1282
1283 llist_for_each_entry(bind, &nsi->binding, list) {
1284 if (!strcmp(bind->name, name))
1285 return bind;
1286 }
1287
1288 return NULL;
1289}
1290
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001291enum gprs_ns2_vc_mode ns2_dialect_to_vc_mode(enum gprs_ns2_dialect dialect)
Alexander Couzensd923cff2020-12-01 01:03:52 +01001292{
1293 switch (dialect) {
Alexander Couzens138b96f2021-01-25 16:23:29 +01001294 case GPRS_NS2_DIALECT_SNS:
1295 case GPRS_NS2_DIALECT_STATIC_ALIVE:
1296 return GPRS_NS2_VC_MODE_ALIVE;
1297 case GPRS_NS2_DIALECT_STATIC_RESETBLOCK:
1298 case GPRS_NS2_DIALECT_IPACCESS:
1299 return GPRS_NS2_VC_MODE_BLOCKRESET;
Alexander Couzensd923cff2020-12-01 01:03:52 +01001300 default:
1301 return -1;
1302 }
1303}
1304
Alexander Couzens1c8785d2020-12-17 06:58:53 +01001305static void add_bind_array(struct gprs_ns2_vc_bind **array,
1306 struct gprs_ns2_vc_bind *bind, int size)
1307{
1308 int i;
1309 for (i=0; i < size; i++) {
1310 if (array[i] == bind)
1311 return;
1312 if (!array[i])
1313 break;
1314 }
1315
1316 if (i == size)
1317 return;
1318
1319 array[i] = bind;
1320}
1321
1322/*! calculate the transfer capabilities for a nse
1323 * \param nse the nse to count the transfer capability
1324 * \param bvci a bvci - unused
1325 * \return the transfer capability in mbit. On error < 0.
1326 */
1327int ns2_count_transfer_cap(struct gprs_ns2_nse *nse,
1328 uint16_t bvci)
1329{
1330 struct gprs_ns2_vc *nsvc;
1331 struct gprs_ns2_vc_bind **active_binds;
1332 int i, active_nsvcs = 0, transfer_cap = 0;
1333
1334 /* calculate the transfer capabilities based on the binds.
1335 * A bind has a transfer capability which is shared across all NSVCs.
1336 * Take care the bind cap is not counted twice within a NSE.
1337 * This should be accurate for FR and UDP but not for FR/GRE. */
1338
1339 if (!nse->alive)
1340 return 0;
1341
1342 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001343 if (ns2_vc_is_unblocked(nsvc))
Alexander Couzens1c8785d2020-12-17 06:58:53 +01001344 active_nsvcs++;
1345 }
1346 /* an alive nse should always have active_nsvcs */
1347 OSMO_ASSERT(active_nsvcs);
1348
1349 active_binds = talloc_zero_array(nse, struct gprs_ns2_vc_bind*, active_nsvcs);
1350 if (!active_binds)
1351 return -ENOMEM;
1352
1353 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001354 if (!ns2_vc_is_unblocked(nsvc))
Alexander Couzens1c8785d2020-12-17 06:58:53 +01001355 continue;
1356 add_bind_array(active_binds, nsvc->bind, active_nsvcs);
1357 }
1358
1359 /* TODO: change calcuation for FR/GRE */
1360 for (i = 0; i < active_nsvcs; i++) {
1361 if (active_binds[i])
1362 transfer_cap += active_binds[i]->transfer_capability;
1363 }
1364
1365 talloc_free(active_binds);
1366 return transfer_cap;
1367}
1368
Harald Weltec3aa8f92021-01-31 11:41:34 +01001369/*! common allocation + low-level initialization of a bind. Called by vc-drivers */
1370int ns2_bind_alloc(struct gprs_ns2_inst *nsi, const char *name,
1371 struct gprs_ns2_vc_bind **result)
1372{
1373 struct gprs_ns2_vc_bind *bind;
1374
1375 if (!name)
1376 return -EINVAL;
1377
1378 if (gprs_ns2_bind_by_name(nsi, name))
1379 return -EALREADY;
1380
1381 bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
1382 if (!bind)
Harald Weltebdfb8b92021-01-31 11:44:57 +01001383 return -ENOMEM;
Harald Weltec3aa8f92021-01-31 11:41:34 +01001384
1385 bind->name = talloc_strdup(bind, name);
1386 if (!bind->name) {
1387 talloc_free(bind);
Harald Weltebdfb8b92021-01-31 11:44:57 +01001388 return -ENOMEM;
Harald Weltec3aa8f92021-01-31 11:41:34 +01001389 }
1390
Harald Welte76346072021-01-31 11:54:02 +01001391 bind->statg = osmo_stat_item_group_alloc(bind, &nsbind_statg_desc, nsi->bind_rate_ctr_idx);
1392 if (!bind->statg) {
1393 talloc_free(bind);
1394 return -ENOMEM;
1395 }
1396
Harald Weltec3aa8f92021-01-31 11:41:34 +01001397 bind->nsi = nsi;
1398 INIT_LLIST_HEAD(&bind->nsvc);
1399 llist_add(&bind->list, &nsi->binding);
1400
Harald Welte76346072021-01-31 11:54:02 +01001401 nsi->bind_rate_ctr_idx++;
1402
Harald Weltec3aa8f92021-01-31 11:41:34 +01001403 if (result)
1404 *result = bind;
1405
1406 return 0;
1407}
1408
Alexander Couzens6a161492020-07-12 13:45:50 +02001409/*! @} */