blob: 5e43364c220a423279b441351d829a9b0003fada [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 Welte5bef2cc2020-09-18 22:33:24 +0200214/*! string-format a given NS-VC into a user-supplied buffer.
215 * \param[in] buf user-allocated output buffer
216 * \param[in] buf_len size of user-allocated output buffer in bytes
217 * \param[in] nsvc NS-VC to be string-formatted
218 * \return pointer to buf on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200219char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc)
220{
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200221 const struct osmo_sockaddr *local;
222 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200223 struct osmo_sockaddr_str local_str;
224 struct osmo_sockaddr_str remote_str;
225
226 if (!buf_len)
Harald Welte92ad0292020-09-18 22:34:24 +0200227 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200228
Alexander Couzensaac90162020-11-19 02:44:04 +0100229 switch (nsvc->nse->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100230 case GPRS_NS2_LL_UDP:
Alexander Couzens6a161492020-07-12 13:45:50 +0200231 if (!gprs_ns2_is_ip_bind(nsvc->bind)) {
232 buf[0] = '\0';
233 return buf;
234 }
235
236 local = gprs_ns2_ip_bind_sockaddr(nsvc->bind);
Alexander Couzensc4229a42020-10-11 20:58:04 +0200237 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200238 if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas))
239 strcpy(local_str.ip, "invalid");
240 if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas))
241 strcpy(remote_str.ip, "invalid");
242
243 if (nsvc->nsvci_is_valid)
244 snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u",
245 local_str.ip, local_str.port,
246 nsvc->nsvci,
247 remote_str.ip, remote_str.port);
248 else
249 snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u",
250 local_str.ip, local_str.port,
251 remote_str.ip, remote_str.port);
252 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100253 case GPRS_NS2_LL_FR_GRE:
Alexander Couzens6a161492020-07-12 13:45:50 +0200254 snprintf(buf, buf_len, "frgre)");
255 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100256 case GPRS_NS2_LL_E1:
Alexander Couzens6a161492020-07-12 13:45:50 +0200257 snprintf(buf, buf_len, "e1)");
258 break;
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100259 case GPRS_NS2_LL_FR:
Alexander Couzens841817e2020-11-19 00:41:29 +0100260 if (!gprs_ns2_is_frgre_bind(nsvc->bind)) {
261 buf[0] = '\0';
262 return buf;
263 }
264
265 snprintf(buf, buf_len, "fr)netif: %s dlci: %u", gprs_ns2_fr_bind_netif(nsvc->bind),
266 gprs_ns2_fr_nsvc_dlci(nsvc));
267 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200268 default:
269 buf[0] = '\0';
270 break;
271 }
272
273 buf[buf_len - 1] = '\0';
274
275 return buf;
276}
277
278/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
279#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
280
Harald Welte5bef2cc2020-09-18 22:33:24 +0200281/*! string-format a given NS-VC to a thread-local static buffer.
282 * \param[in] nsvc NS-VC to be string-formatted
283 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200284const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
285{
286 static __thread char buf[NS2_LL_MAX_STR];
287 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
288}
289
Harald Welte5bef2cc2020-09-18 22:33:24 +0200290/*! string-format a given NS-VC to a dynamically allocated string.
291 * \param[in] ctx talloc context from which to allocate
292 * \param[in] nsvc NS-VC to be string-formatted
293 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200294char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
295{
296 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
297 if (!buf)
298 return buf;
299 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
300}
301
Daniel Willmannf1286542020-11-03 23:03:33 +0100302/*! Return the current state name of a given NS-VC to a thread-local static buffer.
303 * \param[in] nsvc NS-VC to return the state of
304 * \return pointer to the string on success; NULL on error */
305const char *gprs_ns2_nsvc_state_name(struct gprs_ns2_vc *nsvc)
306{
307 return osmo_fsm_inst_state_name(nsvc->fi);
308}
309
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100310/* select a signalling NSVC and respect sig_counter
311 * param[out] reset_counter - all counter has to be resetted to their signal weight
312 * return the chosen nsvc or NULL
313 */
314static struct gprs_ns2_vc *ns2_load_sharing_signal(struct gprs_ns2_nse *nse)
315{
316 struct gprs_ns2_vc *nsvc = NULL, *last = NULL, *tmp;
317
318 llist_for_each_entry(tmp, &nse->nsvc, list) {
319 if (tmp->sig_weight == 0)
320 continue;
321 if (!gprs_ns2_vc_is_unblocked(tmp))
322 continue;
323 if (tmp->sig_counter == 0) {
324 last = tmp;
325 continue;
326 }
327
328 tmp->sig_counter--;
329 nsvc = tmp;
330 break;
331 }
332
333 /* all counter were zero, but there are valid nsvc */
334 if (!nsvc && last) {
335 llist_for_each_entry(tmp, &nse->nsvc, list) {
336 tmp->sig_counter = tmp->sig_weight;
337 }
338
339 last->sig_counter--;
340 return last;
341 } else {
342 return nsvc;
343 }
344}
345
346/* 4.4.1 Load Sharing function for the Frame Relay Sub-Network */
347static struct gprs_ns2_vc *ns2_load_sharing_modulor(
348 struct gprs_ns2_nse *nse,
349 uint16_t bvci,
350 uint32_t load_selector)
351{
352 struct gprs_ns2_vc *tmp;
353 uint32_t mod = (bvci + load_selector) % nse->nsvc_data_count;
354 uint32_t i = 0;
355
356 llist_for_each_entry(tmp, &nse->nsvc, list) {
357 if (!gprs_ns2_vc_is_unblocked(tmp))
358 continue;
359 if (tmp->data_weight == 0)
360 continue;
361
362 if (i == mod)
363 return tmp;
364 i++;
365 }
366
367 return NULL;
368}
369
370/* pick the first available data NSVC - no load sharing */
371struct gprs_ns2_vc *ns2_load_sharing_first(struct gprs_ns2_nse *nse)
372{
373 struct gprs_ns2_vc *nsvc = NULL, *tmp;
374
375 llist_for_each_entry(tmp, &nse->nsvc, list) {
376 if (!gprs_ns2_vc_is_unblocked(tmp))
377 continue;
378 if (tmp->data_weight == 0)
379 continue;
380
381 nsvc = tmp;
382 break;
383 }
384
385 return nsvc;
386}
387
388
389static struct gprs_ns2_vc *ns2_load_sharing(
390 struct gprs_ns2_nse *nse,
391 uint16_t bvci,
392 uint32_t link_selector)
393{
394 struct gprs_ns2_vc *nsvc = NULL;
395
396 if (bvci == 0) {
397 /* signalling */
398 nsvc = ns2_load_sharing_signal(nse);
399 } else {
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100400 /* data with load sharing parameter */
401 if (llist_empty(&nse->nsvc))
402 return NULL;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100403
Alexander Couzensaac90162020-11-19 02:44:04 +0100404 switch (nse->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100405 case GPRS_NS2_LL_FR:
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100406 nsvc = ns2_load_sharing_modulor(nse, bvci, link_selector);
407 break;
408 default:
409 nsvc = ns2_load_sharing_first(nse);
410 break;
411 }
412 }
413
414 return nsvc;
415}
416
Harald Welte5bef2cc2020-09-18 22:33:24 +0200417/*! Receive a primitive from the NS User (Gb).
418 * \param[in] nsi NS instance to which the primitive is issued
419 * \param[in] oph The primitive
420 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200421int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
422{
Alexander Couzens6a161492020-07-12 13:45:50 +0200423 /* TODO: implement resource distribution */
424 /* TODO: check for empty PDUs which can be sent to Request/Confirm
425 * the IP endpoint */
426 struct osmo_gprs_ns2_prim *nsp;
427 struct gprs_ns2_nse *nse = NULL;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100428 struct gprs_ns2_vc *nsvc = NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200429 uint16_t bvci, nsei;
430 uint8_t sducontrol = 0;
431
432 if (oph->sap != SAP_NS)
433 return -EINVAL;
434
435 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
436
437 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != PRIM_NS_UNIT_DATA)
438 return -EINVAL;
439
440 if (!oph->msg)
441 return -EINVAL;
442
443 bvci = nsp->bvci;
444 nsei = nsp->nsei;
445
446 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
447 if (!nse)
448 return -EINVAL;
449
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100450 nsvc = ns2_load_sharing(nse, bvci, nsp->u.unitdata.link_selector);
Alexander Couzens6a161492020-07-12 13:45:50 +0200451
452 /* TODO: send a status primitive back */
453 if (!nsvc)
454 return 0;
455
456 if (nsp->u.unitdata.change == NS_ENDPOINT_REQUEST_CHANGE)
457 sducontrol = 1;
458 else if (nsp->u.unitdata.change == NS_ENDPOINT_CONFIRM_CHANGE)
459 sducontrol = 2;
460
461 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
462}
463
Harald Welte5bef2cc2020-09-18 22:33:24 +0200464/*! Send a STATUS.ind primitive to the specified NS instance user.
465 * \param[in] nsi NS instance on which we operate
466 * \param[in] nsei NSEI to which the statue relates
467 * \param[in] bvci BVCI to which the status relates
468 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200469void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100470 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200471 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200472 enum gprs_ns2_affecting_cause cause)
473{
Daniel Willmann15c09a82020-11-03 23:05:43 +0100474 char nsvc_str[NS2_LL_MAX_STR];
Alexander Couzens6a161492020-07-12 13:45:50 +0200475 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200476 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200477 nsp.bvci = bvci;
478 nsp.u.status.cause = cause;
479 nsp.u.status.transfer = -1;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200480 nsp.u.status.first = nse->first;
481 nsp.u.status.persistent = nse->persistent;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100482 if (nsvc)
483 nsp.u.status.nsvc = gprs_ns2_ll_str_buf(nsvc_str, sizeof(nsvc_str), nsvc);
484
Alexander Couzens6a161492020-07-12 13:45:50 +0200485 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS,
486 PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200487 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200488}
489
Harald Welte5bef2cc2020-09-18 22:33:24 +0200490/*! Allocate a NS-VC within the given bind + NSE.
491 * \param[in] bind The 'bind' on which we operate
492 * \param[in] nse The NS Entity on which we operate
493 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
494 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200495struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater)
496{
497 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
498
499 if (!nsvc)
500 return NULL;
501
502 nsvc->bind = bind;
503 nsvc->nse = nse;
504 nsvc->mode = bind->vc_mode;
505 nsvc->sig_weight = 1;
506 nsvc->data_weight = 1;
507
508 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx);
509 if (!nsvc->ctrg) {
510 goto err;
511 }
512 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx);
513 if (!nsvc->statg)
514 goto err_group;
515 if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater))
516 goto err_statg;
517
518 bind->nsi->rate_ctr_idx++;
519
520 llist_add(&nsvc->list, &nse->nsvc);
521 llist_add(&nsvc->blist, &bind->nsvc);
522
523 return nsvc;
524
525err_statg:
526 osmo_stat_item_group_free(nsvc->statg);
527err_group:
528 rate_ctr_group_free(nsvc->ctrg);
529err:
530 talloc_free(nsvc);
531
532 return NULL;
533}
534
Harald Welte5bef2cc2020-09-18 22:33:24 +0200535/*! Destroy/release given NS-VC.
536 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200537void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
538{
539 if (!nsvc)
540 return;
541
Daniel Willmann15c09a82020-11-03 23:05:43 +0100542 ns2_prim_status_ind(nsvc->nse, nsvc, 0, NS_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200543
544 llist_del(&nsvc->list);
545 llist_del(&nsvc->blist);
546
547 /* notify nse this nsvc is unavailable */
548 ns2_nse_notify_unblocked(nsvc, false);
549
550 /* check if sns is using this VC */
551 ns2_sns_free_nsvc(nsvc);
552 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
553
554 /* let the driver/bind clean up it's internal state */
555 if (nsvc->priv && nsvc->bind->free_vc)
556 nsvc->bind->free_vc(nsvc);
557
558 osmo_stat_item_group_free(nsvc->statg);
559 rate_ctr_group_free(nsvc->ctrg);
560
561 talloc_free(nsvc);
562}
563
Harald Welte5bef2cc2020-09-18 22:33:24 +0200564/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200565struct msgb *gprs_ns2_msgb_alloc(void)
566{
567 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
568 "GPRS/NS");
569 if (!msg) {
570 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
571 NS_ALLOC_SIZE);
572 }
573 return msg;
574}
575
Harald Welte5bef2cc2020-09-18 22:33:24 +0200576/*! Create a status message to be sent over a new connection.
577 * \param[in] orig_msg the original message
578 * \param[in] tp TLVP parsed of the original message
579 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
580 * \param[in] cause Cause for the rejection
581 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200582static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
583{
584 struct msgb *msg = gprs_ns2_msgb_alloc();
585 struct gprs_ns_hdr *nsh;
586 bool have_vci = false;
587 uint8_t _cause = cause;
588 uint16_t nsei = 0;
589
590 if (!msg)
591 return -ENOMEM;
592
593 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
594 nsei = tlvp_val16be(tp, NS_IE_NSEI);
595
596 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
597 nsei, gprs_ns2_cause_str(cause));
598 }
599
600 msg->l2h = msgb_put(msg, sizeof(*nsh));
601 nsh = (struct gprs_ns_hdr *) msg->l2h;
602 nsh->pdu_type = NS_PDUT_STATUS;
603
604 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
605 have_vci = TLVP_PRESENT(tp, NS_IE_VCI);
606
607 /* Section 9.2.7.1: Static conditions for NS-VCI */
608 if (cause == NS_CAUSE_NSVC_BLOCKED ||
609 cause == NS_CAUSE_NSVC_UNKNOWN) {
610 if (!have_vci) {
611 msgb_free(msg);
612 return -EINVAL;
613 }
614
615 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
616 }
617
618 /* Section 9.2.7.2: Static conditions for NS PDU */
619 switch (cause) {
620 case NS_CAUSE_SEM_INCORR_PDU:
621 case NS_CAUSE_PDU_INCOMP_PSTATE:
622 case NS_CAUSE_PROTO_ERR_UNSPEC:
623 case NS_CAUSE_INVAL_ESSENT_IE:
624 case NS_CAUSE_MISSING_ESSENT_IE:
625 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
626 orig_msg->l2h);
627 break;
628 default:
629 break;
630 }
631
632 *reject = msg;
633 return 0;
634}
635
Harald Welte5bef2cc2020-09-18 22:33:24 +0200636/*! Resolve a NS Entity based on its NSEI.
637 * \param[in] nsi NS Instance in which we do the look-up
638 * \param[in] nsei NSEI to look up
639 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200640struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
641{
642 struct gprs_ns2_nse *nse;
643
644 llist_for_each_entry(nse, &nsi->nse, list) {
645 if (nse->nsei == nsei)
646 return nse;
647 }
648
649 return NULL;
650}
651
Harald Welte5bef2cc2020-09-18 22:33:24 +0200652/*! Resolve a NS-VC Entity based on its NS-VCI.
653 * \param[in] nsi NS Instance in which we do the look-up
654 * \param[in] nsvci NS-VCI to look up
655 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200656struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
657{
658 struct gprs_ns2_nse *nse;
659 struct gprs_ns2_vc *nsvc;
660
661 llist_for_each_entry(nse, &nsi->nse, list) {
662 llist_for_each_entry(nsvc, &nse->nsvc, list) {
663 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
664 return nsvc;
665 }
666 }
667
668 return NULL;
669}
670
Harald Welte5bef2cc2020-09-18 22:33:24 +0200671/*! Create a NS Entity within given NS instance.
672 * \param[in] nsi NS instance in which to create NS Entity
673 * \param[in] nsei NS Entity Identifier of to-be-created NSE
674 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzensaac90162020-11-19 02:44:04 +0100675struct 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 +0200676{
677 struct gprs_ns2_nse *nse;
678
679 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
680 if (nse) {
681 LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei);
682 return nse;
683 }
684
685 nse = talloc_zero(nsi, struct gprs_ns2_nse);
686 if (!nse)
687 return NULL;
688
Alexander Couzensaac90162020-11-19 02:44:04 +0100689 nse->ll = linklayer;
Alexander Couzens6a161492020-07-12 13:45:50 +0200690 nse->nsei = nsei;
691 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200692 nse->first = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200693 llist_add(&nse->list, &nsi->nse);
694 INIT_LLIST_HEAD(&nse->nsvc);
695
696 return nse;
697}
698
Alexander Couzens05e7f7d2020-10-11 19:51:46 +0200699/*! Return the NSEI
700 * \param[in] nse NS Entity
701 * \return the nsei.
702 */
703uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse)
704{
705 return nse->nsei;
706}
707
Harald Welte5bef2cc2020-09-18 22:33:24 +0200708/*! Destroy given NS Entity.
709 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200710void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
711{
712 struct gprs_ns2_vc *nsvc, *tmp;
713
714 if (!nse)
715 return;
716
717 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
718 gprs_ns2_free_nsvc(nsvc);
719 }
720
Daniel Willmann15c09a82020-11-03 23:05:43 +0100721 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200722
723 llist_del(&nse->list);
724 if (nse->bss_sns_fi)
725 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
726 talloc_free(nse);
727}
728
Alexander Couzens4b6c8af2020-10-11 20:15:25 +0200729void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi)
730{
731 struct gprs_ns2_nse *nse, *ntmp;
732
733 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
734 gprs_ns2_free_nse(nse);
735 }
736}
737
Alexander Couzens6a161492020-07-12 13:45:50 +0200738static inline int ns2_tlv_parse(struct tlv_parsed *dec,
739 const uint8_t *buf, int buf_len, uint8_t lv_tag,
740 uint8_t lv_tag2)
741{
742 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
743 * See comment of ns_att_tlvdef1. */
744 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
745 if (rc < 0)
746 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
747 return rc;
748}
749
750
Harald Welte5bef2cc2020-09-18 22:33:24 +0200751/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
752 * \param[in] bind the bind through which msg was received
753 * \param[in] msg the actual received message
754 * \param[in] logname A name to describe the VC. E.g. ip address pair
755 * \param[out] reject A message filled to be sent back. Only used in failure cases.
756 * \param[out] success A pointer which will be set to the new VC on success
757 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzens6a161492020-07-12 13:45:50 +0200758enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
759 struct msgb *msg,
760 const char *logname,
761 struct msgb **reject,
762 struct gprs_ns2_vc **success)
763{
764 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
765 struct tlv_parsed tp;
766 struct gprs_ns2_vc *nsvc;
767 struct gprs_ns2_nse *nse;
768 uint16_t nsvci;
769 uint16_t nsei;
770
771 int rc;
772
773 if (msg->len < sizeof(struct gprs_ns_hdr))
774 return GPRS_NS2_CS_ERROR;
775
776 if (nsh->pdu_type == NS_PDUT_STATUS) {
777 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
778 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
779 "for non-existing NS-VC\n",
780 logname);
781 return GPRS_NS2_CS_SKIPPED;
782 }
783
784 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
785 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
786 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
787 "for non-existing NS-VC\n",
788 logname);
789 return GPRS_NS2_CS_SKIPPED;
790 }
791
792 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
793 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
794 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
795 "for non-existing NS-VC\n",
796 logname);
797 return GPRS_NS2_CS_SKIPPED;
798 }
799
Alexander Couzens48f63862020-09-12 02:19:19 +0200800 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
801 /* Only the RESET procedure creates a new NSVC */
802 if (nsh->pdu_type != NS_PDUT_RESET) {
803 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200804
Alexander Couzens48f63862020-09-12 02:19:19 +0200805 if (rc < 0) {
806 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
807 return rc;
808 }
809 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200810 }
Alexander Couzens48f63862020-09-12 02:19:19 +0200811 } else { /* NS2_VC_MODE_ALIVE */
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200812 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens48f63862020-09-12 02:19:19 +0200813
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200814 if (rc < 0) {
815 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
816 return rc;
Alexander Couzens48f63862020-09-12 02:19:19 +0200817 }
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200818 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200819 }
820
821 rc = ns2_tlv_parse(&tp, nsh->data,
822 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
823 if (rc < 0) {
824 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
825 "TLV Parse\n", rc);
826 /* TODO: send invalid message back */
827 return GPRS_NS2_CS_REJECTED;
828 }
829
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200830 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
831 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
832 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
833 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
834 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200835 }
836
837 /* find or create NSE */
838 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
839 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
840 if (!nse) {
841 if (!bind->nsi->create_nse) {
842 return GPRS_NS2_CS_SKIPPED;
843 }
844
Alexander Couzensaac90162020-11-19 02:44:04 +0100845 nse = gprs_ns2_create_nse(bind->nsi, nsei, bind->ll);
Alexander Couzens6a161492020-07-12 13:45:50 +0200846 if (!nse) {
847 return GPRS_NS2_CS_ERROR;
848 }
849 }
850
851 nsvc = ns2_vc_alloc(bind, nse, false);
852 if (!nsvc)
853 return GPRS_NS2_CS_SKIPPED;
854
Alexander Couzens6a161492020-07-12 13:45:50 +0200855 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
856 nsvc->nsvci = nsvci;
857 nsvc->nsvci_is_valid = true;
858
859 *success = nsvc;
860
861 return GPRS_NS2_CS_CREATED;
862}
863
Harald Welte5bef2cc2020-09-18 22:33:24 +0200864/*! Create, and connect an inactive, new IP-based NS-VC
865 * \param[in] bind bind in which the new NS-VC is to be created
866 * \param[in] remote remote address to which to connect
867 * \param[in] nse NS Entity in which the NS-VC is to be created
868 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
869 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200870struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700871 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200872 struct gprs_ns2_nse *nse,
873 uint16_t nsvci)
874{
875 struct gprs_ns2_vc *nsvc;
876
877 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
878 if (!nsvc)
879 return NULL;
880
881 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
882 nsvc->nsvci = nsvci;
883 nsvc->nsvci_is_valid = true;
884 }
885
886 return nsvc;
887}
888
Harald Welte5bef2cc2020-09-18 22:33:24 +0200889/*! Create, connect and activate a new IP-based NS-VC
890 * \param[in] bind bind in which the new NS-VC is to be created
891 * \param[in] remote remote address to which to connect
892 * \param[in] nse NS Entity in which the NS-VC is to be created
893 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
894 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200895struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700896 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200897 struct gprs_ns2_nse *nse,
898 uint16_t nsvci)
899{
900 struct gprs_ns2_vc *nsvc;
901 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
902 if (!nsvc)
903 return NULL;
904
905 gprs_ns2_vc_fsm_start(nsvc);
906
907 return nsvc;
908}
909
Harald Welte5bef2cc2020-09-18 22:33:24 +0200910/*! Create, connect and activate a new IP-based NS-VC
911 * \param[in] bind bind in which the new NS-VC is to be created
912 * \param[in] remote remote address to which to connect
913 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
914 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
915 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200916struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700917 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200918 uint16_t nsei,
919 uint16_t nsvci)
920{
921 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
922
923 if (!nse) {
Alexander Couzensaac90162020-11-19 02:44:04 +0100924 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP);
Alexander Couzens6a161492020-07-12 13:45:50 +0200925 if (!nse)
926 return NULL;
927 }
928
929 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
930}
931
Harald Welte5bef2cc2020-09-18 22:33:24 +0200932/*! Create, connect and activate a new IP-SNS NSE.
933 * \param[in] bind bind in which the new NS-VC is to be created
934 * \param[in] remote remote address to which to connect
935 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
936 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200937int gprs_ns2_ip_connect_sns(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700938 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200939 uint16_t nsei)
940{
941 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
942 struct gprs_ns2_vc *nsvc;
943
944 if (!nse) {
Alexander Couzensaac90162020-11-19 02:44:04 +0100945 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP);
Alexander Couzens6a161492020-07-12 13:45:50 +0200946 if (!nse)
947 return -1;
948 }
949
950 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
951 if (!nsvc)
952 return -1;
953
954 if (!nse->bss_sns_fi)
955 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, NULL);
956
957 if (!nse->bss_sns_fi)
958 return -1;
959
960 return ns2_sns_bss_fsm_start(nse, nsvc, remote);
961}
962
Harald Welte5bef2cc2020-09-18 22:33:24 +0200963/*! Find NS-VC for given socket address.
964 * \param[in] nse NS Entity in which to search
965 * \param[in] sockaddr socket address to search for
966 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +0200967struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700968 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +0200969{
970 struct gprs_ns2_vc *nsvc;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200971 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200972
973 OSMO_ASSERT(nse);
974 OSMO_ASSERT(sockaddr);
975
976 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200977 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200978 if (!osmo_sockaddr_cmp(sockaddr, remote))
979 return nsvc;
980 }
981
982 return NULL;
983}
984
Alexander Couzens6cb5d5f2020-10-11 23:23:31 +0200985/*!
986 * Iterate over all nsvc of a NS Entity and call the callback.
987 * If the callback returns < 0 it aborts the loop and returns the callback return code.
988 * \param[in] nse NS Entity to iterate over all nsvcs
989 * \param[in] cb the callback to call
990 * \param[inout] cb_data the private data of the callback
991 * \return 0 if the loop completes. If a callback returns < 0 it will returns this value.
992 */
993int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data)
994{
995 struct gprs_ns2_vc *nsvc, *tmp;
996 int rc = 0;
997 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
998 rc = cb(nsvc, cb_data);
999 if (rc < 0)
1000 return rc;
1001 }
1002
1003 return 0;
1004}
1005
1006
Alexander Couzens6a161492020-07-12 13:45:50 +02001007
Harald Welte5bef2cc2020-09-18 22:33:24 +02001008/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +02001009 * \param[in] nsvc NS-VC for which the message was received
1010 * \param msg the received message. Ownership is trasnferred, caller must not free it!
1011 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +02001012int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +02001013 struct msgb *msg)
1014{
1015 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1016 struct tlv_parsed tp;
1017 int rc = 0;
1018
1019 if (msg->len < sizeof(struct gprs_ns_hdr))
1020 return -EINVAL;
1021
1022 switch (nsh->pdu_type) {
1023 case SNS_PDUT_CONFIG:
1024 /* one additional byte ('end flag') before the TLV part starts */
1025 rc = ns2_tlv_parse(&tp, nsh->data+1,
1026 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
1027 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001028 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001029 return rc;
1030 }
1031 /* All sub-network service related message types */
1032 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
1033 break;
1034 case SNS_PDUT_ACK:
1035 case SNS_PDUT_ADD:
1036 case SNS_PDUT_CHANGE_WEIGHT:
1037 case SNS_PDUT_DELETE:
1038 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +02001039 rc = ns2_tlv_parse(&tp, nsh->data+5,
1040 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001041 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001042 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001043 return rc;
1044 }
1045 tp.lv[NS_IE_NSEI].val = nsh->data+2;
1046 tp.lv[NS_IE_NSEI].len = 2;
1047 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
1048 tp.lv[NS_IE_TRANS_ID].len = 1;
1049 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
1050 break;
1051 case SNS_PDUT_CONFIG_ACK:
1052 case SNS_PDUT_SIZE:
1053 case SNS_PDUT_SIZE_ACK:
1054 rc = ns2_tlv_parse(&tp, nsh->data,
1055 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1056 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001057 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001058 return rc;
1059 }
1060 /* All sub-network service related message types */
1061 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
1062 break;
1063
1064 case NS_PDUT_UNITDATA:
1065 rc = gprs_ns2_vc_rx(nsvc, msg, NULL);
1066 break;
1067 default:
1068 rc = ns2_tlv_parse(&tp, nsh->data,
1069 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1070 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001071 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
Alexander Couzens6a161492020-07-12 13:45:50 +02001072 if (nsh->pdu_type != NS_PDUT_STATUS)
1073 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
1074 return rc;
1075 }
1076 rc = gprs_ns2_vc_rx(nsvc, msg, &tp);
1077 break;
1078 }
1079
1080 return rc;
1081}
1082
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001083/* summarize all active data nsvcs */
1084void ns2_nse_data_sum(struct gprs_ns2_nse *nse)
1085{
1086 struct gprs_ns2_vc *nsvc;
1087 nse->nsvc_data_count = 0;
1088
1089 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1090 if (!gprs_ns2_vc_is_unblocked(nsvc))
1091 continue;
1092 if (nsvc->data_weight > 0)
1093 nse->nsvc_data_count++;
1094 }
1095}
1096
Harald Welte5bef2cc2020-09-18 22:33:24 +02001097/*! Notify a nse about the change of a NS-VC.
1098 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
1099 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +02001100void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
1101{
1102 struct gprs_ns2_nse *nse = nsvc->nse;
1103 struct gprs_ns2_vc *tmp;
1104
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001105 ns2_nse_data_sum(nse);
1106
Alexander Couzens6a161492020-07-12 13:45:50 +02001107 if (unblocked == nse->alive)
1108 return;
1109
1110 if (unblocked) {
1111 /* this is the first unblocked NSVC on an unavailable NSE */
1112 nse->alive = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +01001113 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +02001114 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +02001115 return;
1116 }
1117
1118 /* check if there are any remaining alive vcs */
1119 llist_for_each_entry(tmp, &nse->nsvc, list) {
1120 if (tmp == nsvc)
1121 continue;
1122
1123 if (gprs_ns2_vc_is_unblocked(tmp)) {
1124 /* there is at least one remaining alive NSVC */
1125 return;
1126 }
1127 }
1128
1129 /* nse became unavailable */
1130 nse->alive = false;
Daniel Willmann15c09a82020-11-03 23:05:43 +01001131 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001132}
1133
1134/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +02001135 * \param[in] ctx a talloc context to allocate NS instance from
Alexander Couzenscce88282020-10-26 00:25:50 +01001136 * \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 +02001137 * \param[in] cb_data transparent user data passed to Call-back
1138 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001139struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
1140{
1141 struct gprs_ns2_inst *nsi;
1142
1143 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
1144 if (!nsi)
1145 return NULL;
1146
1147 nsi->cb = cb;
1148 nsi->cb_data = cb_data;
1149 INIT_LLIST_HEAD(&nsi->binding);
1150 INIT_LLIST_HEAD(&nsi->nse);
1151
1152 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1153 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1154 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1155 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1156 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1157 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1158 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
1159 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
1160
1161 return nsi;
1162}
1163
Harald Welte5bef2cc2020-09-18 22:33:24 +02001164/*! Destroy a NS Instance (including all its NSEs, binds, ...).
1165 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001166void gprs_ns2_free(struct gprs_ns2_inst *nsi)
1167{
Alexander Couzens6a161492020-07-12 13:45:50 +02001168 if (!nsi)
1169 return;
1170
Alexander Couzens4b6c8af2020-10-11 20:15:25 +02001171 gprs_ns2_free_nses(nsi);
Alexander Couzens896fcd52020-10-11 19:52:36 +02001172 gprs_ns2_free_binds(nsi);
Alexander Couzens35315042020-10-10 03:28:17 +02001173
1174 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001175}
1176
Harald Welte5bef2cc2020-09-18 22:33:24 +02001177/*! Configure whether a NS Instance should dynamically create NSEs based on incoming traffic.
1178 * \param nsi the instance to modify
1179 * \param create_nse if NSE can be created on receiving package. SGSN set this.
1180 * \return 0 on success; negative on error
Alexander Couzens6a161492020-07-12 13:45:50 +02001181 */
1182int gprs_ns2_dynamic_create_nse(struct gprs_ns2_inst *nsi, bool create_nse)
1183{
1184 nsi->create_nse = create_nse;
1185
1186 return 0;
1187}
1188
Harald Welte5bef2cc2020-09-18 22:33:24 +02001189/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1190 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001191void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1192{
1193 struct gprs_ns2_vc *nsvc;
1194 OSMO_ASSERT(nse);
1195
1196 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1197 if (nsvc->sns_only)
1198 continue;
1199
1200 gprs_ns2_vc_fsm_start(nsvc);
1201 }
1202}
1203
Harald Welte5bef2cc2020-09-18 22:33:24 +02001204/*! Set the mode of given bind.
1205 * \param[in] bind the bind we want to set the mode of
1206 * \param[in] modde mode to set bind to */
Alexander Couzens6a161492020-07-12 13:45:50 +02001207void gprs_ns2_bind_set_mode(struct gprs_ns2_vc_bind *bind, enum gprs_ns2_vc_mode mode)
1208{
1209 bind->vc_mode = mode;
1210}
1211
Harald Welte5bef2cc2020-09-18 22:33:24 +02001212/*! Destroy a given bind.
1213 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001214void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1215{
1216 struct gprs_ns2_vc *nsvc, *tmp;
1217 if (!bind)
1218 return;
1219
1220 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1221 gprs_ns2_free_nsvc(nsvc);
1222 }
1223
1224 if (bind->driver->free_bind)
1225 bind->driver->free_bind(bind);
1226
1227 llist_del(&bind->list);
1228 talloc_free(bind);
1229}
Alexander Couzens896fcd52020-10-11 19:52:36 +02001230
1231void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi)
1232{
1233 struct gprs_ns2_vc_bind *bind, *tbind;
1234
1235 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1236 gprs_ns2_free_bind(bind);
1237 }
1238}
Alexander Couzens6a161492020-07-12 13:45:50 +02001239/*! @} */