blob: c48575b3099408a412c058ecb5d2e88e078d311c [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 Couzensb2de5462021-02-16 16:24:30 +0100220 { GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS, "NSE SNS no endpoints"},
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100221 { GPRS_NS2_AFF_CAUSE_MTU_CHANGE, "NSE MTU changed" },
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100222 { 0, NULL }
223};
224
Alexander Couzens0ab028c2020-11-04 02:41:44 +0100225const struct value_string gprs_ns2_prim_strs[] = {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100226 { GPRS_NS2_PRIM_UNIT_DATA, "UNIT DATA" },
227 { GPRS_NS2_PRIM_CONGESTION, "CONGESTION" },
228 { GPRS_NS2_PRIM_STATUS, "STATUS" },
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100229 { 0, NULL }
230};
231
Harald Weltea24e7ee2020-11-29 17:38:48 +0100232const struct value_string gprs_ns2_lltype_strs[] = {
233 { GPRS_NS2_LL_UDP, "UDP" },
234 { GPRS_NS2_LL_FR_GRE, "FR_GRE" },
235 { GPRS_NS2_LL_FR, "FR" },
236 { 0, NULL }
237};
238
Harald Welte5bef2cc2020-09-18 22:33:24 +0200239/*! string-format a given NS-VC into a user-supplied buffer.
240 * \param[in] buf user-allocated output buffer
241 * \param[in] buf_len size of user-allocated output buffer in bytes
242 * \param[in] nsvc NS-VC to be string-formatted
243 * \return pointer to buf on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200244char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc)
245{
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200246 const struct osmo_sockaddr *local;
247 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200248 struct osmo_sockaddr_str local_str;
249 struct osmo_sockaddr_str remote_str;
250
251 if (!buf_len)
Harald Welte92ad0292020-09-18 22:34:24 +0200252 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200253
Alexander Couzensaac90162020-11-19 02:44:04 +0100254 switch (nsvc->nse->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100255 case GPRS_NS2_LL_UDP:
Alexander Couzens6a161492020-07-12 13:45:50 +0200256 if (!gprs_ns2_is_ip_bind(nsvc->bind)) {
257 buf[0] = '\0';
258 return buf;
259 }
260
261 local = gprs_ns2_ip_bind_sockaddr(nsvc->bind);
Alexander Couzensc4229a42020-10-11 20:58:04 +0200262 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200263 if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas))
264 strcpy(local_str.ip, "invalid");
265 if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas))
266 strcpy(remote_str.ip, "invalid");
267
268 if (nsvc->nsvci_is_valid)
269 snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u",
270 local_str.ip, local_str.port,
271 nsvc->nsvci,
272 remote_str.ip, remote_str.port);
273 else
274 snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u",
275 local_str.ip, local_str.port,
276 remote_str.ip, remote_str.port);
277 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100278 case GPRS_NS2_LL_FR_GRE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200279 snprintf(buf, buf_len, "frgre)");
280 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100281 case GPRS_NS2_LL_FR:
Alexander Couzens841817e2020-11-19 00:41:29 +0100282 snprintf(buf, buf_len, "fr)netif: %s dlci: %u", gprs_ns2_fr_bind_netif(nsvc->bind),
283 gprs_ns2_fr_nsvc_dlci(nsvc));
284 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200285 default:
Harald Welte6188e002020-12-01 17:20:07 +0100286 snprintf(buf, buf_len, "unknown)");
Alexander Couzens6a161492020-07-12 13:45:50 +0200287 break;
288 }
289
290 buf[buf_len - 1] = '\0';
291
292 return buf;
293}
294
295/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
296#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
297
Harald Welte5bef2cc2020-09-18 22:33:24 +0200298/*! string-format a given NS-VC to a thread-local static buffer.
299 * \param[in] nsvc NS-VC to be string-formatted
300 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200301const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
302{
303 static __thread char buf[NS2_LL_MAX_STR];
304 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
305}
306
Harald Welte5bef2cc2020-09-18 22:33:24 +0200307/*! string-format a given NS-VC to a dynamically allocated string.
308 * \param[in] ctx talloc context from which to allocate
309 * \param[in] nsvc NS-VC to be string-formatted
310 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200311char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
312{
313 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
314 if (!buf)
315 return buf;
316 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
317}
318
Daniel Willmannf1286542020-11-03 23:03:33 +0100319/*! Return the current state name of a given NS-VC to a thread-local static buffer.
320 * \param[in] nsvc NS-VC to return the state of
321 * \return pointer to the string on success; NULL on error */
322const char *gprs_ns2_nsvc_state_name(struct gprs_ns2_vc *nsvc)
323{
324 return osmo_fsm_inst_state_name(nsvc->fi);
325}
326
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100327/* select a signalling NSVC and respect sig_counter
328 * param[out] reset_counter - all counter has to be resetted to their signal weight
329 * return the chosen nsvc or NULL
330 */
331static struct gprs_ns2_vc *ns2_load_sharing_signal(struct gprs_ns2_nse *nse)
332{
333 struct gprs_ns2_vc *nsvc = NULL, *last = NULL, *tmp;
334
335 llist_for_each_entry(tmp, &nse->nsvc, list) {
336 if (tmp->sig_weight == 0)
337 continue;
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100338 if (!ns2_vc_is_unblocked(tmp))
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100339 continue;
340 if (tmp->sig_counter == 0) {
341 last = tmp;
342 continue;
343 }
344
345 tmp->sig_counter--;
346 nsvc = tmp;
347 break;
348 }
349
350 /* all counter were zero, but there are valid nsvc */
351 if (!nsvc && last) {
352 llist_for_each_entry(tmp, &nse->nsvc, list) {
353 tmp->sig_counter = tmp->sig_weight;
354 }
355
356 last->sig_counter--;
357 return last;
358 } else {
359 return nsvc;
360 }
361}
362
363/* 4.4.1 Load Sharing function for the Frame Relay Sub-Network */
Alexander Couzens9e81a322021-02-16 17:36:25 +0100364static struct gprs_ns2_vc *ns2_load_sharing_modulo(
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100365 struct gprs_ns2_nse *nse,
366 uint16_t bvci,
367 uint32_t load_selector)
368{
369 struct gprs_ns2_vc *tmp;
Alexander Couzens265a5eb2021-02-02 11:26:17 +0100370 uint32_t mod;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100371 uint32_t i = 0;
372
Alexander Couzens265a5eb2021-02-02 11:26:17 +0100373 if (nse->nsvc_count == 0)
374 return NULL;
375
376 mod = (bvci + load_selector) % nse->nsvc_count;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100377 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100378 if (!ns2_vc_is_unblocked(tmp))
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100379 continue;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100380 if (i == mod)
381 return tmp;
382 i++;
383 }
384
385 return NULL;
386}
387
Alexander Couzensd8a8d982021-02-15 01:07:45 +0100388/* 4.4.2 Load Sharing function for the IP Sub-Network
389 *
390 * Implement a simple approach for UDP load sharing of data weight based on the modulo of the lsp.
391 *
392 * E.g. 3 NSVC: 1st weight 5, 2nd weight 3, 3rd weight 1, lsp = 3.
393 * sum all weights = 9
394 * target_weight = lsp % sum = 3
395 *
396 * 1st NSVC will be the target for 0-4
397 * 2nd NSVC will be the target for 5-7
398 * 3rd NSVC will be the target for 8
399 *
400 * The 1st NSVC will be used.
401 * E.g. lsp = 7. The 2nd NSVC will used.
402 */
403static struct gprs_ns2_vc *ns2_load_sharing_weight_modulo(
404 struct gprs_ns2_nse *nse,
405 uint16_t bvci,
406 uint32_t load_selector)
407{
408 struct gprs_ns2_vc *tmp;
409 uint32_t mod;
410 uint32_t i = 0;
411
412 if (nse->nsvc_count == 0)
413 return NULL;
414
415 mod = (bvci + load_selector) % nse->sum_data_weight;
416 llist_for_each_entry(tmp, &nse->nsvc, list) {
417 if (!ns2_vc_is_unblocked(tmp))
418 continue;
419 if (i == mod || mod < i + tmp->data_weight)
420 return tmp;
421 i += tmp->data_weight;
422 }
423
424 return NULL;
425}
426
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100427/* pick the first available data NSVC - no load sharing */
428struct gprs_ns2_vc *ns2_load_sharing_first(struct gprs_ns2_nse *nse)
429{
430 struct gprs_ns2_vc *nsvc = NULL, *tmp;
431
432 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100433 if (!ns2_vc_is_unblocked(tmp))
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100434 continue;
435 if (tmp->data_weight == 0)
436 continue;
437
438 nsvc = tmp;
439 break;
440 }
441
442 return nsvc;
443}
444
445
446static struct gprs_ns2_vc *ns2_load_sharing(
447 struct gprs_ns2_nse *nse,
448 uint16_t bvci,
449 uint32_t link_selector)
450{
451 struct gprs_ns2_vc *nsvc = NULL;
452
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100453 switch (nse->ll) {
454 case GPRS_NS2_LL_FR:
Alexander Couzens9e81a322021-02-16 17:36:25 +0100455 nsvc = ns2_load_sharing_modulo(nse, bvci, link_selector);
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100456 break;
457 case GPRS_NS2_LL_UDP:
458 default:
459 if (bvci == 0) {
460 /* signalling */
461 nsvc = ns2_load_sharing_signal(nse);
462 } else {
463 /* data with load sharing parameter */
Alexander Couzensd8a8d982021-02-15 01:07:45 +0100464 nsvc = ns2_load_sharing_weight_modulo(nse, bvci, link_selector);
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100465 }
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100466 break;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100467 }
468
469 return nsvc;
470}
471
Harald Welte5bef2cc2020-09-18 22:33:24 +0200472/*! Receive a primitive from the NS User (Gb).
473 * \param[in] nsi NS instance to which the primitive is issued
474 * \param[in] oph The primitive
475 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200476int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
477{
Alexander Couzens6a161492020-07-12 13:45:50 +0200478 /* TODO: implement resource distribution */
479 /* TODO: check for empty PDUs which can be sent to Request/Confirm
480 * the IP endpoint */
481 struct osmo_gprs_ns2_prim *nsp;
482 struct gprs_ns2_nse *nse = NULL;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100483 struct gprs_ns2_vc *nsvc = NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200484 uint16_t bvci, nsei;
485 uint8_t sducontrol = 0;
Alexander Couzens6df11602021-02-14 23:39:40 +0100486 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200487
Alexander Couzens6df11602021-02-14 23:39:40 +0100488 if (oph->sap != SAP_NS) {
489 rc = -EINVAL;
490 goto out;
491 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200492
493 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
494
Alexander Couzens6df11602021-02-14 23:39:40 +0100495 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != GPRS_NS2_PRIM_UNIT_DATA) {
496 rc = -EINVAL;
497 goto out;
498 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200499
Alexander Couzens6df11602021-02-14 23:39:40 +0100500 if (!oph->msg) {
501 rc = -EINVAL;
502 goto out;
503 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200504
505 bvci = nsp->bvci;
506 nsei = nsp->nsei;
507
508 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
Alexander Couzens6df11602021-02-14 23:39:40 +0100509 if (!nse) {
510 rc = -EINVAL;
511 goto out;
512 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200513
Alexander Couzens6df11602021-02-14 23:39:40 +0100514 if (!nse->alive) {
515 goto out;
516 }
Alexander Couzens265a5eb2021-02-02 11:26:17 +0100517
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100518 nsvc = ns2_load_sharing(nse, bvci, nsp->u.unitdata.link_selector);
Alexander Couzens6a161492020-07-12 13:45:50 +0200519
520 /* TODO: send a status primitive back */
521 if (!nsvc)
Alexander Couzens6df11602021-02-14 23:39:40 +0100522 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200523
Alexander Couzens138b96f2021-01-25 16:23:29 +0100524 if (nsp->u.unitdata.change == GPRS_NS2_ENDPOINT_REQUEST_CHANGE)
Alexander Couzens6a161492020-07-12 13:45:50 +0200525 sducontrol = 1;
Alexander Couzens138b96f2021-01-25 16:23:29 +0100526 else if (nsp->u.unitdata.change == GPRS_NS2_ENDPOINT_CONFIRM_CHANGE)
Alexander Couzens6a161492020-07-12 13:45:50 +0200527 sducontrol = 2;
528
529 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
Alexander Couzens6df11602021-02-14 23:39:40 +0100530
531out:
532 msgb_free(oph->msg);
533 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200534}
535
Harald Welte5bef2cc2020-09-18 22:33:24 +0200536/*! Send a STATUS.ind primitive to the specified NS instance user.
537 * \param[in] nsi NS instance on which we operate
538 * \param[in] nsei NSEI to which the statue relates
539 * \param[in] bvci BVCI to which the status relates
540 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200541void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100542 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200543 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200544 enum gprs_ns2_affecting_cause cause)
545{
Daniel Willmann15c09a82020-11-03 23:05:43 +0100546 char nsvc_str[NS2_LL_MAX_STR];
Alexander Couzens6a161492020-07-12 13:45:50 +0200547 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200548 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200549 nsp.bvci = bvci;
550 nsp.u.status.cause = cause;
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100551 nsp.u.status.transfer = ns2_count_transfer_cap(nse, bvci);
Alexander Couzensda0a2852020-10-01 23:24:07 +0200552 nsp.u.status.first = nse->first;
553 nsp.u.status.persistent = nse->persistent;
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100554 if (nse->mtu < 4)
555 nsp.u.status.mtu = 0;
556 else
557 nsp.u.status.mtu = nse->mtu - 4; /* 1 Byte NS PDU type, 1 Byte NS SDU control, 2 Byte BVCI */
558
Harald Welte86690432021-01-31 16:09:19 +0100559 if (nsvc) {
Daniel Willmann15c09a82020-11-03 23:05:43 +0100560 nsp.u.status.nsvc = gprs_ns2_ll_str_buf(nsvc_str, sizeof(nsvc_str), nsvc);
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100561 LOGNSVC(nsvc, LOGL_NOTICE, "NS-STATUS.ind(bvci=%05u): cause=%s, transfer=%d, first=%d, mtu=%d\n",
Harald Welte86690432021-01-31 16:09:19 +0100562 nsp.bvci, gprs_ns2_aff_cause_prim_str(nsp.u.status.cause),
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100563 nsp.u.status.transfer, nsp.u.status.first, nse->mtu);
Harald Welte86690432021-01-31 16:09:19 +0100564 } else {
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100565 LOGNSE(nse, LOGL_NOTICE, "NS-STATUS.ind(bvci=%05u): cause=%s, transfer=%d, first=%d, mtu=%d\n",
Harald Welte86690432021-01-31 16:09:19 +0100566 nsp.bvci, gprs_ns2_aff_cause_prim_str(nsp.u.status.cause),
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100567 nsp.u.status.transfer, nsp.u.status.first, nse->mtu);
Harald Welte86690432021-01-31 16:09:19 +0100568 }
Daniel Willmann15c09a82020-11-03 23:05:43 +0100569
Harald Welte86690432021-01-31 16:09:19 +0100570 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_STATUS, PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200571 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200572}
573
Harald Welte5bef2cc2020-09-18 22:33:24 +0200574/*! Allocate a NS-VC within the given bind + NSE.
575 * \param[in] bind The 'bind' on which we operate
576 * \param[in] nse The NS Entity on which we operate
577 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
Harald Welte603f4042020-11-29 17:39:19 +0100578 * \param[in] id - human-readable identifier
Harald Welte5bef2cc2020-09-18 22:33:24 +0200579 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzensd923cff2020-12-01 01:03:52 +0100580struct 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 +0100581 enum gprs_ns2_vc_mode vc_mode, const char *id)
Alexander Couzens6a161492020-07-12 13:45:50 +0200582{
Daniel Willmannbb899052021-01-16 14:02:45 +0100583 /* Sanity check */
584 OSMO_ASSERT(bind->ll == nse->ll);
585
Alexander Couzens6a161492020-07-12 13:45:50 +0200586 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
587
588 if (!nsvc)
589 return NULL;
590
591 nsvc->bind = bind;
592 nsvc->nse = nse;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100593 nsvc->mode = vc_mode;
Alexander Couzens6a161492020-07-12 13:45:50 +0200594 nsvc->sig_weight = 1;
595 nsvc->data_weight = 1;
596
Harald Welte97ccbf72021-01-31 11:49:19 +0100597 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->nsvc_rate_ctr_idx);
Alexander Couzens6a161492020-07-12 13:45:50 +0200598 if (!nsvc->ctrg) {
599 goto err;
600 }
Harald Welte97ccbf72021-01-31 11:49:19 +0100601 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->nsvc_rate_ctr_idx);
Alexander Couzens6a161492020-07-12 13:45:50 +0200602 if (!nsvc->statg)
603 goto err_group;
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100604 if (!ns2_vc_fsm_alloc(nsvc, id, initiater))
Alexander Couzens6a161492020-07-12 13:45:50 +0200605 goto err_statg;
606
Harald Welte97ccbf72021-01-31 11:49:19 +0100607 bind->nsi->nsvc_rate_ctr_idx++;
Alexander Couzens6a161492020-07-12 13:45:50 +0200608
609 llist_add(&nsvc->list, &nse->nsvc);
610 llist_add(&nsvc->blist, &bind->nsvc);
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100611 ns2_nse_update_mtu(nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200612
613 return nsvc;
614
615err_statg:
616 osmo_stat_item_group_free(nsvc->statg);
617err_group:
618 rate_ctr_group_free(nsvc->ctrg);
619err:
620 talloc_free(nsvc);
621
622 return NULL;
623}
624
Harald Welte5bef2cc2020-09-18 22:33:24 +0200625/*! Destroy/release given NS-VC.
626 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200627void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
628{
629 if (!nsvc)
630 return;
631
Alexander Couzens138b96f2021-01-25 16:23:29 +0100632 ns2_prim_status_ind(nsvc->nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200633
634 llist_del(&nsvc->list);
635 llist_del(&nsvc->blist);
636
637 /* notify nse this nsvc is unavailable */
638 ns2_nse_notify_unblocked(nsvc, false);
639
640 /* check if sns is using this VC */
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100641 ns2_sns_replace_nsvc(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200642 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
643
644 /* let the driver/bind clean up it's internal state */
645 if (nsvc->priv && nsvc->bind->free_vc)
646 nsvc->bind->free_vc(nsvc);
647
648 osmo_stat_item_group_free(nsvc->statg);
649 rate_ctr_group_free(nsvc->ctrg);
650
651 talloc_free(nsvc);
652}
653
Alexander Couzens47558792020-12-06 03:16:11 +0100654/*! Destroy/release all NS-VC of given NSE
655 * \param[in] nse NSE
656 */
657void gprs_ns2_free_nsvcs(struct gprs_ns2_nse *nse)
658{
659 struct gprs_ns2_vc *nsvc, *tmp;
660
661 if (!nse)
662 return;
663
664 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
665 gprs_ns2_free_nsvc(nsvc);
666 }
667}
668
Harald Welte5bef2cc2020-09-18 22:33:24 +0200669/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100670struct msgb *ns2_msgb_alloc(void)
Alexander Couzens6a161492020-07-12 13:45:50 +0200671{
672 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
673 "GPRS/NS");
674 if (!msg) {
675 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
676 NS_ALLOC_SIZE);
677 }
678 return msg;
679}
680
Harald Welte5bef2cc2020-09-18 22:33:24 +0200681/*! Create a status message to be sent over a new connection.
682 * \param[in] orig_msg the original message
683 * \param[in] tp TLVP parsed of the original message
684 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
685 * \param[in] cause Cause for the rejection
686 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200687static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
688{
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100689 struct msgb *msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200690 struct gprs_ns_hdr *nsh;
691 bool have_vci = false;
692 uint8_t _cause = cause;
693 uint16_t nsei = 0;
694
695 if (!msg)
696 return -ENOMEM;
697
Harald Welte798efea2020-12-03 16:01:02 +0100698 if (TLVP_PRES_LEN(tp, NS_IE_NSEI, 2)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200699 nsei = tlvp_val16be(tp, NS_IE_NSEI);
700
701 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
702 nsei, gprs_ns2_cause_str(cause));
703 }
704
705 msg->l2h = msgb_put(msg, sizeof(*nsh));
706 nsh = (struct gprs_ns_hdr *) msg->l2h;
707 nsh->pdu_type = NS_PDUT_STATUS;
708
709 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
Harald Welte798efea2020-12-03 16:01:02 +0100710 have_vci = TLVP_PRES_LEN(tp, NS_IE_VCI, 2);
Alexander Couzens6a161492020-07-12 13:45:50 +0200711
712 /* Section 9.2.7.1: Static conditions for NS-VCI */
713 if (cause == NS_CAUSE_NSVC_BLOCKED ||
714 cause == NS_CAUSE_NSVC_UNKNOWN) {
715 if (!have_vci) {
716 msgb_free(msg);
717 return -EINVAL;
718 }
719
720 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
721 }
722
723 /* Section 9.2.7.2: Static conditions for NS PDU */
724 switch (cause) {
725 case NS_CAUSE_SEM_INCORR_PDU:
726 case NS_CAUSE_PDU_INCOMP_PSTATE:
727 case NS_CAUSE_PROTO_ERR_UNSPEC:
728 case NS_CAUSE_INVAL_ESSENT_IE:
729 case NS_CAUSE_MISSING_ESSENT_IE:
730 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
731 orig_msg->l2h);
732 break;
733 default:
734 break;
735 }
736
737 *reject = msg;
738 return 0;
739}
740
Harald Welte5bef2cc2020-09-18 22:33:24 +0200741/*! Resolve a NS Entity based on its NSEI.
742 * \param[in] nsi NS Instance in which we do the look-up
743 * \param[in] nsei NSEI to look up
744 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200745struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
746{
747 struct gprs_ns2_nse *nse;
748
749 llist_for_each_entry(nse, &nsi->nse, list) {
750 if (nse->nsei == nsei)
751 return nse;
752 }
753
754 return NULL;
755}
756
Harald Welte5bef2cc2020-09-18 22:33:24 +0200757/*! Resolve a NS-VC Entity based on its NS-VCI.
758 * \param[in] nsi NS Instance in which we do the look-up
759 * \param[in] nsvci NS-VCI to look up
760 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200761struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
762{
763 struct gprs_ns2_nse *nse;
764 struct gprs_ns2_vc *nsvc;
765
766 llist_for_each_entry(nse, &nsi->nse, list) {
767 llist_for_each_entry(nsvc, &nse->nsvc, list) {
768 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
769 return nsvc;
770 }
771 }
772
773 return NULL;
774}
775
Harald Welte5bef2cc2020-09-18 22:33:24 +0200776/*! Create a NS Entity within given NS instance.
777 * \param[in] nsi NS instance in which to create NS Entity
778 * \param[in] nsei NS Entity Identifier of to-be-created NSE
779 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzensd923cff2020-12-01 01:03:52 +0100780struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei,
781 enum gprs_ns2_ll linklayer, enum gprs_ns2_dialect dialect)
Alexander Couzens6a161492020-07-12 13:45:50 +0200782{
783 struct gprs_ns2_nse *nse;
Alexander Couzens93ad4992020-12-06 03:03:03 +0100784 char sns[16];
Alexander Couzens6a161492020-07-12 13:45:50 +0200785
786 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
787 if (nse) {
Harald Weltef2949742021-01-20 14:54:14 +0100788 LOGNSE(nse, LOGL_ERROR, "Can not create a NSE with already taken NSEI\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200789 return nse;
790 }
791
792 nse = talloc_zero(nsi, struct gprs_ns2_nse);
793 if (!nse)
794 return NULL;
795
Alexander Couzens138b96f2021-01-25 16:23:29 +0100796 if (dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens93ad4992020-12-06 03:03:03 +0100797 snprintf(sns, sizeof(sns), "NSE%05u-SNS", nsei);
798 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, sns);
799 if (!nse->bss_sns_fi) {
800 talloc_free(nse);
801 return NULL;
802 }
803 }
804
Alexander Couzensd923cff2020-12-01 01:03:52 +0100805 nse->dialect = dialect;
Alexander Couzensaac90162020-11-19 02:44:04 +0100806 nse->ll = linklayer;
Alexander Couzens6a161492020-07-12 13:45:50 +0200807 nse->nsei = nsei;
808 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200809 nse->first = true;
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100810 nse->mtu = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200811 llist_add(&nse->list, &nsi->nse);
812 INIT_LLIST_HEAD(&nse->nsvc);
813
814 return nse;
815}
816
Alexander Couzens05e7f7d2020-10-11 19:51:46 +0200817/*! Return the NSEI
818 * \param[in] nse NS Entity
819 * \return the nsei.
820 */
821uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse)
822{
823 return nse->nsei;
824}
825
Harald Welte5bef2cc2020-09-18 22:33:24 +0200826/*! Destroy given NS Entity.
827 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200828void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
829{
Alexander Couzens6a161492020-07-12 13:45:50 +0200830 if (!nse)
831 return;
832
Alexander Couzensd1cd6502021-01-15 02:16:23 +0100833 nse->alive = false;
Alexander Couzens47558792020-12-06 03:16:11 +0100834 gprs_ns2_free_nsvcs(nse);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100835 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200836
837 llist_del(&nse->list);
838 if (nse->bss_sns_fi)
839 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
840 talloc_free(nse);
841}
842
Alexander Couzens4b6c8af2020-10-11 20:15:25 +0200843void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi)
844{
845 struct gprs_ns2_nse *nse, *ntmp;
846
847 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
848 gprs_ns2_free_nse(nse);
849 }
850}
851
Alexander Couzens6a161492020-07-12 13:45:50 +0200852static inline int ns2_tlv_parse(struct tlv_parsed *dec,
853 const uint8_t *buf, int buf_len, uint8_t lv_tag,
854 uint8_t lv_tag2)
855{
856 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
857 * See comment of ns_att_tlvdef1. */
858 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
859 if (rc < 0)
860 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
861 return rc;
862}
863
864
Harald Welte5bef2cc2020-09-18 22:33:24 +0200865/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
866 * \param[in] bind the bind through which msg was received
867 * \param[in] msg the actual received message
868 * \param[in] logname A name to describe the VC. E.g. ip address pair
869 * \param[out] reject A message filled to be sent back. Only used in failure cases.
870 * \param[out] success A pointer which will be set to the new VC on success
871 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzensba5a9922021-01-25 16:03:23 +0100872enum ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
873 struct msgb *msg,
874 const char *logname,
875 struct msgb **reject,
876 struct gprs_ns2_vc **success)
Alexander Couzens6a161492020-07-12 13:45:50 +0200877{
878 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
879 struct tlv_parsed tp;
880 struct gprs_ns2_vc *nsvc;
881 struct gprs_ns2_nse *nse;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100882 enum gprs_ns2_dialect dialect;
883 enum gprs_ns2_vc_mode vc_mode;
Alexander Couzens6a161492020-07-12 13:45:50 +0200884 uint16_t nsvci;
885 uint16_t nsei;
Harald Welte603f4042020-11-29 17:39:19 +0100886 char idbuf[32];
Alexander Couzens6a161492020-07-12 13:45:50 +0200887
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100888 int rc, tlv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200889
890 if (msg->len < sizeof(struct gprs_ns_hdr))
Alexander Couzensba5a9922021-01-25 16:03:23 +0100891 return NS2_CS_ERROR;
Alexander Couzens6a161492020-07-12 13:45:50 +0200892
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100893 /* parse the tlv early to allow reject status msg to
894 * work with valid tp.
895 * Ignore the return code until the pdu type is parsed because
896 * an unknown pdu type should be ignored */
897 tlv = ns2_tlv_parse(&tp, nsh->data,
Alexander Couzensbac5b012020-12-09 23:32:22 +0100898 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Alexander Couzensbac5b012020-12-09 23:32:22 +0100899
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100900 switch (nsh->pdu_type) {
901 case NS_PDUT_STATUS:
Alexander Couzens6a161492020-07-12 13:45:50 +0200902 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
903 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
904 "for non-existing NS-VC\n",
905 logname);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100906 return NS2_CS_SKIPPED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100907 case NS_PDUT_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200908 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
909 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
910 "for non-existing NS-VC\n",
911 logname);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100912 return NS2_CS_SKIPPED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100913 case NS_PDUT_RESET_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200914 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
915 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
916 "for non-existing NS-VC\n",
917 logname);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100918 return NS2_CS_SKIPPED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100919 case NS_PDUT_RESET:
920 /* accept PDU RESET when vc_mode matches */
Alexander Couzensd923cff2020-12-01 01:03:52 +0100921 if (bind->accept_ipaccess) {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100922 dialect = GPRS_NS2_DIALECT_IPACCESS;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100923 break;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100924 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200925
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100926 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100927 if (rc < 0)
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100928 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100929 return NS2_CS_REJECTED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100930 default:
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200931 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100932 if (rc < 0)
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200933 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100934 return NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200935 }
936
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100937 if (tlv < 0) {
938 /* TODO: correct behaviour would checking what's wrong.
939 * If it's an essential TLV for the PDU return NS_CAUSE_INVAL_ESSENT_IE.
940 * Otherwise ignore the non-essential TLV. */
941 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
942 "TLV Parse\n", tlv);
943 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PROTO_ERR_UNSPEC);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100944 if (rc < 0)
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100945 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100946 return NS2_CS_REJECTED;
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100947 }
948
Harald Welte798efea2020-12-03 16:01:02 +0100949 if (!TLVP_PRES_LEN(&tp, NS_IE_CAUSE, 1) ||
950 !TLVP_PRES_LEN(&tp, NS_IE_VCI, 2) || !TLVP_PRES_LEN(&tp, NS_IE_NSEI, 2)) {
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200951 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
952 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100953 if (rc < 0)
954 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100955 return NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200956 }
957
Alexander Couzens6a161492020-07-12 13:45:50 +0200958 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100959 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
960
961 /* find or create NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +0200962 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
963 if (!nse) {
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100964 /* only create nse for udp & ipaccess */
Alexander Couzens138b96f2021-01-25 16:23:29 +0100965 if (bind->ll != GPRS_NS2_LL_UDP || dialect != GPRS_NS2_DIALECT_IPACCESS)
Alexander Couzensba5a9922021-01-25 16:03:23 +0100966 return NS2_CS_SKIPPED;
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100967
Alexander Couzens2e1a3a92021-01-27 20:44:41 +0100968 if (!bind->accept_ipaccess)
Alexander Couzensba5a9922021-01-25 16:03:23 +0100969 return NS2_CS_SKIPPED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200970
Alexander Couzensd923cff2020-12-01 01:03:52 +0100971 nse = gprs_ns2_create_nse(bind->nsi, nsei, bind->ll, dialect);
Alexander Couzens6a161492020-07-12 13:45:50 +0200972 if (!nse) {
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100973 LOGP(DLNS, LOGL_ERROR, "Failed to create NSE(%05u)\n", nsei);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100974 return NS2_CS_ERROR;
Alexander Couzens6a161492020-07-12 13:45:50 +0200975 }
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100976 } else {
977 /* nsei already known */
978 if (nse->ll != bind->ll) {
Harald Weltef2949742021-01-20 14:54:14 +0100979 LOGNSE(nse, LOGL_ERROR, "Received NS-RESET NS-VCI(%05u) with wrong linklayer(%s)"
980 " for already known NSE(%s)\n", nsvci, gprs_ns2_lltype_str(bind->ll),
981 gprs_ns2_lltype_str(nse->ll));
Alexander Couzensba5a9922021-01-25 16:03:23 +0100982 return NS2_CS_SKIPPED;
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100983 }
984 }
985
986 nsvc = gprs_ns2_nsvc_by_nsvci(bind->nsi, nsvci);
987 if (nsvc) {
988 if (nsvc->persistent) {
Harald Weltef2949742021-01-20 14:54:14 +0100989 LOGNSVC(nsvc, LOGL_ERROR, "Received NS-RESET for a persistent NSE over wrong connection.\n");
Alexander Couzensba5a9922021-01-25 16:03:23 +0100990 return NS2_CS_SKIPPED;
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100991 }
992 /* destroy old dynamic nsvc */
993 gprs_ns2_free_nsvc(nsvc);
994 }
995
996 /* do nse persistent check late to be more precise on the error message */
997 if (nse->persistent) {
Harald Weltef2949742021-01-20 14:54:14 +0100998 LOGNSE(nse, LOGL_ERROR, "Received NS-RESET for a persistent NSE but the unknown "
999 "NS-VCI(%05u)\n", nsvci);
Alexander Couzensba5a9922021-01-25 16:03:23 +01001000 return NS2_CS_SKIPPED;
Alexander Couzens6a161492020-07-12 13:45:50 +02001001 }
1002
Harald Welte603f4042020-11-29 17:39:19 +01001003 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001004 vc_mode = ns2_dialect_to_vc_mode(dialect);
Harald Welte603f4042020-11-29 17:39:19 +01001005 snprintf(idbuf, sizeof(idbuf), "%s-NSE%05u-NSVC%05u", gprs_ns2_lltype_str(nse->ll),
1006 nse->nsei, nsvci);
1007 nsvc = ns2_vc_alloc(bind, nse, false, vc_mode, idbuf);
Alexander Couzens6a161492020-07-12 13:45:50 +02001008 if (!nsvc)
Alexander Couzensba5a9922021-01-25 16:03:23 +01001009 return NS2_CS_SKIPPED;
Alexander Couzens6a161492020-07-12 13:45:50 +02001010
Alexander Couzens6a161492020-07-12 13:45:50 +02001011 nsvc->nsvci = nsvci;
1012 nsvc->nsvci_is_valid = true;
1013
1014 *success = nsvc;
1015
Alexander Couzensba5a9922021-01-25 16:03:23 +01001016 return NS2_CS_CREATED;
Alexander Couzens6a161492020-07-12 13:45:50 +02001017}
1018
Harald Welte5bef2cc2020-09-18 22:33:24 +02001019/*! Create, and connect an inactive, new IP-based NS-VC
1020 * \param[in] bind bind in which the new NS-VC is to be created
1021 * \param[in] remote remote address to which to connect
1022 * \param[in] nse NS Entity in which the NS-VC is to be created
1023 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
1024 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001025struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +07001026 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +02001027 struct gprs_ns2_nse *nse,
1028 uint16_t nsvci)
1029{
1030 struct gprs_ns2_vc *nsvc;
1031
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001032 nsvc = ns2_ip_bind_connect(bind, nse, remote);
Alexander Couzens6a161492020-07-12 13:45:50 +02001033 if (!nsvc)
1034 return NULL;
1035
Alexander Couzens138b96f2021-01-25 16:23:29 +01001036 if (nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001037 nsvc->nsvci = nsvci;
1038 nsvc->nsvci_is_valid = true;
1039 }
1040
1041 return nsvc;
1042}
1043
Harald Welte5bef2cc2020-09-18 22:33:24 +02001044/*! Create, connect and activate a new IP-based NS-VC
1045 * \param[in] bind bind in which the new NS-VC is to be created
1046 * \param[in] remote remote address to which to connect
1047 * \param[in] nse NS Entity in which the NS-VC is to be created
1048 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
1049 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001050struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +07001051 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +02001052 struct gprs_ns2_nse *nse,
1053 uint16_t nsvci)
1054{
1055 struct gprs_ns2_vc *nsvc;
1056 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
1057 if (!nsvc)
1058 return NULL;
1059
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001060 ns2_vc_fsm_start(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +02001061
1062 return nsvc;
1063}
1064
Harald Welte5bef2cc2020-09-18 22:33:24 +02001065/*! Create, connect and activate a new IP-based NS-VC
1066 * \param[in] bind bind in which the new NS-VC is to be created
1067 * \param[in] remote remote address to which to connect
1068 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
1069 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
1070 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001071struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +07001072 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +02001073 uint16_t nsei,
Alexander Couzensd923cff2020-12-01 01:03:52 +01001074 uint16_t nsvci,
1075 enum gprs_ns2_dialect dialect)
Alexander Couzens6a161492020-07-12 13:45:50 +02001076{
1077 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
1078
1079 if (!nse) {
Alexander Couzensd923cff2020-12-01 01:03:52 +01001080 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP, dialect);
Alexander Couzens6a161492020-07-12 13:45:50 +02001081 if (!nse)
1082 return NULL;
1083 }
1084
1085 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
1086}
1087
Harald Welte5bef2cc2020-09-18 22:33:24 +02001088/*! Find NS-VC for given socket address.
1089 * \param[in] nse NS Entity in which to search
1090 * \param[in] sockaddr socket address to search for
1091 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +02001092struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +07001093 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +02001094{
1095 struct gprs_ns2_vc *nsvc;
Alexander Couzens9a4cf272020-10-11 20:48:04 +02001096 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +02001097
1098 OSMO_ASSERT(nse);
1099 OSMO_ASSERT(sockaddr);
1100
1101 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +02001102 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +02001103 if (!osmo_sockaddr_cmp(sockaddr, remote))
1104 return nsvc;
1105 }
1106
1107 return NULL;
1108}
1109
Alexander Couzens6cb5d5f2020-10-11 23:23:31 +02001110/*!
1111 * Iterate over all nsvc of a NS Entity and call the callback.
1112 * If the callback returns < 0 it aborts the loop and returns the callback return code.
1113 * \param[in] nse NS Entity to iterate over all nsvcs
1114 * \param[in] cb the callback to call
1115 * \param[inout] cb_data the private data of the callback
1116 * \return 0 if the loop completes. If a callback returns < 0 it will returns this value.
1117 */
1118int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data)
1119{
1120 struct gprs_ns2_vc *nsvc, *tmp;
1121 int rc = 0;
1122 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
1123 rc = cb(nsvc, cb_data);
1124 if (rc < 0)
1125 return rc;
1126 }
1127
1128 return 0;
1129}
1130
1131
Alexander Couzens6a161492020-07-12 13:45:50 +02001132
Harald Welte5bef2cc2020-09-18 22:33:24 +02001133/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +02001134 * \param[in] nsvc NS-VC for which the message was received
1135 * \param msg the received message. Ownership is trasnferred, caller must not free it!
1136 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +02001137int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +02001138 struct msgb *msg)
1139{
1140 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Alexander Couzensba634532021-01-25 13:46:38 +01001141 struct tlv_parsed tp = { };
Alexander Couzens6a161492020-07-12 13:45:50 +02001142 int rc = 0;
1143
Daniel Willmann751977b2020-12-02 18:59:44 +01001144 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
1145 log_set_context(LOG_CTX_GB_NSVC, nsvc);
1146
Harald Weltee5f55f72021-01-30 21:51:15 +01001147 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
1148 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msg->len);
1149
Alexander Couzens6a161492020-07-12 13:45:50 +02001150 if (msg->len < sizeof(struct gprs_ns_hdr))
1151 return -EINVAL;
1152
1153 switch (nsh->pdu_type) {
1154 case SNS_PDUT_CONFIG:
1155 /* one additional byte ('end flag') before the TLV part starts */
1156 rc = ns2_tlv_parse(&tp, nsh->data+1,
1157 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
1158 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001159 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001160 return rc;
1161 }
1162 /* All sub-network service related message types */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001163 rc = ns2_sns_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001164 break;
1165 case SNS_PDUT_ACK:
1166 case SNS_PDUT_ADD:
1167 case SNS_PDUT_CHANGE_WEIGHT:
1168 case SNS_PDUT_DELETE:
1169 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +02001170 rc = ns2_tlv_parse(&tp, nsh->data+5,
1171 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001172 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001173 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001174 return rc;
1175 }
1176 tp.lv[NS_IE_NSEI].val = nsh->data+2;
1177 tp.lv[NS_IE_NSEI].len = 2;
1178 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
1179 tp.lv[NS_IE_TRANS_ID].len = 1;
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001180 rc = ns2_sns_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001181 break;
1182 case SNS_PDUT_CONFIG_ACK:
1183 case SNS_PDUT_SIZE:
1184 case SNS_PDUT_SIZE_ACK:
1185 rc = ns2_tlv_parse(&tp, nsh->data,
1186 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1187 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001188 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001189 return rc;
1190 }
1191 /* All sub-network service related message types */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001192 rc = ns2_sns_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001193 break;
1194
1195 case NS_PDUT_UNITDATA:
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001196 rc = ns2_vc_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001197 break;
1198 default:
1199 rc = ns2_tlv_parse(&tp, nsh->data,
1200 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1201 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001202 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
Alexander Couzens6a161492020-07-12 13:45:50 +02001203 if (nsh->pdu_type != NS_PDUT_STATUS)
1204 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
1205 return rc;
1206 }
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001207 rc = ns2_vc_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001208 break;
1209 }
1210
1211 return rc;
1212}
1213
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001214/* summarize all active data nsvcs */
1215void ns2_nse_data_sum(struct gprs_ns2_nse *nse)
1216{
1217 struct gprs_ns2_vc *nsvc;
Harald Welte3221f872021-01-18 14:58:51 +01001218
Alexander Couzens8a2a1a42020-12-26 18:00:17 +01001219 nse->nsvc_count = 0;
Harald Welte3221f872021-01-18 14:58:51 +01001220 nse->sum_data_weight = 0;
1221 nse->sum_sig_weight = 0;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001222
1223 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001224 if (!ns2_vc_is_unblocked(nsvc))
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001225 continue;
Alexander Couzens8a2a1a42020-12-26 18:00:17 +01001226
1227 nse->nsvc_count++;
Harald Welte3221f872021-01-18 14:58:51 +01001228 nse->sum_data_weight += nsvc->data_weight;
1229 nse->sum_sig_weight += nsvc->sig_weight;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001230 }
1231}
1232
Harald Welte5bef2cc2020-09-18 22:33:24 +02001233/*! Notify a nse about the change of a NS-VC.
1234 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
1235 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +02001236void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
1237{
1238 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +02001239
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001240 ns2_sns_notify_alive(nse, nsvc, unblocked);
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001241 ns2_nse_data_sum(nse);
1242
Alexander Couzens6a161492020-07-12 13:45:50 +02001243 if (unblocked == nse->alive)
1244 return;
1245
Harald Welte3221f872021-01-18 14:58:51 +01001246 /* wait until both data_weight and sig_weight are != 0 before declaring NSE as alive */
1247 if (unblocked && nse->sum_data_weight && nse->sum_sig_weight) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001248 nse->alive = true;
Alexander Couzens138b96f2021-01-25 16:23:29 +01001249 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +02001250 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +02001251 return;
1252 }
1253
Harald Welte3221f872021-01-18 14:58:51 +01001254 if (nse->alive && (nse->sum_data_weight == 0 || nse->sum_sig_weight == 0)) {
1255 /* nse became unavailable */
1256 nse->alive = false;
Alexander Couzens138b96f2021-01-25 16:23:29 +01001257 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001258 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001259}
1260
1261/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +02001262 * \param[in] ctx a talloc context to allocate NS instance from
Alexander Couzenscce88282020-10-26 00:25:50 +01001263 * \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 +02001264 * \param[in] cb_data transparent user data passed to Call-back
1265 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001266struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
1267{
1268 struct gprs_ns2_inst *nsi;
1269
1270 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
1271 if (!nsi)
1272 return NULL;
1273
1274 nsi->cb = cb;
1275 nsi->cb_data = cb_data;
1276 INIT_LLIST_HEAD(&nsi->binding);
1277 INIT_LLIST_HEAD(&nsi->nse);
1278
1279 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1280 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1281 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1282 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1283 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1284 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1285 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
1286 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
Alexander Couzens90ee9632020-12-07 06:18:32 +01001287 nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES] = 3;
1288 nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES] = 3;
Alexander Couzens6a161492020-07-12 13:45:50 +02001289
1290 return nsi;
1291}
1292
Harald Welte5bef2cc2020-09-18 22:33:24 +02001293/*! Destroy a NS Instance (including all its NSEs, binds, ...).
1294 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001295void gprs_ns2_free(struct gprs_ns2_inst *nsi)
1296{
Alexander Couzens6a161492020-07-12 13:45:50 +02001297 if (!nsi)
1298 return;
1299
Alexander Couzens4b6c8af2020-10-11 20:15:25 +02001300 gprs_ns2_free_nses(nsi);
Alexander Couzens896fcd52020-10-11 19:52:36 +02001301 gprs_ns2_free_binds(nsi);
Alexander Couzens35315042020-10-10 03:28:17 +02001302
1303 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001304}
1305
Harald Welte5bef2cc2020-09-18 22:33:24 +02001306/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1307 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001308void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1309{
1310 struct gprs_ns2_vc *nsvc;
1311 OSMO_ASSERT(nse);
1312
1313 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1314 if (nsvc->sns_only)
1315 continue;
1316
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001317 ns2_vc_fsm_start(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +02001318 }
1319}
1320
Harald Welte5bef2cc2020-09-18 22:33:24 +02001321/*! Destroy a given bind.
1322 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001323void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1324{
1325 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001326 struct gprs_ns2_nse *nse;
Alexander Couzens6a161492020-07-12 13:45:50 +02001327 if (!bind)
1328 return;
1329
1330 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1331 gprs_ns2_free_nsvc(nsvc);
1332 }
1333
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001334 if (gprs_ns2_is_ip_bind(bind)) {
1335 llist_for_each_entry(nse, &bind->nsi->nse, list) {
1336 gprs_ns2_sns_del_bind(nse, bind);
1337 }
1338 }
1339
Alexander Couzens6a161492020-07-12 13:45:50 +02001340 if (bind->driver->free_bind)
1341 bind->driver->free_bind(bind);
1342
1343 llist_del(&bind->list);
Harald Welte76346072021-01-31 11:54:02 +01001344 osmo_stat_item_group_free(bind->statg);
Alexander Couzensaaa55a62020-12-03 06:02:03 +01001345 talloc_free((char *)bind->name);
Alexander Couzens6a161492020-07-12 13:45:50 +02001346 talloc_free(bind);
1347}
Alexander Couzens896fcd52020-10-11 19:52:36 +02001348
1349void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi)
1350{
1351 struct gprs_ns2_vc_bind *bind, *tbind;
1352
1353 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1354 gprs_ns2_free_bind(bind);
1355 }
1356}
Alexander Couzensd923cff2020-12-01 01:03:52 +01001357
Alexander Couzensaaa55a62020-12-03 06:02:03 +01001358/*! Search for a bind with a unique name
1359 * \param[in] nsi NS instance on which we operate
1360 * \param[in] name The unique bind name to search for
1361 * \return the bind or NULL if not found
1362 */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001363struct gprs_ns2_vc_bind *gprs_ns2_bind_by_name(struct gprs_ns2_inst *nsi, const char *name)
Alexander Couzensaaa55a62020-12-03 06:02:03 +01001364{
1365 struct gprs_ns2_vc_bind *bind;
1366
1367 llist_for_each_entry(bind, &nsi->binding, list) {
1368 if (!strcmp(bind->name, name))
1369 return bind;
1370 }
1371
1372 return NULL;
1373}
1374
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001375enum gprs_ns2_vc_mode ns2_dialect_to_vc_mode(enum gprs_ns2_dialect dialect)
Alexander Couzensd923cff2020-12-01 01:03:52 +01001376{
1377 switch (dialect) {
Alexander Couzens138b96f2021-01-25 16:23:29 +01001378 case GPRS_NS2_DIALECT_SNS:
1379 case GPRS_NS2_DIALECT_STATIC_ALIVE:
1380 return GPRS_NS2_VC_MODE_ALIVE;
1381 case GPRS_NS2_DIALECT_STATIC_RESETBLOCK:
1382 case GPRS_NS2_DIALECT_IPACCESS:
1383 return GPRS_NS2_VC_MODE_BLOCKRESET;
Alexander Couzensd923cff2020-12-01 01:03:52 +01001384 default:
1385 return -1;
1386 }
1387}
1388
Alexander Couzens1c8785d2020-12-17 06:58:53 +01001389static void add_bind_array(struct gprs_ns2_vc_bind **array,
1390 struct gprs_ns2_vc_bind *bind, int size)
1391{
1392 int i;
1393 for (i=0; i < size; i++) {
1394 if (array[i] == bind)
1395 return;
1396 if (!array[i])
1397 break;
1398 }
1399
1400 if (i == size)
1401 return;
1402
1403 array[i] = bind;
1404}
1405
Alexander Couzens4f1128f2021-01-20 17:42:48 +01001406void ns2_nse_update_mtu(struct gprs_ns2_nse *nse)
1407{
1408 struct gprs_ns2_vc *nsvc;
1409 int mtu = 0;
1410
1411 if (llist_empty(&nse->nsvc)) {
1412 nse->mtu = 0;
1413 return;
1414 }
1415
1416 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1417 if (mtu == 0)
1418 mtu = nsvc->bind->mtu;
1419 else if (mtu > nsvc->bind->mtu)
1420 mtu = nsvc->bind->mtu;
1421 }
1422
1423 if (nse->mtu == mtu)
1424 return;
1425
1426 nse->mtu = mtu;
1427 if (nse->alive)
1428 ns2_prim_status_ind(nsvc->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_MTU_CHANGE);
1429}
1430
Alexander Couzens1c8785d2020-12-17 06:58:53 +01001431/*! calculate the transfer capabilities for a nse
1432 * \param nse the nse to count the transfer capability
1433 * \param bvci a bvci - unused
1434 * \return the transfer capability in mbit. On error < 0.
1435 */
1436int ns2_count_transfer_cap(struct gprs_ns2_nse *nse,
1437 uint16_t bvci)
1438{
1439 struct gprs_ns2_vc *nsvc;
1440 struct gprs_ns2_vc_bind **active_binds;
1441 int i, active_nsvcs = 0, transfer_cap = 0;
1442
1443 /* calculate the transfer capabilities based on the binds.
1444 * A bind has a transfer capability which is shared across all NSVCs.
1445 * Take care the bind cap is not counted twice within a NSE.
1446 * This should be accurate for FR and UDP but not for FR/GRE. */
1447
1448 if (!nse->alive)
1449 return 0;
1450
1451 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001452 if (ns2_vc_is_unblocked(nsvc))
Alexander Couzens1c8785d2020-12-17 06:58:53 +01001453 active_nsvcs++;
1454 }
1455 /* an alive nse should always have active_nsvcs */
1456 OSMO_ASSERT(active_nsvcs);
1457
1458 active_binds = talloc_zero_array(nse, struct gprs_ns2_vc_bind*, active_nsvcs);
1459 if (!active_binds)
1460 return -ENOMEM;
1461
1462 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001463 if (!ns2_vc_is_unblocked(nsvc))
Alexander Couzens1c8785d2020-12-17 06:58:53 +01001464 continue;
1465 add_bind_array(active_binds, nsvc->bind, active_nsvcs);
1466 }
1467
1468 /* TODO: change calcuation for FR/GRE */
1469 for (i = 0; i < active_nsvcs; i++) {
1470 if (active_binds[i])
1471 transfer_cap += active_binds[i]->transfer_capability;
1472 }
1473
1474 talloc_free(active_binds);
1475 return transfer_cap;
1476}
1477
Harald Weltec3aa8f92021-01-31 11:41:34 +01001478/*! common allocation + low-level initialization of a bind. Called by vc-drivers */
1479int ns2_bind_alloc(struct gprs_ns2_inst *nsi, const char *name,
1480 struct gprs_ns2_vc_bind **result)
1481{
1482 struct gprs_ns2_vc_bind *bind;
1483
1484 if (!name)
1485 return -EINVAL;
1486
1487 if (gprs_ns2_bind_by_name(nsi, name))
1488 return -EALREADY;
1489
1490 bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
1491 if (!bind)
Harald Weltebdfb8b92021-01-31 11:44:57 +01001492 return -ENOMEM;
Harald Weltec3aa8f92021-01-31 11:41:34 +01001493
1494 bind->name = talloc_strdup(bind, name);
1495 if (!bind->name) {
1496 talloc_free(bind);
Harald Weltebdfb8b92021-01-31 11:44:57 +01001497 return -ENOMEM;
Harald Weltec3aa8f92021-01-31 11:41:34 +01001498 }
1499
Harald Welte76346072021-01-31 11:54:02 +01001500 bind->statg = osmo_stat_item_group_alloc(bind, &nsbind_statg_desc, nsi->bind_rate_ctr_idx);
1501 if (!bind->statg) {
1502 talloc_free(bind);
1503 return -ENOMEM;
1504 }
1505
Alexander Couzensc4704762021-02-08 23:13:12 +01001506 bind->sns_sig_weight = 1;
1507 bind->sns_data_weight = 1;
Harald Weltec3aa8f92021-01-31 11:41:34 +01001508 bind->nsi = nsi;
1509 INIT_LLIST_HEAD(&bind->nsvc);
1510 llist_add(&bind->list, &nsi->binding);
1511
Harald Welte76346072021-01-31 11:54:02 +01001512 nsi->bind_rate_ctr_idx++;
1513
Harald Weltec3aa8f92021-01-31 11:41:34 +01001514 if (result)
1515 *result = bind;
1516
1517 return 0;
1518}
1519
Alexander Couzens6a161492020-07-12 13:45:50 +02001520/*! @} */