blob: bc4db53ed9dc0de12e8695f6e22003b2b56f6ae4 [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 Couzens4f1128f2021-01-20 17:42:48 +0100220 { GPRS_NS2_AFF_CAUSE_MTU_CHANGE, "NSE MTU changed" },
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100221 { 0, NULL }
222};
223
Alexander Couzens0ab028c2020-11-04 02:41:44 +0100224const struct value_string gprs_ns2_prim_strs[] = {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100225 { GPRS_NS2_PRIM_UNIT_DATA, "UNIT DATA" },
226 { GPRS_NS2_PRIM_CONGESTION, "CONGESTION" },
227 { GPRS_NS2_PRIM_STATUS, "STATUS" },
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100228 { 0, NULL }
229};
230
Harald Weltea24e7ee2020-11-29 17:38:48 +0100231const struct value_string gprs_ns2_lltype_strs[] = {
232 { GPRS_NS2_LL_UDP, "UDP" },
233 { GPRS_NS2_LL_FR_GRE, "FR_GRE" },
234 { GPRS_NS2_LL_FR, "FR" },
235 { 0, NULL }
236};
237
Harald Welte5bef2cc2020-09-18 22:33:24 +0200238/*! string-format a given NS-VC into a user-supplied buffer.
239 * \param[in] buf user-allocated output buffer
240 * \param[in] buf_len size of user-allocated output buffer in bytes
241 * \param[in] nsvc NS-VC to be string-formatted
242 * \return pointer to buf on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200243char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc)
244{
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200245 const struct osmo_sockaddr *local;
246 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200247 struct osmo_sockaddr_str local_str;
248 struct osmo_sockaddr_str remote_str;
249
250 if (!buf_len)
Harald Welte92ad0292020-09-18 22:34:24 +0200251 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200252
Alexander Couzensaac90162020-11-19 02:44:04 +0100253 switch (nsvc->nse->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100254 case GPRS_NS2_LL_UDP:
Alexander Couzens6a161492020-07-12 13:45:50 +0200255 if (!gprs_ns2_is_ip_bind(nsvc->bind)) {
256 buf[0] = '\0';
257 return buf;
258 }
259
260 local = gprs_ns2_ip_bind_sockaddr(nsvc->bind);
Alexander Couzensc4229a42020-10-11 20:58:04 +0200261 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200262 if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas))
263 strcpy(local_str.ip, "invalid");
264 if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas))
265 strcpy(remote_str.ip, "invalid");
266
267 if (nsvc->nsvci_is_valid)
268 snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u",
269 local_str.ip, local_str.port,
270 nsvc->nsvci,
271 remote_str.ip, remote_str.port);
272 else
273 snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u",
274 local_str.ip, local_str.port,
275 remote_str.ip, remote_str.port);
276 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100277 case GPRS_NS2_LL_FR_GRE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200278 snprintf(buf, buf_len, "frgre)");
279 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100280 case GPRS_NS2_LL_FR:
Alexander Couzens841817e2020-11-19 00:41:29 +0100281 snprintf(buf, buf_len, "fr)netif: %s dlci: %u", gprs_ns2_fr_bind_netif(nsvc->bind),
282 gprs_ns2_fr_nsvc_dlci(nsvc));
283 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200284 default:
Harald Welte6188e002020-12-01 17:20:07 +0100285 snprintf(buf, buf_len, "unknown)");
Alexander Couzens6a161492020-07-12 13:45:50 +0200286 break;
287 }
288
289 buf[buf_len - 1] = '\0';
290
291 return buf;
292}
293
294/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
295#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
296
Harald Welte5bef2cc2020-09-18 22:33:24 +0200297/*! string-format a given NS-VC to a thread-local static buffer.
298 * \param[in] nsvc NS-VC to be string-formatted
299 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200300const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
301{
302 static __thread char buf[NS2_LL_MAX_STR];
303 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
304}
305
Harald Welte5bef2cc2020-09-18 22:33:24 +0200306/*! string-format a given NS-VC to a dynamically allocated string.
307 * \param[in] ctx talloc context from which to allocate
308 * \param[in] nsvc NS-VC to be string-formatted
309 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200310char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
311{
312 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
313 if (!buf)
314 return buf;
315 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
316}
317
Daniel Willmannf1286542020-11-03 23:03:33 +0100318/*! Return the current state name of a given NS-VC to a thread-local static buffer.
319 * \param[in] nsvc NS-VC to return the state of
320 * \return pointer to the string on success; NULL on error */
321const char *gprs_ns2_nsvc_state_name(struct gprs_ns2_vc *nsvc)
322{
323 return osmo_fsm_inst_state_name(nsvc->fi);
324}
325
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100326/* select a signalling NSVC and respect sig_counter
327 * param[out] reset_counter - all counter has to be resetted to their signal weight
328 * return the chosen nsvc or NULL
329 */
330static struct gprs_ns2_vc *ns2_load_sharing_signal(struct gprs_ns2_nse *nse)
331{
332 struct gprs_ns2_vc *nsvc = NULL, *last = NULL, *tmp;
333
334 llist_for_each_entry(tmp, &nse->nsvc, list) {
335 if (tmp->sig_weight == 0)
336 continue;
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100337 if (!ns2_vc_is_unblocked(tmp))
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100338 continue;
339 if (tmp->sig_counter == 0) {
340 last = tmp;
341 continue;
342 }
343
344 tmp->sig_counter--;
345 nsvc = tmp;
346 break;
347 }
348
349 /* all counter were zero, but there are valid nsvc */
350 if (!nsvc && last) {
351 llist_for_each_entry(tmp, &nse->nsvc, list) {
352 tmp->sig_counter = tmp->sig_weight;
353 }
354
355 last->sig_counter--;
356 return last;
357 } else {
358 return nsvc;
359 }
360}
361
362/* 4.4.1 Load Sharing function for the Frame Relay Sub-Network */
363static struct gprs_ns2_vc *ns2_load_sharing_modulor(
364 struct gprs_ns2_nse *nse,
365 uint16_t bvci,
366 uint32_t load_selector)
367{
368 struct gprs_ns2_vc *tmp;
Alexander Couzens265a5eb2021-02-02 11:26:17 +0100369 uint32_t mod;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100370 uint32_t i = 0;
371
Alexander Couzens265a5eb2021-02-02 11:26:17 +0100372 if (nse->nsvc_count == 0)
373 return NULL;
374
375 mod = (bvci + load_selector) % nse->nsvc_count;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100376 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100377 if (!ns2_vc_is_unblocked(tmp))
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100378 continue;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100379 if (i == mod)
380 return tmp;
381 i++;
382 }
383
384 return NULL;
385}
386
387/* pick the first available data NSVC - no load sharing */
388struct gprs_ns2_vc *ns2_load_sharing_first(struct gprs_ns2_nse *nse)
389{
390 struct gprs_ns2_vc *nsvc = NULL, *tmp;
391
392 llist_for_each_entry(tmp, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100393 if (!ns2_vc_is_unblocked(tmp))
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100394 continue;
395 if (tmp->data_weight == 0)
396 continue;
397
398 nsvc = tmp;
399 break;
400 }
401
402 return nsvc;
403}
404
405
406static struct gprs_ns2_vc *ns2_load_sharing(
407 struct gprs_ns2_nse *nse,
408 uint16_t bvci,
409 uint32_t link_selector)
410{
411 struct gprs_ns2_vc *nsvc = NULL;
412
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100413 switch (nse->ll) {
414 case GPRS_NS2_LL_FR:
415 nsvc = ns2_load_sharing_modulor(nse, bvci, link_selector);
416 break;
417 case GPRS_NS2_LL_UDP:
418 default:
419 if (bvci == 0) {
420 /* signalling */
421 nsvc = ns2_load_sharing_signal(nse);
422 } else {
423 /* data with load sharing parameter */
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100424 nsvc = ns2_load_sharing_first(nse);
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100425 }
Alexander Couzens8a2a1a42020-12-26 18:00:17 +0100426 break;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100427 }
428
429 return nsvc;
430}
431
Harald Welte5bef2cc2020-09-18 22:33:24 +0200432/*! Receive a primitive from the NS User (Gb).
433 * \param[in] nsi NS instance to which the primitive is issued
434 * \param[in] oph The primitive
435 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200436int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
437{
Alexander Couzens6a161492020-07-12 13:45:50 +0200438 /* TODO: implement resource distribution */
439 /* TODO: check for empty PDUs which can be sent to Request/Confirm
440 * the IP endpoint */
441 struct osmo_gprs_ns2_prim *nsp;
442 struct gprs_ns2_nse *nse = NULL;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100443 struct gprs_ns2_vc *nsvc = NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200444 uint16_t bvci, nsei;
445 uint8_t sducontrol = 0;
446
447 if (oph->sap != SAP_NS)
448 return -EINVAL;
449
450 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
451
Alexander Couzens138b96f2021-01-25 16:23:29 +0100452 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != GPRS_NS2_PRIM_UNIT_DATA)
Alexander Couzens6a161492020-07-12 13:45:50 +0200453 return -EINVAL;
454
455 if (!oph->msg)
456 return -EINVAL;
457
458 bvci = nsp->bvci;
459 nsei = nsp->nsei;
460
461 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
462 if (!nse)
463 return -EINVAL;
464
Alexander Couzens265a5eb2021-02-02 11:26:17 +0100465 if (!nse->alive)
466 return 0;
467
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100468 nsvc = ns2_load_sharing(nse, bvci, nsp->u.unitdata.link_selector);
Alexander Couzens6a161492020-07-12 13:45:50 +0200469
470 /* TODO: send a status primitive back */
471 if (!nsvc)
472 return 0;
473
Alexander Couzens138b96f2021-01-25 16:23:29 +0100474 if (nsp->u.unitdata.change == GPRS_NS2_ENDPOINT_REQUEST_CHANGE)
Alexander Couzens6a161492020-07-12 13:45:50 +0200475 sducontrol = 1;
Alexander Couzens138b96f2021-01-25 16:23:29 +0100476 else if (nsp->u.unitdata.change == GPRS_NS2_ENDPOINT_CONFIRM_CHANGE)
Alexander Couzens6a161492020-07-12 13:45:50 +0200477 sducontrol = 2;
478
479 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
480}
481
Harald Welte5bef2cc2020-09-18 22:33:24 +0200482/*! Send a STATUS.ind primitive to the specified NS instance user.
483 * \param[in] nsi NS instance on which we operate
484 * \param[in] nsei NSEI to which the statue relates
485 * \param[in] bvci BVCI to which the status relates
486 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200487void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100488 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200489 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200490 enum gprs_ns2_affecting_cause cause)
491{
Daniel Willmann15c09a82020-11-03 23:05:43 +0100492 char nsvc_str[NS2_LL_MAX_STR];
Alexander Couzens6a161492020-07-12 13:45:50 +0200493 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200494 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200495 nsp.bvci = bvci;
496 nsp.u.status.cause = cause;
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100497 nsp.u.status.transfer = ns2_count_transfer_cap(nse, bvci);
Alexander Couzensda0a2852020-10-01 23:24:07 +0200498 nsp.u.status.first = nse->first;
499 nsp.u.status.persistent = nse->persistent;
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100500 if (nse->mtu < 4)
501 nsp.u.status.mtu = 0;
502 else
503 nsp.u.status.mtu = nse->mtu - 4; /* 1 Byte NS PDU type, 1 Byte NS SDU control, 2 Byte BVCI */
504
Harald Welte86690432021-01-31 16:09:19 +0100505 if (nsvc) {
Daniel Willmann15c09a82020-11-03 23:05:43 +0100506 nsp.u.status.nsvc = gprs_ns2_ll_str_buf(nsvc_str, sizeof(nsvc_str), nsvc);
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100507 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 +0100508 nsp.bvci, gprs_ns2_aff_cause_prim_str(nsp.u.status.cause),
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100509 nsp.u.status.transfer, nsp.u.status.first, nse->mtu);
Harald Welte86690432021-01-31 16:09:19 +0100510 } else {
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100511 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 +0100512 nsp.bvci, gprs_ns2_aff_cause_prim_str(nsp.u.status.cause),
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100513 nsp.u.status.transfer, nsp.u.status.first, nse->mtu);
Harald Welte86690432021-01-31 16:09:19 +0100514 }
Daniel Willmann15c09a82020-11-03 23:05:43 +0100515
Harald Welte86690432021-01-31 16:09:19 +0100516 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_STATUS, PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200517 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200518}
519
Harald Welte5bef2cc2020-09-18 22:33:24 +0200520/*! Allocate a NS-VC within the given bind + NSE.
521 * \param[in] bind The 'bind' on which we operate
522 * \param[in] nse The NS Entity on which we operate
523 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
Harald Welte603f4042020-11-29 17:39:19 +0100524 * \param[in] id - human-readable identifier
Harald Welte5bef2cc2020-09-18 22:33:24 +0200525 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzensd923cff2020-12-01 01:03:52 +0100526struct 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 +0100527 enum gprs_ns2_vc_mode vc_mode, const char *id)
Alexander Couzens6a161492020-07-12 13:45:50 +0200528{
Daniel Willmannbb899052021-01-16 14:02:45 +0100529 /* Sanity check */
530 OSMO_ASSERT(bind->ll == nse->ll);
531
Alexander Couzens6a161492020-07-12 13:45:50 +0200532 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
533
534 if (!nsvc)
535 return NULL;
536
537 nsvc->bind = bind;
538 nsvc->nse = nse;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100539 nsvc->mode = vc_mode;
Alexander Couzens6a161492020-07-12 13:45:50 +0200540 nsvc->sig_weight = 1;
541 nsvc->data_weight = 1;
542
Harald Welte97ccbf72021-01-31 11:49:19 +0100543 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->nsvc_rate_ctr_idx);
Alexander Couzens6a161492020-07-12 13:45:50 +0200544 if (!nsvc->ctrg) {
545 goto err;
546 }
Harald Welte97ccbf72021-01-31 11:49:19 +0100547 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->nsvc_rate_ctr_idx);
Alexander Couzens6a161492020-07-12 13:45:50 +0200548 if (!nsvc->statg)
549 goto err_group;
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100550 if (!ns2_vc_fsm_alloc(nsvc, id, initiater))
Alexander Couzens6a161492020-07-12 13:45:50 +0200551 goto err_statg;
552
Harald Welte97ccbf72021-01-31 11:49:19 +0100553 bind->nsi->nsvc_rate_ctr_idx++;
Alexander Couzens6a161492020-07-12 13:45:50 +0200554
555 llist_add(&nsvc->list, &nse->nsvc);
556 llist_add(&nsvc->blist, &bind->nsvc);
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100557 ns2_nse_update_mtu(nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200558
559 return nsvc;
560
561err_statg:
562 osmo_stat_item_group_free(nsvc->statg);
563err_group:
564 rate_ctr_group_free(nsvc->ctrg);
565err:
566 talloc_free(nsvc);
567
568 return NULL;
569}
570
Harald Welte5bef2cc2020-09-18 22:33:24 +0200571/*! Destroy/release given NS-VC.
572 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200573void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
574{
575 if (!nsvc)
576 return;
577
Alexander Couzens138b96f2021-01-25 16:23:29 +0100578 ns2_prim_status_ind(nsvc->nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200579
580 llist_del(&nsvc->list);
581 llist_del(&nsvc->blist);
582
583 /* notify nse this nsvc is unavailable */
584 ns2_nse_notify_unblocked(nsvc, false);
585
586 /* check if sns is using this VC */
Alexander Couzensbe7cecc2021-02-03 18:25:27 +0100587 ns2_sns_replace_nsvc(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200588 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
589
590 /* let the driver/bind clean up it's internal state */
591 if (nsvc->priv && nsvc->bind->free_vc)
592 nsvc->bind->free_vc(nsvc);
593
594 osmo_stat_item_group_free(nsvc->statg);
595 rate_ctr_group_free(nsvc->ctrg);
596
597 talloc_free(nsvc);
598}
599
Alexander Couzens47558792020-12-06 03:16:11 +0100600/*! Destroy/release all NS-VC of given NSE
601 * \param[in] nse NSE
602 */
603void gprs_ns2_free_nsvcs(struct gprs_ns2_nse *nse)
604{
605 struct gprs_ns2_vc *nsvc, *tmp;
606
607 if (!nse)
608 return;
609
610 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
611 gprs_ns2_free_nsvc(nsvc);
612 }
613}
614
Harald Welte5bef2cc2020-09-18 22:33:24 +0200615/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100616struct msgb *ns2_msgb_alloc(void)
Alexander Couzens6a161492020-07-12 13:45:50 +0200617{
618 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
619 "GPRS/NS");
620 if (!msg) {
621 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
622 NS_ALLOC_SIZE);
623 }
624 return msg;
625}
626
Harald Welte5bef2cc2020-09-18 22:33:24 +0200627/*! Create a status message to be sent over a new connection.
628 * \param[in] orig_msg the original message
629 * \param[in] tp TLVP parsed of the original message
630 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
631 * \param[in] cause Cause for the rejection
632 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200633static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
634{
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100635 struct msgb *msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200636 struct gprs_ns_hdr *nsh;
637 bool have_vci = false;
638 uint8_t _cause = cause;
639 uint16_t nsei = 0;
640
641 if (!msg)
642 return -ENOMEM;
643
Harald Welte798efea2020-12-03 16:01:02 +0100644 if (TLVP_PRES_LEN(tp, NS_IE_NSEI, 2)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200645 nsei = tlvp_val16be(tp, NS_IE_NSEI);
646
647 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
648 nsei, gprs_ns2_cause_str(cause));
649 }
650
651 msg->l2h = msgb_put(msg, sizeof(*nsh));
652 nsh = (struct gprs_ns_hdr *) msg->l2h;
653 nsh->pdu_type = NS_PDUT_STATUS;
654
655 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
Harald Welte798efea2020-12-03 16:01:02 +0100656 have_vci = TLVP_PRES_LEN(tp, NS_IE_VCI, 2);
Alexander Couzens6a161492020-07-12 13:45:50 +0200657
658 /* Section 9.2.7.1: Static conditions for NS-VCI */
659 if (cause == NS_CAUSE_NSVC_BLOCKED ||
660 cause == NS_CAUSE_NSVC_UNKNOWN) {
661 if (!have_vci) {
662 msgb_free(msg);
663 return -EINVAL;
664 }
665
666 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
667 }
668
669 /* Section 9.2.7.2: Static conditions for NS PDU */
670 switch (cause) {
671 case NS_CAUSE_SEM_INCORR_PDU:
672 case NS_CAUSE_PDU_INCOMP_PSTATE:
673 case NS_CAUSE_PROTO_ERR_UNSPEC:
674 case NS_CAUSE_INVAL_ESSENT_IE:
675 case NS_CAUSE_MISSING_ESSENT_IE:
676 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
677 orig_msg->l2h);
678 break;
679 default:
680 break;
681 }
682
683 *reject = msg;
684 return 0;
685}
686
Harald Welte5bef2cc2020-09-18 22:33:24 +0200687/*! Resolve a NS Entity based on its NSEI.
688 * \param[in] nsi NS Instance in which we do the look-up
689 * \param[in] nsei NSEI to look up
690 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200691struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
692{
693 struct gprs_ns2_nse *nse;
694
695 llist_for_each_entry(nse, &nsi->nse, list) {
696 if (nse->nsei == nsei)
697 return nse;
698 }
699
700 return NULL;
701}
702
Harald Welte5bef2cc2020-09-18 22:33:24 +0200703/*! Resolve a NS-VC Entity based on its NS-VCI.
704 * \param[in] nsi NS Instance in which we do the look-up
705 * \param[in] nsvci NS-VCI to look up
706 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200707struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
708{
709 struct gprs_ns2_nse *nse;
710 struct gprs_ns2_vc *nsvc;
711
712 llist_for_each_entry(nse, &nsi->nse, list) {
713 llist_for_each_entry(nsvc, &nse->nsvc, list) {
714 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
715 return nsvc;
716 }
717 }
718
719 return NULL;
720}
721
Harald Welte5bef2cc2020-09-18 22:33:24 +0200722/*! Create a NS Entity within given NS instance.
723 * \param[in] nsi NS instance in which to create NS Entity
724 * \param[in] nsei NS Entity Identifier of to-be-created NSE
725 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzensd923cff2020-12-01 01:03:52 +0100726struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei,
727 enum gprs_ns2_ll linklayer, enum gprs_ns2_dialect dialect)
Alexander Couzens6a161492020-07-12 13:45:50 +0200728{
729 struct gprs_ns2_nse *nse;
Alexander Couzens93ad4992020-12-06 03:03:03 +0100730 char sns[16];
Alexander Couzens6a161492020-07-12 13:45:50 +0200731
732 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
733 if (nse) {
Harald Weltef2949742021-01-20 14:54:14 +0100734 LOGNSE(nse, LOGL_ERROR, "Can not create a NSE with already taken NSEI\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200735 return nse;
736 }
737
738 nse = talloc_zero(nsi, struct gprs_ns2_nse);
739 if (!nse)
740 return NULL;
741
Alexander Couzens138b96f2021-01-25 16:23:29 +0100742 if (dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens93ad4992020-12-06 03:03:03 +0100743 snprintf(sns, sizeof(sns), "NSE%05u-SNS", nsei);
744 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, sns);
745 if (!nse->bss_sns_fi) {
746 talloc_free(nse);
747 return NULL;
748 }
749 }
750
Alexander Couzensd923cff2020-12-01 01:03:52 +0100751 nse->dialect = dialect;
Alexander Couzensaac90162020-11-19 02:44:04 +0100752 nse->ll = linklayer;
Alexander Couzens6a161492020-07-12 13:45:50 +0200753 nse->nsei = nsei;
754 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200755 nse->first = true;
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100756 nse->mtu = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200757 llist_add(&nse->list, &nsi->nse);
758 INIT_LLIST_HEAD(&nse->nsvc);
759
760 return nse;
761}
762
Alexander Couzens05e7f7d2020-10-11 19:51:46 +0200763/*! Return the NSEI
764 * \param[in] nse NS Entity
765 * \return the nsei.
766 */
767uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse)
768{
769 return nse->nsei;
770}
771
Harald Welte5bef2cc2020-09-18 22:33:24 +0200772/*! Destroy given NS Entity.
773 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200774void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
775{
Alexander Couzens6a161492020-07-12 13:45:50 +0200776 if (!nse)
777 return;
778
Alexander Couzensd1cd6502021-01-15 02:16:23 +0100779 nse->alive = false;
Alexander Couzens47558792020-12-06 03:16:11 +0100780 gprs_ns2_free_nsvcs(nse);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100781 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200782
783 llist_del(&nse->list);
784 if (nse->bss_sns_fi)
785 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
786 talloc_free(nse);
787}
788
Alexander Couzens4b6c8af2020-10-11 20:15:25 +0200789void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi)
790{
791 struct gprs_ns2_nse *nse, *ntmp;
792
793 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
794 gprs_ns2_free_nse(nse);
795 }
796}
797
Alexander Couzens6a161492020-07-12 13:45:50 +0200798static inline int ns2_tlv_parse(struct tlv_parsed *dec,
799 const uint8_t *buf, int buf_len, uint8_t lv_tag,
800 uint8_t lv_tag2)
801{
802 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
803 * See comment of ns_att_tlvdef1. */
804 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
805 if (rc < 0)
806 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
807 return rc;
808}
809
810
Harald Welte5bef2cc2020-09-18 22:33:24 +0200811/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
812 * \param[in] bind the bind through which msg was received
813 * \param[in] msg the actual received message
814 * \param[in] logname A name to describe the VC. E.g. ip address pair
815 * \param[out] reject A message filled to be sent back. Only used in failure cases.
816 * \param[out] success A pointer which will be set to the new VC on success
817 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzensba5a9922021-01-25 16:03:23 +0100818enum ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
819 struct msgb *msg,
820 const char *logname,
821 struct msgb **reject,
822 struct gprs_ns2_vc **success)
Alexander Couzens6a161492020-07-12 13:45:50 +0200823{
824 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
825 struct tlv_parsed tp;
826 struct gprs_ns2_vc *nsvc;
827 struct gprs_ns2_nse *nse;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100828 enum gprs_ns2_dialect dialect;
829 enum gprs_ns2_vc_mode vc_mode;
Alexander Couzens6a161492020-07-12 13:45:50 +0200830 uint16_t nsvci;
831 uint16_t nsei;
Harald Welte603f4042020-11-29 17:39:19 +0100832 char idbuf[32];
Alexander Couzens6a161492020-07-12 13:45:50 +0200833
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100834 int rc, tlv;
Alexander Couzens6a161492020-07-12 13:45:50 +0200835
836 if (msg->len < sizeof(struct gprs_ns_hdr))
Alexander Couzensba5a9922021-01-25 16:03:23 +0100837 return NS2_CS_ERROR;
Alexander Couzens6a161492020-07-12 13:45:50 +0200838
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100839 /* parse the tlv early to allow reject status msg to
840 * work with valid tp.
841 * Ignore the return code until the pdu type is parsed because
842 * an unknown pdu type should be ignored */
843 tlv = ns2_tlv_parse(&tp, nsh->data,
Alexander Couzensbac5b012020-12-09 23:32:22 +0100844 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
Alexander Couzensbac5b012020-12-09 23:32:22 +0100845
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100846 switch (nsh->pdu_type) {
847 case NS_PDUT_STATUS:
Alexander Couzens6a161492020-07-12 13:45:50 +0200848 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
849 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
850 "for non-existing NS-VC\n",
851 logname);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100852 return NS2_CS_SKIPPED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100853 case NS_PDUT_ALIVE_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200854 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
855 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
856 "for non-existing NS-VC\n",
857 logname);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100858 return NS2_CS_SKIPPED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100859 case NS_PDUT_RESET_ACK:
Alexander Couzens6a161492020-07-12 13:45:50 +0200860 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
861 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
862 "for non-existing NS-VC\n",
863 logname);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100864 return NS2_CS_SKIPPED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100865 case NS_PDUT_RESET:
866 /* accept PDU RESET when vc_mode matches */
Alexander Couzensd923cff2020-12-01 01:03:52 +0100867 if (bind->accept_ipaccess) {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100868 dialect = GPRS_NS2_DIALECT_IPACCESS;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100869 break;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100870 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200871
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100872 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100873 if (rc < 0)
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100874 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100875 return NS2_CS_REJECTED;
Alexander Couzensd87a2f12020-11-30 22:20:20 +0100876 default:
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200877 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100878 if (rc < 0)
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200879 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100880 return NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200881 }
882
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100883 if (tlv < 0) {
884 /* TODO: correct behaviour would checking what's wrong.
885 * If it's an essential TLV for the PDU return NS_CAUSE_INVAL_ESSENT_IE.
886 * Otherwise ignore the non-essential TLV. */
887 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
888 "TLV Parse\n", tlv);
889 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PROTO_ERR_UNSPEC);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100890 if (rc < 0)
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100891 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100892 return NS2_CS_REJECTED;
Alexander Couzens12e5e6b2020-12-10 00:18:17 +0100893 }
894
Harald Welte798efea2020-12-03 16:01:02 +0100895 if (!TLVP_PRES_LEN(&tp, NS_IE_CAUSE, 1) ||
896 !TLVP_PRES_LEN(&tp, NS_IE_VCI, 2) || !TLVP_PRES_LEN(&tp, NS_IE_NSEI, 2)) {
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200897 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
898 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
Alexander Couzens3c22f912020-12-10 00:20:18 +0100899 if (rc < 0)
900 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100901 return NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200902 }
903
Alexander Couzens6a161492020-07-12 13:45:50 +0200904 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100905 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
906
907 /* find or create NSE */
Alexander Couzens6a161492020-07-12 13:45:50 +0200908 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
909 if (!nse) {
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100910 /* only create nse for udp & ipaccess */
Alexander Couzens138b96f2021-01-25 16:23:29 +0100911 if (bind->ll != GPRS_NS2_LL_UDP || dialect != GPRS_NS2_DIALECT_IPACCESS)
Alexander Couzensba5a9922021-01-25 16:03:23 +0100912 return NS2_CS_SKIPPED;
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100913
Alexander Couzens2e1a3a92021-01-27 20:44:41 +0100914 if (!bind->accept_ipaccess)
Alexander Couzensba5a9922021-01-25 16:03:23 +0100915 return NS2_CS_SKIPPED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200916
Alexander Couzensd923cff2020-12-01 01:03:52 +0100917 nse = gprs_ns2_create_nse(bind->nsi, nsei, bind->ll, dialect);
Alexander Couzens6a161492020-07-12 13:45:50 +0200918 if (!nse) {
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100919 LOGP(DLNS, LOGL_ERROR, "Failed to create NSE(%05u)\n", nsei);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100920 return NS2_CS_ERROR;
Alexander Couzens6a161492020-07-12 13:45:50 +0200921 }
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100922 } else {
923 /* nsei already known */
924 if (nse->ll != bind->ll) {
Harald Weltef2949742021-01-20 14:54:14 +0100925 LOGNSE(nse, LOGL_ERROR, "Received NS-RESET NS-VCI(%05u) with wrong linklayer(%s)"
926 " for already known NSE(%s)\n", nsvci, gprs_ns2_lltype_str(bind->ll),
927 gprs_ns2_lltype_str(nse->ll));
Alexander Couzensba5a9922021-01-25 16:03:23 +0100928 return NS2_CS_SKIPPED;
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100929 }
930 }
931
932 nsvc = gprs_ns2_nsvc_by_nsvci(bind->nsi, nsvci);
933 if (nsvc) {
934 if (nsvc->persistent) {
Harald Weltef2949742021-01-20 14:54:14 +0100935 LOGNSVC(nsvc, LOGL_ERROR, "Received NS-RESET for a persistent NSE over wrong connection.\n");
Alexander Couzensba5a9922021-01-25 16:03:23 +0100936 return NS2_CS_SKIPPED;
Alexander Couzens05ac4e52021-01-04 19:07:05 +0100937 }
938 /* destroy old dynamic nsvc */
939 gprs_ns2_free_nsvc(nsvc);
940 }
941
942 /* do nse persistent check late to be more precise on the error message */
943 if (nse->persistent) {
Harald Weltef2949742021-01-20 14:54:14 +0100944 LOGNSE(nse, LOGL_ERROR, "Received NS-RESET for a persistent NSE but the unknown "
945 "NS-VCI(%05u)\n", nsvci);
Alexander Couzensba5a9922021-01-25 16:03:23 +0100946 return NS2_CS_SKIPPED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200947 }
948
Harald Welte603f4042020-11-29 17:39:19 +0100949 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100950 vc_mode = ns2_dialect_to_vc_mode(dialect);
Harald Welte603f4042020-11-29 17:39:19 +0100951 snprintf(idbuf, sizeof(idbuf), "%s-NSE%05u-NSVC%05u", gprs_ns2_lltype_str(nse->ll),
952 nse->nsei, nsvci);
953 nsvc = ns2_vc_alloc(bind, nse, false, vc_mode, idbuf);
Alexander Couzens6a161492020-07-12 13:45:50 +0200954 if (!nsvc)
Alexander Couzensba5a9922021-01-25 16:03:23 +0100955 return NS2_CS_SKIPPED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200956
Alexander Couzens6a161492020-07-12 13:45:50 +0200957 nsvc->nsvci = nsvci;
958 nsvc->nsvci_is_valid = true;
959
960 *success = nsvc;
961
Alexander Couzensba5a9922021-01-25 16:03:23 +0100962 return NS2_CS_CREATED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200963}
964
Harald Welte5bef2cc2020-09-18 22:33:24 +0200965/*! Create, and connect an inactive, new IP-based NS-VC
966 * \param[in] bind bind in which the new NS-VC is to be created
967 * \param[in] remote remote address to which to connect
968 * \param[in] nse NS Entity in which the NS-VC is to be created
969 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
970 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200971struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700972 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200973 struct gprs_ns2_nse *nse,
974 uint16_t nsvci)
975{
976 struct gprs_ns2_vc *nsvc;
977
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100978 nsvc = ns2_ip_bind_connect(bind, nse, remote);
Alexander Couzens6a161492020-07-12 13:45:50 +0200979 if (!nsvc)
980 return NULL;
981
Alexander Couzens138b96f2021-01-25 16:23:29 +0100982 if (nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200983 nsvc->nsvci = nsvci;
984 nsvc->nsvci_is_valid = true;
985 }
986
987 return nsvc;
988}
989
Harald Welte5bef2cc2020-09-18 22:33:24 +0200990/*! Create, connect and activate a new IP-based NS-VC
991 * \param[in] bind bind in which the new NS-VC is to be created
992 * \param[in] remote remote address to which to connect
993 * \param[in] nse NS Entity in which the NS-VC is to be created
994 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
995 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200996struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700997 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200998 struct gprs_ns2_nse *nse,
999 uint16_t nsvci)
1000{
1001 struct gprs_ns2_vc *nsvc;
1002 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
1003 if (!nsvc)
1004 return NULL;
1005
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001006 ns2_vc_fsm_start(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +02001007
1008 return nsvc;
1009}
1010
Harald Welte5bef2cc2020-09-18 22:33:24 +02001011/*! Create, connect and activate a new IP-based NS-VC
1012 * \param[in] bind bind in which the new NS-VC is to be created
1013 * \param[in] remote remote address to which to connect
1014 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
1015 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
1016 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001017struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +07001018 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +02001019 uint16_t nsei,
Alexander Couzensd923cff2020-12-01 01:03:52 +01001020 uint16_t nsvci,
1021 enum gprs_ns2_dialect dialect)
Alexander Couzens6a161492020-07-12 13:45:50 +02001022{
1023 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
1024
1025 if (!nse) {
Alexander Couzensd923cff2020-12-01 01:03:52 +01001026 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP, dialect);
Alexander Couzens6a161492020-07-12 13:45:50 +02001027 if (!nse)
1028 return NULL;
1029 }
1030
1031 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
1032}
1033
Harald Welte5bef2cc2020-09-18 22:33:24 +02001034/*! Find NS-VC for given socket address.
1035 * \param[in] nse NS Entity in which to search
1036 * \param[in] sockaddr socket address to search for
1037 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +02001038struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +07001039 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +02001040{
1041 struct gprs_ns2_vc *nsvc;
Alexander Couzens9a4cf272020-10-11 20:48:04 +02001042 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +02001043
1044 OSMO_ASSERT(nse);
1045 OSMO_ASSERT(sockaddr);
1046
1047 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +02001048 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +02001049 if (!osmo_sockaddr_cmp(sockaddr, remote))
1050 return nsvc;
1051 }
1052
1053 return NULL;
1054}
1055
Alexander Couzens6cb5d5f2020-10-11 23:23:31 +02001056/*!
1057 * Iterate over all nsvc of a NS Entity and call the callback.
1058 * If the callback returns < 0 it aborts the loop and returns the callback return code.
1059 * \param[in] nse NS Entity to iterate over all nsvcs
1060 * \param[in] cb the callback to call
1061 * \param[inout] cb_data the private data of the callback
1062 * \return 0 if the loop completes. If a callback returns < 0 it will returns this value.
1063 */
1064int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data)
1065{
1066 struct gprs_ns2_vc *nsvc, *tmp;
1067 int rc = 0;
1068 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
1069 rc = cb(nsvc, cb_data);
1070 if (rc < 0)
1071 return rc;
1072 }
1073
1074 return 0;
1075}
1076
1077
Alexander Couzens6a161492020-07-12 13:45:50 +02001078
Harald Welte5bef2cc2020-09-18 22:33:24 +02001079/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +02001080 * \param[in] nsvc NS-VC for which the message was received
1081 * \param msg the received message. Ownership is trasnferred, caller must not free it!
1082 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +02001083int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +02001084 struct msgb *msg)
1085{
1086 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
Alexander Couzensba634532021-01-25 13:46:38 +01001087 struct tlv_parsed tp = { };
Alexander Couzens6a161492020-07-12 13:45:50 +02001088 int rc = 0;
1089
Daniel Willmann751977b2020-12-02 18:59:44 +01001090 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
1091 log_set_context(LOG_CTX_GB_NSVC, nsvc);
1092
Harald Weltee5f55f72021-01-30 21:51:15 +01001093 rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]);
1094 rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msg->len);
1095
Alexander Couzens6a161492020-07-12 13:45:50 +02001096 if (msg->len < sizeof(struct gprs_ns_hdr))
1097 return -EINVAL;
1098
1099 switch (nsh->pdu_type) {
1100 case SNS_PDUT_CONFIG:
1101 /* one additional byte ('end flag') before the TLV part starts */
1102 rc = ns2_tlv_parse(&tp, nsh->data+1,
1103 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
1104 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001105 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001106 return rc;
1107 }
1108 /* All sub-network service related message types */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001109 rc = ns2_sns_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001110 break;
1111 case SNS_PDUT_ACK:
1112 case SNS_PDUT_ADD:
1113 case SNS_PDUT_CHANGE_WEIGHT:
1114 case SNS_PDUT_DELETE:
1115 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +02001116 rc = ns2_tlv_parse(&tp, nsh->data+5,
1117 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001118 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001119 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001120 return rc;
1121 }
1122 tp.lv[NS_IE_NSEI].val = nsh->data+2;
1123 tp.lv[NS_IE_NSEI].len = 2;
1124 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
1125 tp.lv[NS_IE_TRANS_ID].len = 1;
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001126 rc = ns2_sns_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001127 break;
1128 case SNS_PDUT_CONFIG_ACK:
1129 case SNS_PDUT_SIZE:
1130 case SNS_PDUT_SIZE_ACK:
1131 rc = ns2_tlv_parse(&tp, nsh->data,
1132 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1133 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001134 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001135 return rc;
1136 }
1137 /* All sub-network service related message types */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001138 rc = ns2_sns_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001139 break;
1140
1141 case NS_PDUT_UNITDATA:
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001142 rc = ns2_vc_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001143 break;
1144 default:
1145 rc = ns2_tlv_parse(&tp, nsh->data,
1146 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1147 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001148 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
Alexander Couzens6a161492020-07-12 13:45:50 +02001149 if (nsh->pdu_type != NS_PDUT_STATUS)
1150 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
1151 return rc;
1152 }
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001153 rc = ns2_vc_rx(nsvc, msg, &tp);
Alexander Couzens6a161492020-07-12 13:45:50 +02001154 break;
1155 }
1156
1157 return rc;
1158}
1159
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001160/* summarize all active data nsvcs */
1161void ns2_nse_data_sum(struct gprs_ns2_nse *nse)
1162{
1163 struct gprs_ns2_vc *nsvc;
Harald Welte3221f872021-01-18 14:58:51 +01001164
Alexander Couzens8a2a1a42020-12-26 18:00:17 +01001165 nse->nsvc_count = 0;
Harald Welte3221f872021-01-18 14:58:51 +01001166 nse->sum_data_weight = 0;
1167 nse->sum_sig_weight = 0;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001168
1169 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001170 if (!ns2_vc_is_unblocked(nsvc))
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001171 continue;
Alexander Couzens8a2a1a42020-12-26 18:00:17 +01001172
1173 nse->nsvc_count++;
Harald Welte3221f872021-01-18 14:58:51 +01001174 nse->sum_data_weight += nsvc->data_weight;
1175 nse->sum_sig_weight += nsvc->sig_weight;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001176 }
1177}
1178
Harald Welte5bef2cc2020-09-18 22:33:24 +02001179/*! Notify a nse about the change of a NS-VC.
1180 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
1181 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +02001182void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
1183{
1184 struct gprs_ns2_nse *nse = nsvc->nse;
Alexander Couzens6a161492020-07-12 13:45:50 +02001185
Alexander Couzensbe7cecc2021-02-03 18:25:27 +01001186 ns2_sns_notify_alive(nse, nsvc, unblocked);
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001187 ns2_nse_data_sum(nse);
1188
Alexander Couzens6a161492020-07-12 13:45:50 +02001189 if (unblocked == nse->alive)
1190 return;
1191
Harald Welte3221f872021-01-18 14:58:51 +01001192 /* wait until both data_weight and sig_weight are != 0 before declaring NSE as alive */
1193 if (unblocked && nse->sum_data_weight && nse->sum_sig_weight) {
Alexander Couzens6a161492020-07-12 13:45:50 +02001194 nse->alive = true;
Alexander Couzens138b96f2021-01-25 16:23:29 +01001195 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +02001196 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +02001197 return;
1198 }
1199
Harald Welte3221f872021-01-18 14:58:51 +01001200 if (nse->alive && (nse->sum_data_weight == 0 || nse->sum_sig_weight == 0)) {
1201 /* nse became unavailable */
1202 nse->alive = false;
Alexander Couzens138b96f2021-01-25 16:23:29 +01001203 ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001204 }
Alexander Couzens6a161492020-07-12 13:45:50 +02001205}
1206
1207/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +02001208 * \param[in] ctx a talloc context to allocate NS instance from
Alexander Couzenscce88282020-10-26 00:25:50 +01001209 * \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 +02001210 * \param[in] cb_data transparent user data passed to Call-back
1211 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001212struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
1213{
1214 struct gprs_ns2_inst *nsi;
1215
1216 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
1217 if (!nsi)
1218 return NULL;
1219
1220 nsi->cb = cb;
1221 nsi->cb_data = cb_data;
1222 INIT_LLIST_HEAD(&nsi->binding);
1223 INIT_LLIST_HEAD(&nsi->nse);
1224
1225 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1226 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1227 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1228 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1229 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1230 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1231 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
1232 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
Alexander Couzens90ee9632020-12-07 06:18:32 +01001233 nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES] = 3;
1234 nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES] = 3;
Alexander Couzens6a161492020-07-12 13:45:50 +02001235
1236 return nsi;
1237}
1238
Harald Welte5bef2cc2020-09-18 22:33:24 +02001239/*! Destroy a NS Instance (including all its NSEs, binds, ...).
1240 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001241void gprs_ns2_free(struct gprs_ns2_inst *nsi)
1242{
Alexander Couzens6a161492020-07-12 13:45:50 +02001243 if (!nsi)
1244 return;
1245
Alexander Couzens4b6c8af2020-10-11 20:15:25 +02001246 gprs_ns2_free_nses(nsi);
Alexander Couzens896fcd52020-10-11 19:52:36 +02001247 gprs_ns2_free_binds(nsi);
Alexander Couzens35315042020-10-10 03:28:17 +02001248
1249 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001250}
1251
Harald Welte5bef2cc2020-09-18 22:33:24 +02001252/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1253 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001254void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1255{
1256 struct gprs_ns2_vc *nsvc;
1257 OSMO_ASSERT(nse);
1258
1259 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1260 if (nsvc->sns_only)
1261 continue;
1262
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001263 ns2_vc_fsm_start(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +02001264 }
1265}
1266
Harald Welte5bef2cc2020-09-18 22:33:24 +02001267/*! Destroy a given bind.
1268 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001269void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1270{
1271 struct gprs_ns2_vc *nsvc, *tmp;
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001272 struct gprs_ns2_nse *nse;
Alexander Couzens6a161492020-07-12 13:45:50 +02001273 if (!bind)
1274 return;
1275
1276 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1277 gprs_ns2_free_nsvc(nsvc);
1278 }
1279
Alexander Couzens6b9d2322021-02-12 03:17:59 +01001280 if (gprs_ns2_is_ip_bind(bind)) {
1281 llist_for_each_entry(nse, &bind->nsi->nse, list) {
1282 gprs_ns2_sns_del_bind(nse, bind);
1283 }
1284 }
1285
Alexander Couzens6a161492020-07-12 13:45:50 +02001286 if (bind->driver->free_bind)
1287 bind->driver->free_bind(bind);
1288
1289 llist_del(&bind->list);
Harald Welte76346072021-01-31 11:54:02 +01001290 osmo_stat_item_group_free(bind->statg);
Alexander Couzensaaa55a62020-12-03 06:02:03 +01001291 talloc_free((char *)bind->name);
Alexander Couzens6a161492020-07-12 13:45:50 +02001292 talloc_free(bind);
1293}
Alexander Couzens896fcd52020-10-11 19:52:36 +02001294
1295void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi)
1296{
1297 struct gprs_ns2_vc_bind *bind, *tbind;
1298
1299 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1300 gprs_ns2_free_bind(bind);
1301 }
1302}
Alexander Couzensd923cff2020-12-01 01:03:52 +01001303
Alexander Couzensaaa55a62020-12-03 06:02:03 +01001304/*! Search for a bind with a unique name
1305 * \param[in] nsi NS instance on which we operate
1306 * \param[in] name The unique bind name to search for
1307 * \return the bind or NULL if not found
1308 */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001309struct gprs_ns2_vc_bind *gprs_ns2_bind_by_name(struct gprs_ns2_inst *nsi, const char *name)
Alexander Couzensaaa55a62020-12-03 06:02:03 +01001310{
1311 struct gprs_ns2_vc_bind *bind;
1312
1313 llist_for_each_entry(bind, &nsi->binding, list) {
1314 if (!strcmp(bind->name, name))
1315 return bind;
1316 }
1317
1318 return NULL;
1319}
1320
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001321enum gprs_ns2_vc_mode ns2_dialect_to_vc_mode(enum gprs_ns2_dialect dialect)
Alexander Couzensd923cff2020-12-01 01:03:52 +01001322{
1323 switch (dialect) {
Alexander Couzens138b96f2021-01-25 16:23:29 +01001324 case GPRS_NS2_DIALECT_SNS:
1325 case GPRS_NS2_DIALECT_STATIC_ALIVE:
1326 return GPRS_NS2_VC_MODE_ALIVE;
1327 case GPRS_NS2_DIALECT_STATIC_RESETBLOCK:
1328 case GPRS_NS2_DIALECT_IPACCESS:
1329 return GPRS_NS2_VC_MODE_BLOCKRESET;
Alexander Couzensd923cff2020-12-01 01:03:52 +01001330 default:
1331 return -1;
1332 }
1333}
1334
Alexander Couzens1c8785d2020-12-17 06:58:53 +01001335static void add_bind_array(struct gprs_ns2_vc_bind **array,
1336 struct gprs_ns2_vc_bind *bind, int size)
1337{
1338 int i;
1339 for (i=0; i < size; i++) {
1340 if (array[i] == bind)
1341 return;
1342 if (!array[i])
1343 break;
1344 }
1345
1346 if (i == size)
1347 return;
1348
1349 array[i] = bind;
1350}
1351
Alexander Couzens4f1128f2021-01-20 17:42:48 +01001352void ns2_nse_update_mtu(struct gprs_ns2_nse *nse)
1353{
1354 struct gprs_ns2_vc *nsvc;
1355 int mtu = 0;
1356
1357 if (llist_empty(&nse->nsvc)) {
1358 nse->mtu = 0;
1359 return;
1360 }
1361
1362 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1363 if (mtu == 0)
1364 mtu = nsvc->bind->mtu;
1365 else if (mtu > nsvc->bind->mtu)
1366 mtu = nsvc->bind->mtu;
1367 }
1368
1369 if (nse->mtu == mtu)
1370 return;
1371
1372 nse->mtu = mtu;
1373 if (nse->alive)
1374 ns2_prim_status_ind(nsvc->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_MTU_CHANGE);
1375}
1376
Alexander Couzens1c8785d2020-12-17 06:58:53 +01001377/*! calculate the transfer capabilities for a nse
1378 * \param nse the nse to count the transfer capability
1379 * \param bvci a bvci - unused
1380 * \return the transfer capability in mbit. On error < 0.
1381 */
1382int ns2_count_transfer_cap(struct gprs_ns2_nse *nse,
1383 uint16_t bvci)
1384{
1385 struct gprs_ns2_vc *nsvc;
1386 struct gprs_ns2_vc_bind **active_binds;
1387 int i, active_nsvcs = 0, transfer_cap = 0;
1388
1389 /* calculate the transfer capabilities based on the binds.
1390 * A bind has a transfer capability which is shared across all NSVCs.
1391 * Take care the bind cap is not counted twice within a NSE.
1392 * This should be accurate for FR and UDP but not for FR/GRE. */
1393
1394 if (!nse->alive)
1395 return 0;
1396
1397 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001398 if (ns2_vc_is_unblocked(nsvc))
Alexander Couzens1c8785d2020-12-17 06:58:53 +01001399 active_nsvcs++;
1400 }
1401 /* an alive nse should always have active_nsvcs */
1402 OSMO_ASSERT(active_nsvcs);
1403
1404 active_binds = talloc_zero_array(nse, struct gprs_ns2_vc_bind*, active_nsvcs);
1405 if (!active_binds)
1406 return -ENOMEM;
1407
1408 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzens8dfc24c2021-01-25 16:09:23 +01001409 if (!ns2_vc_is_unblocked(nsvc))
Alexander Couzens1c8785d2020-12-17 06:58:53 +01001410 continue;
1411 add_bind_array(active_binds, nsvc->bind, active_nsvcs);
1412 }
1413
1414 /* TODO: change calcuation for FR/GRE */
1415 for (i = 0; i < active_nsvcs; i++) {
1416 if (active_binds[i])
1417 transfer_cap += active_binds[i]->transfer_capability;
1418 }
1419
1420 talloc_free(active_binds);
1421 return transfer_cap;
1422}
1423
Harald Weltec3aa8f92021-01-31 11:41:34 +01001424/*! common allocation + low-level initialization of a bind. Called by vc-drivers */
1425int ns2_bind_alloc(struct gprs_ns2_inst *nsi, const char *name,
1426 struct gprs_ns2_vc_bind **result)
1427{
1428 struct gprs_ns2_vc_bind *bind;
1429
1430 if (!name)
1431 return -EINVAL;
1432
1433 if (gprs_ns2_bind_by_name(nsi, name))
1434 return -EALREADY;
1435
1436 bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
1437 if (!bind)
Harald Weltebdfb8b92021-01-31 11:44:57 +01001438 return -ENOMEM;
Harald Weltec3aa8f92021-01-31 11:41:34 +01001439
1440 bind->name = talloc_strdup(bind, name);
1441 if (!bind->name) {
1442 talloc_free(bind);
Harald Weltebdfb8b92021-01-31 11:44:57 +01001443 return -ENOMEM;
Harald Weltec3aa8f92021-01-31 11:41:34 +01001444 }
1445
Harald Welte76346072021-01-31 11:54:02 +01001446 bind->statg = osmo_stat_item_group_alloc(bind, &nsbind_statg_desc, nsi->bind_rate_ctr_idx);
1447 if (!bind->statg) {
1448 talloc_free(bind);
1449 return -ENOMEM;
1450 }
1451
Alexander Couzensc4704762021-02-08 23:13:12 +01001452 bind->sns_sig_weight = 1;
1453 bind->sns_data_weight = 1;
Harald Weltec3aa8f92021-01-31 11:41:34 +01001454 bind->nsi = nsi;
1455 INIT_LLIST_HEAD(&bind->nsvc);
1456 llist_add(&bind->list, &nsi->binding);
1457
Harald Welte76346072021-01-31 11:54:02 +01001458 nsi->bind_rate_ctr_idx++;
1459
Harald Weltec3aa8f92021-01-31 11:41:34 +01001460 if (result)
1461 *result = bind;
1462
1463 return 0;
1464}
1465
Alexander Couzens6a161492020-07-12 13:45:50 +02001466/*! @} */