blob: 30716fc3d6f644af08514e95efaeb51d3d006e44 [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>
75#include <osmocom/core/msgb.h>
76#include <osmocom/core/rate_ctr.h>
77#include <osmocom/core/socket.h>
78#include <osmocom/core/sockaddr_str.h>
79#include <osmocom/core/stats.h>
80#include <osmocom/core/stat_item.h>
81#include <osmocom/core/talloc.h>
82#include <osmocom/gprs/gprs_msgb.h>
83#include <osmocom/gsm/prim.h>
84#include <osmocom/gsm/tlv.h>
85
86#include "gprs_ns2_internal.h"
87
88#define ns_set_state(ns_, st_) ns_set_state_with_log(ns_, st_, false, __FILE__, __LINE__)
89#define ns_set_remote_state(ns_, st_) ns_set_state_with_log(ns_, st_, true, __FILE__, __LINE__)
90#define ns_mark_blocked(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_BLOCKED)
91#define ns_mark_unblocked(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_BLOCKED));
92#define ns_mark_alive(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_ALIVE)
93#define ns_mark_dead(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_ALIVE));
94
95/* HACK: The NS_IE_IP_ADDR does not follow any known TLV rules.
96 * Since it's a hard ABI break to implement 16 bit tag with fixed length entries to workaround it,
97 * the parser will be called with ns_att_tlvdef1 and if it's failed with ns_att_tlvdef2.
98 * The TLV parser depends on 8bit tag in many places.
99 * The NS_IE_IP_ADDR is only valid for SNS_ACK SNS_ADD and SNS_DELETE.
100 */
101static const struct tlv_definition ns_att_tlvdef1 = {
102 .def = {
103 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
104 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
105 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
106 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
107 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
108 [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 },
109 [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 },
110 [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 },
111 [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 },
112 [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 },
113 [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 },
114 /* NS_IE_IP_ADDR in the IPv4 version */
115 [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 5 },
116 },
117};
118
119static const struct tlv_definition ns_att_tlvdef2 = {
120 .def = {
121 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
122 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
123 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
124 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
125 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
126 [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 },
127 [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 },
128 [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 },
129 [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 },
130 [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 },
131 [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 },
132 /* NS_IE_IP_ADDR in the IPv6 version */
133 [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 17 },
134 },
135};
136
137
138/* Section 10.3.2, Table 13 */
Alexander Couzensb3b837c2020-10-27 15:12:25 +0100139const struct value_string gprs_ns2_cause_strs[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200140 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
141 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
142 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
143 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
144 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
145 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
146 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
147 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
148 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
149 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
150 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
151 { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" },
152 { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" },
153 { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" },
154 { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" },
155 { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" },
156 { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" },
157 { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" },
158 { 0, NULL }
159};
160
Alexander Couzens6a161492020-07-12 13:45:50 +0200161static const struct rate_ctr_desc nsvc_ctr_description[] = {
162 { "packets:in", "Packets at NS Level ( In)" },
163 { "packets:out","Packets at NS Level (Out)" },
164 { "bytes:in", "Bytes at NS Level ( In)" },
165 { "bytes:out", "Bytes at NS Level (Out)" },
166 { "blocked", "NS-VC Block count " },
167 { "dead", "NS-VC gone dead count " },
168 { "replaced", "NS-VC replaced other count" },
169 { "nsei-chg", "NS-VC changed NSEI count " },
170 { "inv-nsvci", "NS-VCI was invalid count " },
171 { "inv-nsei", "NSEI was invalid count " },
172 { "lost:alive", "ALIVE ACK missing count " },
173 { "lost:reset", "RESET ACK missing count " },
174};
175
176static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
177 .group_name_prefix = "ns:nsvc",
178 .group_description = "NSVC Peer Statistics",
179 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
180 .ctr_desc = nsvc_ctr_description,
181 .class_id = OSMO_STATS_CLASS_PEER,
182};
183
184
185static const struct osmo_stat_item_desc nsvc_stat_description[] = {
186 { "alive.delay", "ALIVE response time ", "ms", 16, 0 },
187};
188
189static const struct osmo_stat_item_group_desc nsvc_statg_desc = {
190 .group_name_prefix = "ns.nsvc",
191 .group_description = "NSVC Peer Statistics",
192 .num_items = ARRAY_SIZE(nsvc_stat_description),
193 .item_desc = nsvc_stat_description,
194 .class_id = OSMO_STATS_CLASS_PEER,
195};
196
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100197const struct value_string gprs_ns2_aff_cause_prim_strs[] = {
198 { NS_AFF_CAUSE_VC_FAILURE, "NSVC failure" },
199 { NS_AFF_CAUSE_VC_RECOVERY, "NSVC recovery" },
200 { NS_AFF_CAUSE_FAILURE, "NSE failure" },
201 { NS_AFF_CAUSE_RECOVERY, "NSE recovery" },
202 { NS_AFF_CAUSE_SNS_CONFIGURED, "NSE SNS configured" },
203 { NS_AFF_CAUSE_SNS_FAILURE, "NSE SNS failure" },
204 { 0, NULL }
205};
206
Alexander Couzens0ab028c2020-11-04 02:41:44 +0100207const struct value_string gprs_ns2_prim_strs[] = {
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100208 { PRIM_NS_UNIT_DATA, "UNIT DATA" },
209 { PRIM_NS_CONGESTION, "CONGESTION" },
210 { PRIM_NS_STATUS, "STATUS" },
211 { 0, NULL }
212};
213
Harald Weltea24e7ee2020-11-29 17:38:48 +0100214const struct value_string gprs_ns2_lltype_strs[] = {
215 { GPRS_NS2_LL_UDP, "UDP" },
216 { GPRS_NS2_LL_FR_GRE, "FR_GRE" },
217 { GPRS_NS2_LL_FR, "FR" },
218 { 0, NULL }
219};
220
Harald Welte5bef2cc2020-09-18 22:33:24 +0200221/*! string-format a given NS-VC into a user-supplied buffer.
222 * \param[in] buf user-allocated output buffer
223 * \param[in] buf_len size of user-allocated output buffer in bytes
224 * \param[in] nsvc NS-VC to be string-formatted
225 * \return pointer to buf on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200226char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc)
227{
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200228 const struct osmo_sockaddr *local;
229 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200230 struct osmo_sockaddr_str local_str;
231 struct osmo_sockaddr_str remote_str;
232
233 if (!buf_len)
Harald Welte92ad0292020-09-18 22:34:24 +0200234 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200235
Alexander Couzensaac90162020-11-19 02:44:04 +0100236 switch (nsvc->nse->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100237 case GPRS_NS2_LL_UDP:
Alexander Couzens6a161492020-07-12 13:45:50 +0200238 if (!gprs_ns2_is_ip_bind(nsvc->bind)) {
239 buf[0] = '\0';
240 return buf;
241 }
242
243 local = gprs_ns2_ip_bind_sockaddr(nsvc->bind);
Alexander Couzensc4229a42020-10-11 20:58:04 +0200244 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200245 if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas))
246 strcpy(local_str.ip, "invalid");
247 if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas))
248 strcpy(remote_str.ip, "invalid");
249
250 if (nsvc->nsvci_is_valid)
251 snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u",
252 local_str.ip, local_str.port,
253 nsvc->nsvci,
254 remote_str.ip, remote_str.port);
255 else
256 snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u",
257 local_str.ip, local_str.port,
258 remote_str.ip, remote_str.port);
259 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100260 case GPRS_NS2_LL_FR_GRE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200261 snprintf(buf, buf_len, "frgre)");
262 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100263 case GPRS_NS2_LL_FR:
Alexander Couzens841817e2020-11-19 00:41:29 +0100264 if (!gprs_ns2_is_frgre_bind(nsvc->bind)) {
265 buf[0] = '\0';
266 return buf;
267 }
268
269 snprintf(buf, buf_len, "fr)netif: %s dlci: %u", gprs_ns2_fr_bind_netif(nsvc->bind),
270 gprs_ns2_fr_nsvc_dlci(nsvc));
271 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200272 default:
273 buf[0] = '\0';
274 break;
275 }
276
277 buf[buf_len - 1] = '\0';
278
279 return buf;
280}
281
282/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
283#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
284
Harald Welte5bef2cc2020-09-18 22:33:24 +0200285/*! string-format a given NS-VC to a thread-local static buffer.
286 * \param[in] nsvc NS-VC to be string-formatted
287 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200288const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
289{
290 static __thread char buf[NS2_LL_MAX_STR];
291 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
292}
293
Harald Welte5bef2cc2020-09-18 22:33:24 +0200294/*! string-format a given NS-VC to a dynamically allocated string.
295 * \param[in] ctx talloc context from which to allocate
296 * \param[in] nsvc NS-VC to be string-formatted
297 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200298char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
299{
300 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
301 if (!buf)
302 return buf;
303 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
304}
305
Daniel Willmannf1286542020-11-03 23:03:33 +0100306/*! Return the current state name of a given NS-VC to a thread-local static buffer.
307 * \param[in] nsvc NS-VC to return the state of
308 * \return pointer to the string on success; NULL on error */
309const char *gprs_ns2_nsvc_state_name(struct gprs_ns2_vc *nsvc)
310{
311 return osmo_fsm_inst_state_name(nsvc->fi);
312}
313
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100314/* select a signalling NSVC and respect sig_counter
315 * param[out] reset_counter - all counter has to be resetted to their signal weight
316 * return the chosen nsvc or NULL
317 */
318static struct gprs_ns2_vc *ns2_load_sharing_signal(struct gprs_ns2_nse *nse)
319{
320 struct gprs_ns2_vc *nsvc = NULL, *last = NULL, *tmp;
321
322 llist_for_each_entry(tmp, &nse->nsvc, list) {
323 if (tmp->sig_weight == 0)
324 continue;
325 if (!gprs_ns2_vc_is_unblocked(tmp))
326 continue;
327 if (tmp->sig_counter == 0) {
328 last = tmp;
329 continue;
330 }
331
332 tmp->sig_counter--;
333 nsvc = tmp;
334 break;
335 }
336
337 /* all counter were zero, but there are valid nsvc */
338 if (!nsvc && last) {
339 llist_for_each_entry(tmp, &nse->nsvc, list) {
340 tmp->sig_counter = tmp->sig_weight;
341 }
342
343 last->sig_counter--;
344 return last;
345 } else {
346 return nsvc;
347 }
348}
349
350/* 4.4.1 Load Sharing function for the Frame Relay Sub-Network */
351static struct gprs_ns2_vc *ns2_load_sharing_modulor(
352 struct gprs_ns2_nse *nse,
353 uint16_t bvci,
354 uint32_t load_selector)
355{
356 struct gprs_ns2_vc *tmp;
357 uint32_t mod = (bvci + load_selector) % nse->nsvc_data_count;
358 uint32_t i = 0;
359
360 llist_for_each_entry(tmp, &nse->nsvc, list) {
361 if (!gprs_ns2_vc_is_unblocked(tmp))
362 continue;
363 if (tmp->data_weight == 0)
364 continue;
365
366 if (i == mod)
367 return tmp;
368 i++;
369 }
370
371 return NULL;
372}
373
374/* pick the first available data NSVC - no load sharing */
375struct gprs_ns2_vc *ns2_load_sharing_first(struct gprs_ns2_nse *nse)
376{
377 struct gprs_ns2_vc *nsvc = NULL, *tmp;
378
379 llist_for_each_entry(tmp, &nse->nsvc, list) {
380 if (!gprs_ns2_vc_is_unblocked(tmp))
381 continue;
382 if (tmp->data_weight == 0)
383 continue;
384
385 nsvc = tmp;
386 break;
387 }
388
389 return nsvc;
390}
391
392
393static struct gprs_ns2_vc *ns2_load_sharing(
394 struct gprs_ns2_nse *nse,
395 uint16_t bvci,
396 uint32_t link_selector)
397{
398 struct gprs_ns2_vc *nsvc = NULL;
399
400 if (bvci == 0) {
401 /* signalling */
402 nsvc = ns2_load_sharing_signal(nse);
403 } else {
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100404 /* data with load sharing parameter */
405 if (llist_empty(&nse->nsvc))
406 return NULL;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100407
Alexander Couzensaac90162020-11-19 02:44:04 +0100408 switch (nse->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100409 case GPRS_NS2_LL_FR:
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100410 nsvc = ns2_load_sharing_modulor(nse, bvci, link_selector);
411 break;
412 default:
413 nsvc = ns2_load_sharing_first(nse);
414 break;
415 }
416 }
417
418 return nsvc;
419}
420
Harald Welte5bef2cc2020-09-18 22:33:24 +0200421/*! Receive a primitive from the NS User (Gb).
422 * \param[in] nsi NS instance to which the primitive is issued
423 * \param[in] oph The primitive
424 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200425int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
426{
Alexander Couzens6a161492020-07-12 13:45:50 +0200427 /* TODO: implement resource distribution */
428 /* TODO: check for empty PDUs which can be sent to Request/Confirm
429 * the IP endpoint */
430 struct osmo_gprs_ns2_prim *nsp;
431 struct gprs_ns2_nse *nse = NULL;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100432 struct gprs_ns2_vc *nsvc = NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200433 uint16_t bvci, nsei;
434 uint8_t sducontrol = 0;
435
436 if (oph->sap != SAP_NS)
437 return -EINVAL;
438
439 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
440
441 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != PRIM_NS_UNIT_DATA)
442 return -EINVAL;
443
444 if (!oph->msg)
445 return -EINVAL;
446
447 bvci = nsp->bvci;
448 nsei = nsp->nsei;
449
450 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
451 if (!nse)
452 return -EINVAL;
453
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100454 nsvc = ns2_load_sharing(nse, bvci, nsp->u.unitdata.link_selector);
Alexander Couzens6a161492020-07-12 13:45:50 +0200455
456 /* TODO: send a status primitive back */
457 if (!nsvc)
458 return 0;
459
460 if (nsp->u.unitdata.change == NS_ENDPOINT_REQUEST_CHANGE)
461 sducontrol = 1;
462 else if (nsp->u.unitdata.change == NS_ENDPOINT_CONFIRM_CHANGE)
463 sducontrol = 2;
464
465 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
466}
467
Harald Welte5bef2cc2020-09-18 22:33:24 +0200468/*! Send a STATUS.ind primitive to the specified NS instance user.
469 * \param[in] nsi NS instance on which we operate
470 * \param[in] nsei NSEI to which the statue relates
471 * \param[in] bvci BVCI to which the status relates
472 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200473void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100474 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200475 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200476 enum gprs_ns2_affecting_cause cause)
477{
Daniel Willmann15c09a82020-11-03 23:05:43 +0100478 char nsvc_str[NS2_LL_MAX_STR];
Alexander Couzens6a161492020-07-12 13:45:50 +0200479 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200480 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200481 nsp.bvci = bvci;
482 nsp.u.status.cause = cause;
483 nsp.u.status.transfer = -1;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200484 nsp.u.status.first = nse->first;
485 nsp.u.status.persistent = nse->persistent;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100486 if (nsvc)
487 nsp.u.status.nsvc = gprs_ns2_ll_str_buf(nsvc_str, sizeof(nsvc_str), nsvc);
488
Alexander Couzens6a161492020-07-12 13:45:50 +0200489 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS,
490 PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200491 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200492}
493
Harald Welte5bef2cc2020-09-18 22:33:24 +0200494/*! Allocate a NS-VC within the given bind + NSE.
495 * \param[in] bind The 'bind' on which we operate
496 * \param[in] nse The NS Entity on which we operate
497 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
498 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200499struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater)
500{
501 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
502
503 if (!nsvc)
504 return NULL;
505
506 nsvc->bind = bind;
507 nsvc->nse = nse;
508 nsvc->mode = bind->vc_mode;
509 nsvc->sig_weight = 1;
510 nsvc->data_weight = 1;
511
512 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx);
513 if (!nsvc->ctrg) {
514 goto err;
515 }
516 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx);
517 if (!nsvc->statg)
518 goto err_group;
519 if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater))
520 goto err_statg;
521
522 bind->nsi->rate_ctr_idx++;
523
524 llist_add(&nsvc->list, &nse->nsvc);
525 llist_add(&nsvc->blist, &bind->nsvc);
526
527 return nsvc;
528
529err_statg:
530 osmo_stat_item_group_free(nsvc->statg);
531err_group:
532 rate_ctr_group_free(nsvc->ctrg);
533err:
534 talloc_free(nsvc);
535
536 return NULL;
537}
538
Harald Welte5bef2cc2020-09-18 22:33:24 +0200539/*! Destroy/release given NS-VC.
540 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200541void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
542{
543 if (!nsvc)
544 return;
545
Daniel Willmann15c09a82020-11-03 23:05:43 +0100546 ns2_prim_status_ind(nsvc->nse, nsvc, 0, NS_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200547
548 llist_del(&nsvc->list);
549 llist_del(&nsvc->blist);
550
551 /* notify nse this nsvc is unavailable */
552 ns2_nse_notify_unblocked(nsvc, false);
553
554 /* check if sns is using this VC */
555 ns2_sns_free_nsvc(nsvc);
556 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
557
558 /* let the driver/bind clean up it's internal state */
559 if (nsvc->priv && nsvc->bind->free_vc)
560 nsvc->bind->free_vc(nsvc);
561
562 osmo_stat_item_group_free(nsvc->statg);
563 rate_ctr_group_free(nsvc->ctrg);
564
565 talloc_free(nsvc);
566}
567
Harald Welte5bef2cc2020-09-18 22:33:24 +0200568/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200569struct msgb *gprs_ns2_msgb_alloc(void)
570{
571 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
572 "GPRS/NS");
573 if (!msg) {
574 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
575 NS_ALLOC_SIZE);
576 }
577 return msg;
578}
579
Harald Welte5bef2cc2020-09-18 22:33:24 +0200580/*! Create a status message to be sent over a new connection.
581 * \param[in] orig_msg the original message
582 * \param[in] tp TLVP parsed of the original message
583 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
584 * \param[in] cause Cause for the rejection
585 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200586static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
587{
588 struct msgb *msg = gprs_ns2_msgb_alloc();
589 struct gprs_ns_hdr *nsh;
590 bool have_vci = false;
591 uint8_t _cause = cause;
592 uint16_t nsei = 0;
593
594 if (!msg)
595 return -ENOMEM;
596
597 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
598 nsei = tlvp_val16be(tp, NS_IE_NSEI);
599
600 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
601 nsei, gprs_ns2_cause_str(cause));
602 }
603
604 msg->l2h = msgb_put(msg, sizeof(*nsh));
605 nsh = (struct gprs_ns_hdr *) msg->l2h;
606 nsh->pdu_type = NS_PDUT_STATUS;
607
608 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
609 have_vci = TLVP_PRESENT(tp, NS_IE_VCI);
610
611 /* Section 9.2.7.1: Static conditions for NS-VCI */
612 if (cause == NS_CAUSE_NSVC_BLOCKED ||
613 cause == NS_CAUSE_NSVC_UNKNOWN) {
614 if (!have_vci) {
615 msgb_free(msg);
616 return -EINVAL;
617 }
618
619 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
620 }
621
622 /* Section 9.2.7.2: Static conditions for NS PDU */
623 switch (cause) {
624 case NS_CAUSE_SEM_INCORR_PDU:
625 case NS_CAUSE_PDU_INCOMP_PSTATE:
626 case NS_CAUSE_PROTO_ERR_UNSPEC:
627 case NS_CAUSE_INVAL_ESSENT_IE:
628 case NS_CAUSE_MISSING_ESSENT_IE:
629 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
630 orig_msg->l2h);
631 break;
632 default:
633 break;
634 }
635
636 *reject = msg;
637 return 0;
638}
639
Harald Welte5bef2cc2020-09-18 22:33:24 +0200640/*! Resolve a NS Entity based on its NSEI.
641 * \param[in] nsi NS Instance in which we do the look-up
642 * \param[in] nsei NSEI to look up
643 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200644struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
645{
646 struct gprs_ns2_nse *nse;
647
648 llist_for_each_entry(nse, &nsi->nse, list) {
649 if (nse->nsei == nsei)
650 return nse;
651 }
652
653 return NULL;
654}
655
Harald Welte5bef2cc2020-09-18 22:33:24 +0200656/*! Resolve a NS-VC Entity based on its NS-VCI.
657 * \param[in] nsi NS Instance in which we do the look-up
658 * \param[in] nsvci NS-VCI to look up
659 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200660struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
661{
662 struct gprs_ns2_nse *nse;
663 struct gprs_ns2_vc *nsvc;
664
665 llist_for_each_entry(nse, &nsi->nse, list) {
666 llist_for_each_entry(nsvc, &nse->nsvc, list) {
667 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
668 return nsvc;
669 }
670 }
671
672 return NULL;
673}
674
Harald Welte5bef2cc2020-09-18 22:33:24 +0200675/*! Create a NS Entity within given NS instance.
676 * \param[in] nsi NS instance in which to create NS Entity
677 * \param[in] nsei NS Entity Identifier of to-be-created NSE
678 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzensaac90162020-11-19 02:44:04 +0100679struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei, enum gprs_ns2_ll linklayer)
Alexander Couzens6a161492020-07-12 13:45:50 +0200680{
681 struct gprs_ns2_nse *nse;
682
683 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
684 if (nse) {
685 LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei);
686 return nse;
687 }
688
689 nse = talloc_zero(nsi, struct gprs_ns2_nse);
690 if (!nse)
691 return NULL;
692
Alexander Couzensaac90162020-11-19 02:44:04 +0100693 nse->ll = linklayer;
Alexander Couzens6a161492020-07-12 13:45:50 +0200694 nse->nsei = nsei;
695 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200696 nse->first = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200697 llist_add(&nse->list, &nsi->nse);
698 INIT_LLIST_HEAD(&nse->nsvc);
699
700 return nse;
701}
702
Alexander Couzens05e7f7d2020-10-11 19:51:46 +0200703/*! Return the NSEI
704 * \param[in] nse NS Entity
705 * \return the nsei.
706 */
707uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse)
708{
709 return nse->nsei;
710}
711
Harald Welte5bef2cc2020-09-18 22:33:24 +0200712/*! Destroy given NS Entity.
713 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200714void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
715{
716 struct gprs_ns2_vc *nsvc, *tmp;
717
718 if (!nse)
719 return;
720
721 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
722 gprs_ns2_free_nsvc(nsvc);
723 }
724
Daniel Willmann15c09a82020-11-03 23:05:43 +0100725 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200726
727 llist_del(&nse->list);
728 if (nse->bss_sns_fi)
729 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
730 talloc_free(nse);
731}
732
Alexander Couzens4b6c8af2020-10-11 20:15:25 +0200733void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi)
734{
735 struct gprs_ns2_nse *nse, *ntmp;
736
737 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
738 gprs_ns2_free_nse(nse);
739 }
740}
741
Alexander Couzens6a161492020-07-12 13:45:50 +0200742static inline int ns2_tlv_parse(struct tlv_parsed *dec,
743 const uint8_t *buf, int buf_len, uint8_t lv_tag,
744 uint8_t lv_tag2)
745{
746 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
747 * See comment of ns_att_tlvdef1. */
748 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
749 if (rc < 0)
750 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
751 return rc;
752}
753
754
Harald Welte5bef2cc2020-09-18 22:33:24 +0200755/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
756 * \param[in] bind the bind through which msg was received
757 * \param[in] msg the actual received message
758 * \param[in] logname A name to describe the VC. E.g. ip address pair
759 * \param[out] reject A message filled to be sent back. Only used in failure cases.
760 * \param[out] success A pointer which will be set to the new VC on success
761 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzens6a161492020-07-12 13:45:50 +0200762enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
763 struct msgb *msg,
764 const char *logname,
765 struct msgb **reject,
766 struct gprs_ns2_vc **success)
767{
768 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
769 struct tlv_parsed tp;
770 struct gprs_ns2_vc *nsvc;
771 struct gprs_ns2_nse *nse;
772 uint16_t nsvci;
773 uint16_t nsei;
774
775 int rc;
776
777 if (msg->len < sizeof(struct gprs_ns_hdr))
778 return GPRS_NS2_CS_ERROR;
779
780 if (nsh->pdu_type == NS_PDUT_STATUS) {
781 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
782 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
783 "for non-existing NS-VC\n",
784 logname);
785 return GPRS_NS2_CS_SKIPPED;
786 }
787
788 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
789 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
790 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
791 "for non-existing NS-VC\n",
792 logname);
793 return GPRS_NS2_CS_SKIPPED;
794 }
795
796 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
797 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
798 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
799 "for non-existing NS-VC\n",
800 logname);
801 return GPRS_NS2_CS_SKIPPED;
802 }
803
Alexander Couzens48f63862020-09-12 02:19:19 +0200804 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
805 /* Only the RESET procedure creates a new NSVC */
806 if (nsh->pdu_type != NS_PDUT_RESET) {
807 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200808
Alexander Couzens48f63862020-09-12 02:19:19 +0200809 if (rc < 0) {
810 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
811 return rc;
812 }
813 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200814 }
Alexander Couzens48f63862020-09-12 02:19:19 +0200815 } else { /* NS2_VC_MODE_ALIVE */
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200816 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens48f63862020-09-12 02:19:19 +0200817
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200818 if (rc < 0) {
819 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
820 return rc;
Alexander Couzens48f63862020-09-12 02:19:19 +0200821 }
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200822 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200823 }
824
825 rc = ns2_tlv_parse(&tp, nsh->data,
826 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
827 if (rc < 0) {
828 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
829 "TLV Parse\n", rc);
830 /* TODO: send invalid message back */
831 return GPRS_NS2_CS_REJECTED;
832 }
833
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200834 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
835 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
836 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
837 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
838 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200839 }
840
841 /* find or create NSE */
842 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
843 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
844 if (!nse) {
845 if (!bind->nsi->create_nse) {
846 return GPRS_NS2_CS_SKIPPED;
847 }
848
Alexander Couzensaac90162020-11-19 02:44:04 +0100849 nse = gprs_ns2_create_nse(bind->nsi, nsei, bind->ll);
Alexander Couzens6a161492020-07-12 13:45:50 +0200850 if (!nse) {
851 return GPRS_NS2_CS_ERROR;
852 }
853 }
854
855 nsvc = ns2_vc_alloc(bind, nse, false);
856 if (!nsvc)
857 return GPRS_NS2_CS_SKIPPED;
858
Alexander Couzens6a161492020-07-12 13:45:50 +0200859 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
860 nsvc->nsvci = nsvci;
861 nsvc->nsvci_is_valid = true;
862
863 *success = nsvc;
864
865 return GPRS_NS2_CS_CREATED;
866}
867
Harald Welte5bef2cc2020-09-18 22:33:24 +0200868/*! Create, and connect an inactive, new IP-based NS-VC
869 * \param[in] bind bind in which the new NS-VC is to be created
870 * \param[in] remote remote address to which to connect
871 * \param[in] nse NS Entity in which the NS-VC is to be created
872 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
873 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200874struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700875 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200876 struct gprs_ns2_nse *nse,
877 uint16_t nsvci)
878{
879 struct gprs_ns2_vc *nsvc;
880
881 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
882 if (!nsvc)
883 return NULL;
884
885 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
886 nsvc->nsvci = nsvci;
887 nsvc->nsvci_is_valid = true;
888 }
889
890 return nsvc;
891}
892
Harald Welte5bef2cc2020-09-18 22:33:24 +0200893/*! Create, connect and activate a new IP-based NS-VC
894 * \param[in] bind bind in which the new NS-VC is to be created
895 * \param[in] remote remote address to which to connect
896 * \param[in] nse NS Entity in which the NS-VC is to be created
897 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
898 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200899struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700900 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200901 struct gprs_ns2_nse *nse,
902 uint16_t nsvci)
903{
904 struct gprs_ns2_vc *nsvc;
905 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
906 if (!nsvc)
907 return NULL;
908
909 gprs_ns2_vc_fsm_start(nsvc);
910
911 return nsvc;
912}
913
Harald Welte5bef2cc2020-09-18 22:33:24 +0200914/*! Create, connect and activate a new IP-based NS-VC
915 * \param[in] bind bind in which the new NS-VC is to be created
916 * \param[in] remote remote address to which to connect
917 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
918 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
919 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200920struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700921 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200922 uint16_t nsei,
923 uint16_t nsvci)
924{
925 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
926
927 if (!nse) {
Alexander Couzensaac90162020-11-19 02:44:04 +0100928 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP);
Alexander Couzens6a161492020-07-12 13:45:50 +0200929 if (!nse)
930 return NULL;
931 }
932
933 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
934}
935
Harald Welte5bef2cc2020-09-18 22:33:24 +0200936/*! Create, connect and activate a new IP-SNS NSE.
937 * \param[in] bind bind in which the new NS-VC is to be created
938 * \param[in] remote remote address to which to connect
939 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
940 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200941int gprs_ns2_ip_connect_sns(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700942 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200943 uint16_t nsei)
944{
945 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
946 struct gprs_ns2_vc *nsvc;
947
948 if (!nse) {
Alexander Couzensaac90162020-11-19 02:44:04 +0100949 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP);
Alexander Couzens6a161492020-07-12 13:45:50 +0200950 if (!nse)
951 return -1;
952 }
953
954 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
955 if (!nsvc)
956 return -1;
957
958 if (!nse->bss_sns_fi)
959 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, NULL);
960
961 if (!nse->bss_sns_fi)
962 return -1;
963
964 return ns2_sns_bss_fsm_start(nse, nsvc, remote);
965}
966
Harald Welte5bef2cc2020-09-18 22:33:24 +0200967/*! Find NS-VC for given socket address.
968 * \param[in] nse NS Entity in which to search
969 * \param[in] sockaddr socket address to search for
970 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +0200971struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700972 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +0200973{
974 struct gprs_ns2_vc *nsvc;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200975 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200976
977 OSMO_ASSERT(nse);
978 OSMO_ASSERT(sockaddr);
979
980 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200981 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200982 if (!osmo_sockaddr_cmp(sockaddr, remote))
983 return nsvc;
984 }
985
986 return NULL;
987}
988
Alexander Couzens6cb5d5f2020-10-11 23:23:31 +0200989/*!
990 * Iterate over all nsvc of a NS Entity and call the callback.
991 * If the callback returns < 0 it aborts the loop and returns the callback return code.
992 * \param[in] nse NS Entity to iterate over all nsvcs
993 * \param[in] cb the callback to call
994 * \param[inout] cb_data the private data of the callback
995 * \return 0 if the loop completes. If a callback returns < 0 it will returns this value.
996 */
997int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data)
998{
999 struct gprs_ns2_vc *nsvc, *tmp;
1000 int rc = 0;
1001 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
1002 rc = cb(nsvc, cb_data);
1003 if (rc < 0)
1004 return rc;
1005 }
1006
1007 return 0;
1008}
1009
1010
Alexander Couzens6a161492020-07-12 13:45:50 +02001011
Harald Welte5bef2cc2020-09-18 22:33:24 +02001012/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +02001013 * \param[in] nsvc NS-VC for which the message was received
1014 * \param msg the received message. Ownership is trasnferred, caller must not free it!
1015 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +02001016int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +02001017 struct msgb *msg)
1018{
1019 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1020 struct tlv_parsed tp;
1021 int rc = 0;
1022
1023 if (msg->len < sizeof(struct gprs_ns_hdr))
1024 return -EINVAL;
1025
1026 switch (nsh->pdu_type) {
1027 case SNS_PDUT_CONFIG:
1028 /* one additional byte ('end flag') before the TLV part starts */
1029 rc = ns2_tlv_parse(&tp, nsh->data+1,
1030 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
1031 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001032 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001033 return rc;
1034 }
1035 /* All sub-network service related message types */
1036 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
1037 break;
1038 case SNS_PDUT_ACK:
1039 case SNS_PDUT_ADD:
1040 case SNS_PDUT_CHANGE_WEIGHT:
1041 case SNS_PDUT_DELETE:
1042 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +02001043 rc = ns2_tlv_parse(&tp, nsh->data+5,
1044 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001045 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001046 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001047 return rc;
1048 }
1049 tp.lv[NS_IE_NSEI].val = nsh->data+2;
1050 tp.lv[NS_IE_NSEI].len = 2;
1051 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
1052 tp.lv[NS_IE_TRANS_ID].len = 1;
1053 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
1054 break;
1055 case SNS_PDUT_CONFIG_ACK:
1056 case SNS_PDUT_SIZE:
1057 case SNS_PDUT_SIZE_ACK:
1058 rc = ns2_tlv_parse(&tp, nsh->data,
1059 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1060 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001061 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001062 return rc;
1063 }
1064 /* All sub-network service related message types */
1065 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
1066 break;
1067
1068 case NS_PDUT_UNITDATA:
1069 rc = gprs_ns2_vc_rx(nsvc, msg, NULL);
1070 break;
1071 default:
1072 rc = ns2_tlv_parse(&tp, nsh->data,
1073 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1074 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001075 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
Alexander Couzens6a161492020-07-12 13:45:50 +02001076 if (nsh->pdu_type != NS_PDUT_STATUS)
1077 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
1078 return rc;
1079 }
1080 rc = gprs_ns2_vc_rx(nsvc, msg, &tp);
1081 break;
1082 }
1083
1084 return rc;
1085}
1086
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001087/* summarize all active data nsvcs */
1088void ns2_nse_data_sum(struct gprs_ns2_nse *nse)
1089{
1090 struct gprs_ns2_vc *nsvc;
1091 nse->nsvc_data_count = 0;
1092
1093 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1094 if (!gprs_ns2_vc_is_unblocked(nsvc))
1095 continue;
1096 if (nsvc->data_weight > 0)
1097 nse->nsvc_data_count++;
1098 }
1099}
1100
Harald Welte5bef2cc2020-09-18 22:33:24 +02001101/*! Notify a nse about the change of a NS-VC.
1102 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
1103 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +02001104void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
1105{
1106 struct gprs_ns2_nse *nse = nsvc->nse;
1107 struct gprs_ns2_vc *tmp;
1108
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001109 ns2_nse_data_sum(nse);
1110
Alexander Couzens6a161492020-07-12 13:45:50 +02001111 if (unblocked == nse->alive)
1112 return;
1113
1114 if (unblocked) {
1115 /* this is the first unblocked NSVC on an unavailable NSE */
1116 nse->alive = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +01001117 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +02001118 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +02001119 return;
1120 }
1121
1122 /* check if there are any remaining alive vcs */
1123 llist_for_each_entry(tmp, &nse->nsvc, list) {
1124 if (tmp == nsvc)
1125 continue;
1126
1127 if (gprs_ns2_vc_is_unblocked(tmp)) {
1128 /* there is at least one remaining alive NSVC */
1129 return;
1130 }
1131 }
1132
1133 /* nse became unavailable */
1134 nse->alive = false;
Daniel Willmann15c09a82020-11-03 23:05:43 +01001135 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001136}
1137
1138/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +02001139 * \param[in] ctx a talloc context to allocate NS instance from
Alexander Couzenscce88282020-10-26 00:25:50 +01001140 * \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 +02001141 * \param[in] cb_data transparent user data passed to Call-back
1142 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001143struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
1144{
1145 struct gprs_ns2_inst *nsi;
1146
1147 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
1148 if (!nsi)
1149 return NULL;
1150
1151 nsi->cb = cb;
1152 nsi->cb_data = cb_data;
1153 INIT_LLIST_HEAD(&nsi->binding);
1154 INIT_LLIST_HEAD(&nsi->nse);
1155
1156 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1157 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1158 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1159 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1160 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1161 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1162 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
1163 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
1164
1165 return nsi;
1166}
1167
Harald Welte5bef2cc2020-09-18 22:33:24 +02001168/*! Destroy a NS Instance (including all its NSEs, binds, ...).
1169 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001170void gprs_ns2_free(struct gprs_ns2_inst *nsi)
1171{
Alexander Couzens6a161492020-07-12 13:45:50 +02001172 if (!nsi)
1173 return;
1174
Alexander Couzens4b6c8af2020-10-11 20:15:25 +02001175 gprs_ns2_free_nses(nsi);
Alexander Couzens896fcd52020-10-11 19:52:36 +02001176 gprs_ns2_free_binds(nsi);
Alexander Couzens35315042020-10-10 03:28:17 +02001177
1178 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001179}
1180
Harald Welte5bef2cc2020-09-18 22:33:24 +02001181/*! Configure whether a NS Instance should dynamically create NSEs based on incoming traffic.
1182 * \param nsi the instance to modify
1183 * \param create_nse if NSE can be created on receiving package. SGSN set this.
1184 * \return 0 on success; negative on error
Alexander Couzens6a161492020-07-12 13:45:50 +02001185 */
1186int gprs_ns2_dynamic_create_nse(struct gprs_ns2_inst *nsi, bool create_nse)
1187{
1188 nsi->create_nse = create_nse;
1189
1190 return 0;
1191}
1192
Harald Welte5bef2cc2020-09-18 22:33:24 +02001193/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1194 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001195void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1196{
1197 struct gprs_ns2_vc *nsvc;
1198 OSMO_ASSERT(nse);
1199
1200 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1201 if (nsvc->sns_only)
1202 continue;
1203
1204 gprs_ns2_vc_fsm_start(nsvc);
1205 }
1206}
1207
Harald Welte5bef2cc2020-09-18 22:33:24 +02001208/*! Set the mode of given bind.
1209 * \param[in] bind the bind we want to set the mode of
1210 * \param[in] modde mode to set bind to */
Alexander Couzens6a161492020-07-12 13:45:50 +02001211void gprs_ns2_bind_set_mode(struct gprs_ns2_vc_bind *bind, enum gprs_ns2_vc_mode mode)
1212{
1213 bind->vc_mode = mode;
1214}
1215
Harald Welte5bef2cc2020-09-18 22:33:24 +02001216/*! Destroy a given bind.
1217 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001218void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1219{
1220 struct gprs_ns2_vc *nsvc, *tmp;
1221 if (!bind)
1222 return;
1223
1224 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1225 gprs_ns2_free_nsvc(nsvc);
1226 }
1227
1228 if (bind->driver->free_bind)
1229 bind->driver->free_bind(bind);
1230
1231 llist_del(&bind->list);
1232 talloc_free(bind);
1233}
Alexander Couzens896fcd52020-10-11 19:52:36 +02001234
1235void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi)
1236{
1237 struct gprs_ns2_vc_bind *bind, *tbind;
1238
1239 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1240 gprs_ns2_free_bind(bind);
1241 }
1242}
Alexander Couzens6a161492020-07-12 13:45:50 +02001243/*! @} */