blob: 9bf1d7fc609190b0578ab7c4dc0fa69f55f08a2e [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 snprintf(buf, buf_len, "fr)netif: %s dlci: %u", gprs_ns2_fr_bind_netif(nsvc->bind),
265 gprs_ns2_fr_nsvc_dlci(nsvc));
266 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200267 default:
Harald Welte6188e002020-12-01 17:20:07 +0100268 snprintf(buf, buf_len, "unknown)");
Alexander Couzens6a161492020-07-12 13:45:50 +0200269 break;
270 }
271
272 buf[buf_len - 1] = '\0';
273
274 return buf;
275}
276
277/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
278#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
279
Harald Welte5bef2cc2020-09-18 22:33:24 +0200280/*! string-format a given NS-VC to a thread-local static buffer.
281 * \param[in] nsvc NS-VC to be string-formatted
282 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200283const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
284{
285 static __thread char buf[NS2_LL_MAX_STR];
286 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
287}
288
Harald Welte5bef2cc2020-09-18 22:33:24 +0200289/*! string-format a given NS-VC to a dynamically allocated string.
290 * \param[in] ctx talloc context from which to allocate
291 * \param[in] nsvc NS-VC to be string-formatted
292 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200293char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
294{
295 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
296 if (!buf)
297 return buf;
298 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
299}
300
Daniel Willmannf1286542020-11-03 23:03:33 +0100301/*! Return the current state name of a given NS-VC to a thread-local static buffer.
302 * \param[in] nsvc NS-VC to return the state of
303 * \return pointer to the string on success; NULL on error */
304const char *gprs_ns2_nsvc_state_name(struct gprs_ns2_vc *nsvc)
305{
306 return osmo_fsm_inst_state_name(nsvc->fi);
307}
308
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100309/* select a signalling NSVC and respect sig_counter
310 * param[out] reset_counter - all counter has to be resetted to their signal weight
311 * return the chosen nsvc or NULL
312 */
313static struct gprs_ns2_vc *ns2_load_sharing_signal(struct gprs_ns2_nse *nse)
314{
315 struct gprs_ns2_vc *nsvc = NULL, *last = NULL, *tmp;
316
317 llist_for_each_entry(tmp, &nse->nsvc, list) {
318 if (tmp->sig_weight == 0)
319 continue;
320 if (!gprs_ns2_vc_is_unblocked(tmp))
321 continue;
322 if (tmp->sig_counter == 0) {
323 last = tmp;
324 continue;
325 }
326
327 tmp->sig_counter--;
328 nsvc = tmp;
329 break;
330 }
331
332 /* all counter were zero, but there are valid nsvc */
333 if (!nsvc && last) {
334 llist_for_each_entry(tmp, &nse->nsvc, list) {
335 tmp->sig_counter = tmp->sig_weight;
336 }
337
338 last->sig_counter--;
339 return last;
340 } else {
341 return nsvc;
342 }
343}
344
345/* 4.4.1 Load Sharing function for the Frame Relay Sub-Network */
346static struct gprs_ns2_vc *ns2_load_sharing_modulor(
347 struct gprs_ns2_nse *nse,
348 uint16_t bvci,
349 uint32_t load_selector)
350{
351 struct gprs_ns2_vc *tmp;
352 uint32_t mod = (bvci + load_selector) % nse->nsvc_data_count;
353 uint32_t i = 0;
354
355 llist_for_each_entry(tmp, &nse->nsvc, list) {
356 if (!gprs_ns2_vc_is_unblocked(tmp))
357 continue;
358 if (tmp->data_weight == 0)
359 continue;
360
361 if (i == mod)
362 return tmp;
363 i++;
364 }
365
366 return NULL;
367}
368
369/* pick the first available data NSVC - no load sharing */
370struct gprs_ns2_vc *ns2_load_sharing_first(struct gprs_ns2_nse *nse)
371{
372 struct gprs_ns2_vc *nsvc = NULL, *tmp;
373
374 llist_for_each_entry(tmp, &nse->nsvc, list) {
375 if (!gprs_ns2_vc_is_unblocked(tmp))
376 continue;
377 if (tmp->data_weight == 0)
378 continue;
379
380 nsvc = tmp;
381 break;
382 }
383
384 return nsvc;
385}
386
387
388static struct gprs_ns2_vc *ns2_load_sharing(
389 struct gprs_ns2_nse *nse,
390 uint16_t bvci,
391 uint32_t link_selector)
392{
393 struct gprs_ns2_vc *nsvc = NULL;
394
395 if (bvci == 0) {
396 /* signalling */
397 nsvc = ns2_load_sharing_signal(nse);
398 } else {
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100399 /* data with load sharing parameter */
400 if (llist_empty(&nse->nsvc))
401 return NULL;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100402
Alexander Couzensaac90162020-11-19 02:44:04 +0100403 switch (nse->ll) {
Alexander Couzens24a14ac2020-11-19 02:34:49 +0100404 case GPRS_NS2_LL_FR:
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100405 nsvc = ns2_load_sharing_modulor(nse, bvci, link_selector);
406 break;
407 default:
408 nsvc = ns2_load_sharing_first(nse);
409 break;
410 }
411 }
412
413 return nsvc;
414}
415
Harald Welte5bef2cc2020-09-18 22:33:24 +0200416/*! Receive a primitive from the NS User (Gb).
417 * \param[in] nsi NS instance to which the primitive is issued
418 * \param[in] oph The primitive
419 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200420int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
421{
Alexander Couzens6a161492020-07-12 13:45:50 +0200422 /* TODO: implement resource distribution */
423 /* TODO: check for empty PDUs which can be sent to Request/Confirm
424 * the IP endpoint */
425 struct osmo_gprs_ns2_prim *nsp;
426 struct gprs_ns2_nse *nse = NULL;
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100427 struct gprs_ns2_vc *nsvc = NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200428 uint16_t bvci, nsei;
429 uint8_t sducontrol = 0;
430
431 if (oph->sap != SAP_NS)
432 return -EINVAL;
433
434 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
435
436 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != PRIM_NS_UNIT_DATA)
437 return -EINVAL;
438
439 if (!oph->msg)
440 return -EINVAL;
441
442 bvci = nsp->bvci;
443 nsei = nsp->nsei;
444
445 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
446 if (!nse)
447 return -EINVAL;
448
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +0100449 nsvc = ns2_load_sharing(nse, bvci, nsp->u.unitdata.link_selector);
Alexander Couzens6a161492020-07-12 13:45:50 +0200450
451 /* TODO: send a status primitive back */
452 if (!nsvc)
453 return 0;
454
455 if (nsp->u.unitdata.change == NS_ENDPOINT_REQUEST_CHANGE)
456 sducontrol = 1;
457 else if (nsp->u.unitdata.change == NS_ENDPOINT_CONFIRM_CHANGE)
458 sducontrol = 2;
459
460 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
461}
462
Harald Welte5bef2cc2020-09-18 22:33:24 +0200463/*! Send a STATUS.ind primitive to the specified NS instance user.
464 * \param[in] nsi NS instance on which we operate
465 * \param[in] nsei NSEI to which the statue relates
466 * \param[in] bvci BVCI to which the status relates
467 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200468void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100469 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200470 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200471 enum gprs_ns2_affecting_cause cause)
472{
Daniel Willmann15c09a82020-11-03 23:05:43 +0100473 char nsvc_str[NS2_LL_MAX_STR];
Alexander Couzens6a161492020-07-12 13:45:50 +0200474 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200475 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200476 nsp.bvci = bvci;
477 nsp.u.status.cause = cause;
478 nsp.u.status.transfer = -1;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200479 nsp.u.status.first = nse->first;
480 nsp.u.status.persistent = nse->persistent;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100481 if (nsvc)
482 nsp.u.status.nsvc = gprs_ns2_ll_str_buf(nsvc_str, sizeof(nsvc_str), nsvc);
483
Alexander Couzens6a161492020-07-12 13:45:50 +0200484 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS,
485 PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200486 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200487}
488
Harald Welte5bef2cc2020-09-18 22:33:24 +0200489/*! Allocate a NS-VC within the given bind + NSE.
490 * \param[in] bind The 'bind' on which we operate
491 * \param[in] nse The NS Entity on which we operate
492 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
493 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200494struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater)
495{
496 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
497
498 if (!nsvc)
499 return NULL;
500
501 nsvc->bind = bind;
502 nsvc->nse = nse;
503 nsvc->mode = bind->vc_mode;
504 nsvc->sig_weight = 1;
505 nsvc->data_weight = 1;
506
507 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx);
508 if (!nsvc->ctrg) {
509 goto err;
510 }
511 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx);
512 if (!nsvc->statg)
513 goto err_group;
514 if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater))
515 goto err_statg;
516
517 bind->nsi->rate_ctr_idx++;
518
519 llist_add(&nsvc->list, &nse->nsvc);
520 llist_add(&nsvc->blist, &bind->nsvc);
521
522 return nsvc;
523
524err_statg:
525 osmo_stat_item_group_free(nsvc->statg);
526err_group:
527 rate_ctr_group_free(nsvc->ctrg);
528err:
529 talloc_free(nsvc);
530
531 return NULL;
532}
533
Harald Welte5bef2cc2020-09-18 22:33:24 +0200534/*! Destroy/release given NS-VC.
535 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200536void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
537{
538 if (!nsvc)
539 return;
540
Daniel Willmann15c09a82020-11-03 23:05:43 +0100541 ns2_prim_status_ind(nsvc->nse, nsvc, 0, NS_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200542
543 llist_del(&nsvc->list);
544 llist_del(&nsvc->blist);
545
546 /* notify nse this nsvc is unavailable */
547 ns2_nse_notify_unblocked(nsvc, false);
548
549 /* check if sns is using this VC */
550 ns2_sns_free_nsvc(nsvc);
551 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
552
553 /* let the driver/bind clean up it's internal state */
554 if (nsvc->priv && nsvc->bind->free_vc)
555 nsvc->bind->free_vc(nsvc);
556
557 osmo_stat_item_group_free(nsvc->statg);
558 rate_ctr_group_free(nsvc->ctrg);
559
560 talloc_free(nsvc);
561}
562
Harald Welte5bef2cc2020-09-18 22:33:24 +0200563/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200564struct msgb *gprs_ns2_msgb_alloc(void)
565{
566 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
567 "GPRS/NS");
568 if (!msg) {
569 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
570 NS_ALLOC_SIZE);
571 }
572 return msg;
573}
574
Harald Welte5bef2cc2020-09-18 22:33:24 +0200575/*! Create a status message to be sent over a new connection.
576 * \param[in] orig_msg the original message
577 * \param[in] tp TLVP parsed of the original message
578 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
579 * \param[in] cause Cause for the rejection
580 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200581static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
582{
583 struct msgb *msg = gprs_ns2_msgb_alloc();
584 struct gprs_ns_hdr *nsh;
585 bool have_vci = false;
586 uint8_t _cause = cause;
587 uint16_t nsei = 0;
588
589 if (!msg)
590 return -ENOMEM;
591
592 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
593 nsei = tlvp_val16be(tp, NS_IE_NSEI);
594
595 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
596 nsei, gprs_ns2_cause_str(cause));
597 }
598
599 msg->l2h = msgb_put(msg, sizeof(*nsh));
600 nsh = (struct gprs_ns_hdr *) msg->l2h;
601 nsh->pdu_type = NS_PDUT_STATUS;
602
603 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
604 have_vci = TLVP_PRESENT(tp, NS_IE_VCI);
605
606 /* Section 9.2.7.1: Static conditions for NS-VCI */
607 if (cause == NS_CAUSE_NSVC_BLOCKED ||
608 cause == NS_CAUSE_NSVC_UNKNOWN) {
609 if (!have_vci) {
610 msgb_free(msg);
611 return -EINVAL;
612 }
613
614 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
615 }
616
617 /* Section 9.2.7.2: Static conditions for NS PDU */
618 switch (cause) {
619 case NS_CAUSE_SEM_INCORR_PDU:
620 case NS_CAUSE_PDU_INCOMP_PSTATE:
621 case NS_CAUSE_PROTO_ERR_UNSPEC:
622 case NS_CAUSE_INVAL_ESSENT_IE:
623 case NS_CAUSE_MISSING_ESSENT_IE:
624 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
625 orig_msg->l2h);
626 break;
627 default:
628 break;
629 }
630
631 *reject = msg;
632 return 0;
633}
634
Harald Welte5bef2cc2020-09-18 22:33:24 +0200635/*! Resolve a NS Entity based on its NSEI.
636 * \param[in] nsi NS Instance in which we do the look-up
637 * \param[in] nsei NSEI to look up
638 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200639struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
640{
641 struct gprs_ns2_nse *nse;
642
643 llist_for_each_entry(nse, &nsi->nse, list) {
644 if (nse->nsei == nsei)
645 return nse;
646 }
647
648 return NULL;
649}
650
Harald Welte5bef2cc2020-09-18 22:33:24 +0200651/*! Resolve a NS-VC Entity based on its NS-VCI.
652 * \param[in] nsi NS Instance in which we do the look-up
653 * \param[in] nsvci NS-VCI to look up
654 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200655struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
656{
657 struct gprs_ns2_nse *nse;
658 struct gprs_ns2_vc *nsvc;
659
660 llist_for_each_entry(nse, &nsi->nse, list) {
661 llist_for_each_entry(nsvc, &nse->nsvc, list) {
662 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
663 return nsvc;
664 }
665 }
666
667 return NULL;
668}
669
Harald Welte5bef2cc2020-09-18 22:33:24 +0200670/*! Create a NS Entity within given NS instance.
671 * \param[in] nsi NS instance in which to create NS Entity
672 * \param[in] nsei NS Entity Identifier of to-be-created NSE
673 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzensaac90162020-11-19 02:44:04 +0100674struct 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 +0200675{
676 struct gprs_ns2_nse *nse;
677
678 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
679 if (nse) {
680 LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei);
681 return nse;
682 }
683
684 nse = talloc_zero(nsi, struct gprs_ns2_nse);
685 if (!nse)
686 return NULL;
687
Alexander Couzensaac90162020-11-19 02:44:04 +0100688 nse->ll = linklayer;
Alexander Couzens6a161492020-07-12 13:45:50 +0200689 nse->nsei = nsei;
690 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200691 nse->first = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200692 llist_add(&nse->list, &nsi->nse);
693 INIT_LLIST_HEAD(&nse->nsvc);
694
695 return nse;
696}
697
Alexander Couzens05e7f7d2020-10-11 19:51:46 +0200698/*! Return the NSEI
699 * \param[in] nse NS Entity
700 * \return the nsei.
701 */
702uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse)
703{
704 return nse->nsei;
705}
706
Harald Welte5bef2cc2020-09-18 22:33:24 +0200707/*! Destroy given NS Entity.
708 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200709void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
710{
711 struct gprs_ns2_vc *nsvc, *tmp;
712
713 if (!nse)
714 return;
715
716 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
717 gprs_ns2_free_nsvc(nsvc);
718 }
719
Daniel Willmann15c09a82020-11-03 23:05:43 +0100720 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200721
722 llist_del(&nse->list);
723 if (nse->bss_sns_fi)
724 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
725 talloc_free(nse);
726}
727
Alexander Couzens4b6c8af2020-10-11 20:15:25 +0200728void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi)
729{
730 struct gprs_ns2_nse *nse, *ntmp;
731
732 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
733 gprs_ns2_free_nse(nse);
734 }
735}
736
Alexander Couzens6a161492020-07-12 13:45:50 +0200737static inline int ns2_tlv_parse(struct tlv_parsed *dec,
738 const uint8_t *buf, int buf_len, uint8_t lv_tag,
739 uint8_t lv_tag2)
740{
741 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
742 * See comment of ns_att_tlvdef1. */
743 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
744 if (rc < 0)
745 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
746 return rc;
747}
748
749
Harald Welte5bef2cc2020-09-18 22:33:24 +0200750/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
751 * \param[in] bind the bind through which msg was received
752 * \param[in] msg the actual received message
753 * \param[in] logname A name to describe the VC. E.g. ip address pair
754 * \param[out] reject A message filled to be sent back. Only used in failure cases.
755 * \param[out] success A pointer which will be set to the new VC on success
756 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzens6a161492020-07-12 13:45:50 +0200757enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
758 struct msgb *msg,
759 const char *logname,
760 struct msgb **reject,
761 struct gprs_ns2_vc **success)
762{
763 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
764 struct tlv_parsed tp;
765 struct gprs_ns2_vc *nsvc;
766 struct gprs_ns2_nse *nse;
767 uint16_t nsvci;
768 uint16_t nsei;
769
770 int rc;
771
772 if (msg->len < sizeof(struct gprs_ns_hdr))
773 return GPRS_NS2_CS_ERROR;
774
775 if (nsh->pdu_type == NS_PDUT_STATUS) {
776 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
777 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
778 "for non-existing NS-VC\n",
779 logname);
780 return GPRS_NS2_CS_SKIPPED;
781 }
782
783 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
784 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
785 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
786 "for non-existing NS-VC\n",
787 logname);
788 return GPRS_NS2_CS_SKIPPED;
789 }
790
791 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
792 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
793 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
794 "for non-existing NS-VC\n",
795 logname);
796 return GPRS_NS2_CS_SKIPPED;
797 }
798
Alexander Couzens48f63862020-09-12 02:19:19 +0200799 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
800 /* Only the RESET procedure creates a new NSVC */
801 if (nsh->pdu_type != NS_PDUT_RESET) {
802 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200803
Alexander Couzens48f63862020-09-12 02:19:19 +0200804 if (rc < 0) {
805 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
806 return rc;
807 }
808 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200809 }
Alexander Couzens48f63862020-09-12 02:19:19 +0200810 } else { /* NS2_VC_MODE_ALIVE */
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200811 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens48f63862020-09-12 02:19:19 +0200812
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200813 if (rc < 0) {
814 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
815 return rc;
Alexander Couzens48f63862020-09-12 02:19:19 +0200816 }
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200817 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200818 }
819
820 rc = ns2_tlv_parse(&tp, nsh->data,
821 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
822 if (rc < 0) {
823 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
824 "TLV Parse\n", rc);
825 /* TODO: send invalid message back */
826 return GPRS_NS2_CS_REJECTED;
827 }
828
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200829 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
830 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
831 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
832 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
833 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200834 }
835
836 /* find or create NSE */
837 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
838 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
839 if (!nse) {
840 if (!bind->nsi->create_nse) {
841 return GPRS_NS2_CS_SKIPPED;
842 }
843
Alexander Couzensaac90162020-11-19 02:44:04 +0100844 nse = gprs_ns2_create_nse(bind->nsi, nsei, bind->ll);
Alexander Couzens6a161492020-07-12 13:45:50 +0200845 if (!nse) {
846 return GPRS_NS2_CS_ERROR;
847 }
848 }
849
850 nsvc = ns2_vc_alloc(bind, nse, false);
851 if (!nsvc)
852 return GPRS_NS2_CS_SKIPPED;
853
Alexander Couzens6a161492020-07-12 13:45:50 +0200854 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
855 nsvc->nsvci = nsvci;
856 nsvc->nsvci_is_valid = true;
857
858 *success = nsvc;
859
860 return GPRS_NS2_CS_CREATED;
861}
862
Harald Welte5bef2cc2020-09-18 22:33:24 +0200863/*! Create, and connect an inactive, new IP-based NS-VC
864 * \param[in] bind bind in which the new NS-VC is to be created
865 * \param[in] remote remote address to which to connect
866 * \param[in] nse NS Entity in which the NS-VC is to be created
867 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
868 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200869struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700870 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200871 struct gprs_ns2_nse *nse,
872 uint16_t nsvci)
873{
874 struct gprs_ns2_vc *nsvc;
875
876 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
877 if (!nsvc)
878 return NULL;
879
880 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
881 nsvc->nsvci = nsvci;
882 nsvc->nsvci_is_valid = true;
883 }
884
885 return nsvc;
886}
887
Harald Welte5bef2cc2020-09-18 22:33:24 +0200888/*! Create, connect and activate a new IP-based NS-VC
889 * \param[in] bind bind in which the new NS-VC is to be created
890 * \param[in] remote remote address to which to connect
891 * \param[in] nse NS Entity in which the NS-VC is to be created
892 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
893 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200894struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700895 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200896 struct gprs_ns2_nse *nse,
897 uint16_t nsvci)
898{
899 struct gprs_ns2_vc *nsvc;
900 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
901 if (!nsvc)
902 return NULL;
903
904 gprs_ns2_vc_fsm_start(nsvc);
905
906 return nsvc;
907}
908
Harald Welte5bef2cc2020-09-18 22:33:24 +0200909/*! Create, connect and activate a new IP-based NS-VC
910 * \param[in] bind bind in which the new NS-VC is to be created
911 * \param[in] remote remote address to which to connect
912 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
913 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
914 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200915struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700916 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200917 uint16_t nsei,
918 uint16_t nsvci)
919{
920 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
921
922 if (!nse) {
Alexander Couzensaac90162020-11-19 02:44:04 +0100923 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP);
Alexander Couzens6a161492020-07-12 13:45:50 +0200924 if (!nse)
925 return NULL;
926 }
927
928 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
929}
930
Harald Welte5bef2cc2020-09-18 22:33:24 +0200931/*! Create, connect and activate a new IP-SNS NSE.
932 * \param[in] bind bind in which the new NS-VC is to be created
933 * \param[in] remote remote address to which to connect
934 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
935 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200936int gprs_ns2_ip_connect_sns(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700937 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200938 uint16_t nsei)
939{
940 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
941 struct gprs_ns2_vc *nsvc;
942
943 if (!nse) {
Alexander Couzensaac90162020-11-19 02:44:04 +0100944 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP);
Alexander Couzens6a161492020-07-12 13:45:50 +0200945 if (!nse)
946 return -1;
947 }
948
949 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
950 if (!nsvc)
951 return -1;
952
953 if (!nse->bss_sns_fi)
954 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, NULL);
955
956 if (!nse->bss_sns_fi)
957 return -1;
958
959 return ns2_sns_bss_fsm_start(nse, nsvc, remote);
960}
961
Harald Welte5bef2cc2020-09-18 22:33:24 +0200962/*! Find NS-VC for given socket address.
963 * \param[in] nse NS Entity in which to search
964 * \param[in] sockaddr socket address to search for
965 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +0200966struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700967 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +0200968{
969 struct gprs_ns2_vc *nsvc;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200970 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200971
972 OSMO_ASSERT(nse);
973 OSMO_ASSERT(sockaddr);
974
975 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200976 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200977 if (!osmo_sockaddr_cmp(sockaddr, remote))
978 return nsvc;
979 }
980
981 return NULL;
982}
983
Alexander Couzens6cb5d5f2020-10-11 23:23:31 +0200984/*!
985 * Iterate over all nsvc of a NS Entity and call the callback.
986 * If the callback returns < 0 it aborts the loop and returns the callback return code.
987 * \param[in] nse NS Entity to iterate over all nsvcs
988 * \param[in] cb the callback to call
989 * \param[inout] cb_data the private data of the callback
990 * \return 0 if the loop completes. If a callback returns < 0 it will returns this value.
991 */
992int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data)
993{
994 struct gprs_ns2_vc *nsvc, *tmp;
995 int rc = 0;
996 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
997 rc = cb(nsvc, cb_data);
998 if (rc < 0)
999 return rc;
1000 }
1001
1002 return 0;
1003}
1004
1005
Alexander Couzens6a161492020-07-12 13:45:50 +02001006
Harald Welte5bef2cc2020-09-18 22:33:24 +02001007/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +02001008 * \param[in] nsvc NS-VC for which the message was received
1009 * \param msg the received message. Ownership is trasnferred, caller must not free it!
1010 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +02001011int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +02001012 struct msgb *msg)
1013{
1014 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
1015 struct tlv_parsed tp;
1016 int rc = 0;
1017
1018 if (msg->len < sizeof(struct gprs_ns_hdr))
1019 return -EINVAL;
1020
1021 switch (nsh->pdu_type) {
1022 case SNS_PDUT_CONFIG:
1023 /* one additional byte ('end flag') before the TLV part starts */
1024 rc = ns2_tlv_parse(&tp, nsh->data+1,
1025 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
1026 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001027 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001028 return rc;
1029 }
1030 /* All sub-network service related message types */
1031 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
1032 break;
1033 case SNS_PDUT_ACK:
1034 case SNS_PDUT_ADD:
1035 case SNS_PDUT_CHANGE_WEIGHT:
1036 case SNS_PDUT_DELETE:
1037 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +02001038 rc = ns2_tlv_parse(&tp, nsh->data+5,
1039 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +02001040 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001041 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001042 return rc;
1043 }
1044 tp.lv[NS_IE_NSEI].val = nsh->data+2;
1045 tp.lv[NS_IE_NSEI].len = 2;
1046 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
1047 tp.lv[NS_IE_TRANS_ID].len = 1;
1048 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
1049 break;
1050 case SNS_PDUT_CONFIG_ACK:
1051 case SNS_PDUT_SIZE:
1052 case SNS_PDUT_SIZE_ACK:
1053 rc = ns2_tlv_parse(&tp, nsh->data,
1054 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1055 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001056 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +02001057 return rc;
1058 }
1059 /* All sub-network service related message types */
1060 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
1061 break;
1062
1063 case NS_PDUT_UNITDATA:
1064 rc = gprs_ns2_vc_rx(nsvc, msg, NULL);
1065 break;
1066 default:
1067 rc = ns2_tlv_parse(&tp, nsh->data,
1068 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
1069 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +02001070 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
Alexander Couzens6a161492020-07-12 13:45:50 +02001071 if (nsh->pdu_type != NS_PDUT_STATUS)
1072 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
1073 return rc;
1074 }
1075 rc = gprs_ns2_vc_rx(nsvc, msg, &tp);
1076 break;
1077 }
1078
1079 return rc;
1080}
1081
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001082/* summarize all active data nsvcs */
1083void ns2_nse_data_sum(struct gprs_ns2_nse *nse)
1084{
1085 struct gprs_ns2_vc *nsvc;
1086 nse->nsvc_data_count = 0;
1087
1088 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1089 if (!gprs_ns2_vc_is_unblocked(nsvc))
1090 continue;
1091 if (nsvc->data_weight > 0)
1092 nse->nsvc_data_count++;
1093 }
1094}
1095
Harald Welte5bef2cc2020-09-18 22:33:24 +02001096/*! Notify a nse about the change of a NS-VC.
1097 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
1098 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +02001099void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
1100{
1101 struct gprs_ns2_nse *nse = nsvc->nse;
1102 struct gprs_ns2_vc *tmp;
1103
Alexander Couzensfc3dd1f2020-11-19 00:41:47 +01001104 ns2_nse_data_sum(nse);
1105
Alexander Couzens6a161492020-07-12 13:45:50 +02001106 if (unblocked == nse->alive)
1107 return;
1108
1109 if (unblocked) {
1110 /* this is the first unblocked NSVC on an unavailable NSE */
1111 nse->alive = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +01001112 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +02001113 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +02001114 return;
1115 }
1116
1117 /* check if there are any remaining alive vcs */
1118 llist_for_each_entry(tmp, &nse->nsvc, list) {
1119 if (tmp == nsvc)
1120 continue;
1121
1122 if (gprs_ns2_vc_is_unblocked(tmp)) {
1123 /* there is at least one remaining alive NSVC */
1124 return;
1125 }
1126 }
1127
1128 /* nse became unavailable */
1129 nse->alive = false;
Daniel Willmann15c09a82020-11-03 23:05:43 +01001130 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001131}
1132
1133/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +02001134 * \param[in] ctx a talloc context to allocate NS instance from
Alexander Couzenscce88282020-10-26 00:25:50 +01001135 * \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 +02001136 * \param[in] cb_data transparent user data passed to Call-back
1137 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001138struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
1139{
1140 struct gprs_ns2_inst *nsi;
1141
1142 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
1143 if (!nsi)
1144 return NULL;
1145
1146 nsi->cb = cb;
1147 nsi->cb_data = cb_data;
1148 INIT_LLIST_HEAD(&nsi->binding);
1149 INIT_LLIST_HEAD(&nsi->nse);
1150
1151 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1152 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1153 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1154 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1155 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1156 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1157 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
1158 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
1159
1160 return nsi;
1161}
1162
Harald Welte5bef2cc2020-09-18 22:33:24 +02001163/*! Destroy a NS Instance (including all its NSEs, binds, ...).
1164 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001165void gprs_ns2_free(struct gprs_ns2_inst *nsi)
1166{
Alexander Couzens6a161492020-07-12 13:45:50 +02001167 if (!nsi)
1168 return;
1169
Alexander Couzens4b6c8af2020-10-11 20:15:25 +02001170 gprs_ns2_free_nses(nsi);
Alexander Couzens896fcd52020-10-11 19:52:36 +02001171 gprs_ns2_free_binds(nsi);
Alexander Couzens35315042020-10-10 03:28:17 +02001172
1173 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001174}
1175
Harald Welte5bef2cc2020-09-18 22:33:24 +02001176/*! Configure whether a NS Instance should dynamically create NSEs based on incoming traffic.
1177 * \param nsi the instance to modify
1178 * \param create_nse if NSE can be created on receiving package. SGSN set this.
1179 * \return 0 on success; negative on error
Alexander Couzens6a161492020-07-12 13:45:50 +02001180 */
1181int gprs_ns2_dynamic_create_nse(struct gprs_ns2_inst *nsi, bool create_nse)
1182{
1183 nsi->create_nse = create_nse;
1184
1185 return 0;
1186}
1187
Harald Welte5bef2cc2020-09-18 22:33:24 +02001188/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1189 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001190void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1191{
1192 struct gprs_ns2_vc *nsvc;
1193 OSMO_ASSERT(nse);
1194
1195 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1196 if (nsvc->sns_only)
1197 continue;
1198
1199 gprs_ns2_vc_fsm_start(nsvc);
1200 }
1201}
1202
Harald Welte5bef2cc2020-09-18 22:33:24 +02001203/*! Set the mode of given bind.
1204 * \param[in] bind the bind we want to set the mode of
1205 * \param[in] modde mode to set bind to */
Alexander Couzens6a161492020-07-12 13:45:50 +02001206void gprs_ns2_bind_set_mode(struct gprs_ns2_vc_bind *bind, enum gprs_ns2_vc_mode mode)
1207{
1208 bind->vc_mode = mode;
1209}
1210
Harald Welte5bef2cc2020-09-18 22:33:24 +02001211/*! Destroy a given bind.
1212 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001213void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1214{
1215 struct gprs_ns2_vc *nsvc, *tmp;
1216 if (!bind)
1217 return;
1218
1219 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1220 gprs_ns2_free_nsvc(nsvc);
1221 }
1222
1223 if (bind->driver->free_bind)
1224 bind->driver->free_bind(bind);
1225
1226 llist_del(&bind->list);
1227 talloc_free(bind);
1228}
Alexander Couzens896fcd52020-10-11 19:52:36 +02001229
1230void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi)
1231{
1232 struct gprs_ns2_vc_bind *bind, *tbind;
1233
1234 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1235 gprs_ns2_free_bind(bind);
1236 }
1237}
Alexander Couzens6a161492020-07-12 13:45:50 +02001238/*! @} */