blob: 096e80bf6941d0e2e207c90d12b82076950870e0 [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:
60 * - Only one NS-VC for each NSE: No load-sharing function
61 * - NSVCI 65535 and 65534 are reserved for internal use
Alexander Couzens6a161492020-07-12 13:45:50 +020062 * - There are no BLOCK and UNBLOCK timers (yet?)
63 *
64 * \file gprs_ns2.c */
65
66#include <stdlib.h>
67#include <unistd.h>
68#include <errno.h>
69#include <stdint.h>
70
71#include <sys/types.h>
72#include <sys/socket.h>
73#include <arpa/inet.h>
74
75#include <osmocom/core/fsm.h>
76#include <osmocom/core/msgb.h>
77#include <osmocom/core/rate_ctr.h>
78#include <osmocom/core/socket.h>
79#include <osmocom/core/sockaddr_str.h>
80#include <osmocom/core/stats.h>
81#include <osmocom/core/stat_item.h>
82#include <osmocom/core/talloc.h>
83#include <osmocom/gprs/gprs_msgb.h>
84#include <osmocom/gsm/prim.h>
85#include <osmocom/gsm/tlv.h>
86
87#include "gprs_ns2_internal.h"
88
89#define ns_set_state(ns_, st_) ns_set_state_with_log(ns_, st_, false, __FILE__, __LINE__)
90#define ns_set_remote_state(ns_, st_) ns_set_state_with_log(ns_, st_, true, __FILE__, __LINE__)
91#define ns_mark_blocked(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_BLOCKED)
92#define ns_mark_unblocked(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_BLOCKED));
93#define ns_mark_alive(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_ALIVE)
94#define ns_mark_dead(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_ALIVE));
95
96/* HACK: The NS_IE_IP_ADDR does not follow any known TLV rules.
97 * Since it's a hard ABI break to implement 16 bit tag with fixed length entries to workaround it,
98 * the parser will be called with ns_att_tlvdef1 and if it's failed with ns_att_tlvdef2.
99 * The TLV parser depends on 8bit tag in many places.
100 * The NS_IE_IP_ADDR is only valid for SNS_ACK SNS_ADD and SNS_DELETE.
101 */
102static const struct tlv_definition ns_att_tlvdef1 = {
103 .def = {
104 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
105 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
106 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
107 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
108 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
109 [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 },
110 [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 },
111 [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 },
112 [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 },
113 [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 },
114 [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 },
115 /* NS_IE_IP_ADDR in the IPv4 version */
116 [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 5 },
117 },
118};
119
120static const struct tlv_definition ns_att_tlvdef2 = {
121 .def = {
122 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
123 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
124 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
125 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
126 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
127 [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 },
128 [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 },
129 [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 },
130 [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 },
131 [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 },
132 [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 },
133 /* NS_IE_IP_ADDR in the IPv6 version */
134 [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 17 },
135 },
136};
137
138
139/* Section 10.3.2, Table 13 */
Alexander Couzensb3b837c2020-10-27 15:12:25 +0100140const struct value_string gprs_ns2_cause_strs[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200141 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
142 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
143 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
144 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
145 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
146 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
147 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
148 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
149 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
150 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
151 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
152 { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" },
153 { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" },
154 { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" },
155 { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" },
156 { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" },
157 { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" },
158 { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" },
159 { 0, NULL }
160};
161
Alexander Couzens6a161492020-07-12 13:45:50 +0200162static const struct rate_ctr_desc nsvc_ctr_description[] = {
163 { "packets:in", "Packets at NS Level ( In)" },
164 { "packets:out","Packets at NS Level (Out)" },
165 { "bytes:in", "Bytes at NS Level ( In)" },
166 { "bytes:out", "Bytes at NS Level (Out)" },
167 { "blocked", "NS-VC Block count " },
168 { "dead", "NS-VC gone dead count " },
169 { "replaced", "NS-VC replaced other count" },
170 { "nsei-chg", "NS-VC changed NSEI count " },
171 { "inv-nsvci", "NS-VCI was invalid count " },
172 { "inv-nsei", "NSEI was invalid count " },
173 { "lost:alive", "ALIVE ACK missing count " },
174 { "lost:reset", "RESET ACK missing count " },
175};
176
177static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
178 .group_name_prefix = "ns:nsvc",
179 .group_description = "NSVC Peer Statistics",
180 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
181 .ctr_desc = nsvc_ctr_description,
182 .class_id = OSMO_STATS_CLASS_PEER,
183};
184
185
186static const struct osmo_stat_item_desc nsvc_stat_description[] = {
187 { "alive.delay", "ALIVE response time ", "ms", 16, 0 },
188};
189
190static const struct osmo_stat_item_group_desc nsvc_statg_desc = {
191 .group_name_prefix = "ns.nsvc",
192 .group_description = "NSVC Peer Statistics",
193 .num_items = ARRAY_SIZE(nsvc_stat_description),
194 .item_desc = nsvc_stat_description,
195 .class_id = OSMO_STATS_CLASS_PEER,
196};
197
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100198const struct value_string gprs_ns2_aff_cause_prim_strs[] = {
199 { NS_AFF_CAUSE_VC_FAILURE, "NSVC failure" },
200 { NS_AFF_CAUSE_VC_RECOVERY, "NSVC recovery" },
201 { NS_AFF_CAUSE_FAILURE, "NSE failure" },
202 { NS_AFF_CAUSE_RECOVERY, "NSE recovery" },
203 { NS_AFF_CAUSE_SNS_CONFIGURED, "NSE SNS configured" },
204 { NS_AFF_CAUSE_SNS_FAILURE, "NSE SNS failure" },
205 { 0, NULL }
206};
207
Alexander Couzens0ab028c2020-11-04 02:41:44 +0100208const struct value_string gprs_ns2_prim_strs[] = {
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100209 { PRIM_NS_UNIT_DATA, "UNIT DATA" },
210 { PRIM_NS_CONGESTION, "CONGESTION" },
211 { PRIM_NS_STATUS, "STATUS" },
212 { 0, NULL }
213};
214
Harald Welte5bef2cc2020-09-18 22:33:24 +0200215/*! string-format a given NS-VC into a user-supplied buffer.
216 * \param[in] buf user-allocated output buffer
217 * \param[in] buf_len size of user-allocated output buffer in bytes
218 * \param[in] nsvc NS-VC to be string-formatted
219 * \return pointer to buf on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200220char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc)
221{
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200222 const struct osmo_sockaddr *local;
223 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200224 struct osmo_sockaddr_str local_str;
225 struct osmo_sockaddr_str remote_str;
226
227 if (!buf_len)
Harald Welte92ad0292020-09-18 22:34:24 +0200228 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200229
230 switch (nsvc->ll) {
231 case GPRS_NS_LL_UDP:
232 if (!gprs_ns2_is_ip_bind(nsvc->bind)) {
233 buf[0] = '\0';
234 return buf;
235 }
236
237 local = gprs_ns2_ip_bind_sockaddr(nsvc->bind);
Alexander Couzensc4229a42020-10-11 20:58:04 +0200238 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200239 if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas))
240 strcpy(local_str.ip, "invalid");
241 if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas))
242 strcpy(remote_str.ip, "invalid");
243
244 if (nsvc->nsvci_is_valid)
245 snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u",
246 local_str.ip, local_str.port,
247 nsvc->nsvci,
248 remote_str.ip, remote_str.port);
249 else
250 snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u",
251 local_str.ip, local_str.port,
252 remote_str.ip, remote_str.port);
253 break;
254 case GPRS_NS_LL_FR_GRE:
255 snprintf(buf, buf_len, "frgre)");
256 break;
257 case GPRS_NS_LL_E1:
258 snprintf(buf, buf_len, "e1)");
259 break;
Alexander Couzens841817e2020-11-19 00:41:29 +0100260 case GPRS_NS_LL_FR:
261 if (!gprs_ns2_is_frgre_bind(nsvc->bind)) {
262 buf[0] = '\0';
263 return buf;
264 }
265
266 snprintf(buf, buf_len, "fr)netif: %s dlci: %u", gprs_ns2_fr_bind_netif(nsvc->bind),
267 gprs_ns2_fr_nsvc_dlci(nsvc));
268 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200269 default:
270 buf[0] = '\0';
271 break;
272 }
273
274 buf[buf_len - 1] = '\0';
275
276 return buf;
277}
278
279/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
280#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
281
Harald Welte5bef2cc2020-09-18 22:33:24 +0200282/*! string-format a given NS-VC to a thread-local static buffer.
283 * \param[in] nsvc NS-VC to be string-formatted
284 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200285const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
286{
287 static __thread char buf[NS2_LL_MAX_STR];
288 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
289}
290
Harald Welte5bef2cc2020-09-18 22:33:24 +0200291/*! string-format a given NS-VC to a dynamically allocated string.
292 * \param[in] ctx talloc context from which to allocate
293 * \param[in] nsvc NS-VC to be string-formatted
294 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200295char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
296{
297 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
298 if (!buf)
299 return buf;
300 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
301}
302
Daniel Willmannf1286542020-11-03 23:03:33 +0100303/*! Return the current state name of a given NS-VC to a thread-local static buffer.
304 * \param[in] nsvc NS-VC to return the state of
305 * \return pointer to the string on success; NULL on error */
306const char *gprs_ns2_nsvc_state_name(struct gprs_ns2_vc *nsvc)
307{
308 return osmo_fsm_inst_state_name(nsvc->fi);
309}
310
Harald Welte5bef2cc2020-09-18 22:33:24 +0200311/*! Receive a primitive from the NS User (Gb).
312 * \param[in] nsi NS instance to which the primitive is issued
313 * \param[in] oph The primitive
314 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200315int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
316{
317 /* TODO: implement load distribution function */
318 /* TODO: implement resource distribution */
319 /* TODO: check for empty PDUs which can be sent to Request/Confirm
320 * the IP endpoint */
321 struct osmo_gprs_ns2_prim *nsp;
322 struct gprs_ns2_nse *nse = NULL;
323 struct gprs_ns2_vc *nsvc = NULL, *tmp;
324 uint16_t bvci, nsei;
325 uint8_t sducontrol = 0;
326
327 if (oph->sap != SAP_NS)
328 return -EINVAL;
329
330 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
331
332 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != PRIM_NS_UNIT_DATA)
333 return -EINVAL;
334
335 if (!oph->msg)
336 return -EINVAL;
337
338 bvci = nsp->bvci;
339 nsei = nsp->nsei;
340
341 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
342 if (!nse)
343 return -EINVAL;
344
345 llist_for_each_entry(tmp, &nse->nsvc, list) {
346 if (!gprs_ns2_vc_is_unblocked(tmp))
347 continue;
348 if (bvci == 0 && tmp->sig_weight == 0)
349 continue;
350 if (bvci != 0 && tmp->data_weight == 0)
351 continue;
352
353 nsvc = tmp;
354 }
355
356 /* TODO: send a status primitive back */
357 if (!nsvc)
358 return 0;
359
360 if (nsp->u.unitdata.change == NS_ENDPOINT_REQUEST_CHANGE)
361 sducontrol = 1;
362 else if (nsp->u.unitdata.change == NS_ENDPOINT_CONFIRM_CHANGE)
363 sducontrol = 2;
364
365 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
366}
367
Harald Welte5bef2cc2020-09-18 22:33:24 +0200368/*! Send a STATUS.ind primitive to the specified NS instance user.
369 * \param[in] nsi NS instance on which we operate
370 * \param[in] nsei NSEI to which the statue relates
371 * \param[in] bvci BVCI to which the status relates
372 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200373void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
Daniel Willmann15c09a82020-11-03 23:05:43 +0100374 struct gprs_ns2_vc *nsvc,
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200375 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200376 enum gprs_ns2_affecting_cause cause)
377{
Daniel Willmann15c09a82020-11-03 23:05:43 +0100378 char nsvc_str[NS2_LL_MAX_STR];
Alexander Couzens6a161492020-07-12 13:45:50 +0200379 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200380 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200381 nsp.bvci = bvci;
382 nsp.u.status.cause = cause;
383 nsp.u.status.transfer = -1;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200384 nsp.u.status.first = nse->first;
385 nsp.u.status.persistent = nse->persistent;
Daniel Willmann15c09a82020-11-03 23:05:43 +0100386 if (nsvc)
387 nsp.u.status.nsvc = gprs_ns2_ll_str_buf(nsvc_str, sizeof(nsvc_str), nsvc);
388
Alexander Couzens6a161492020-07-12 13:45:50 +0200389 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS,
390 PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200391 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200392}
393
Harald Welte5bef2cc2020-09-18 22:33:24 +0200394/*! Allocate a NS-VC within the given bind + NSE.
395 * \param[in] bind The 'bind' on which we operate
396 * \param[in] nse The NS Entity on which we operate
397 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
398 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200399struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater)
400{
401 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
402
403 if (!nsvc)
404 return NULL;
405
406 nsvc->bind = bind;
407 nsvc->nse = nse;
408 nsvc->mode = bind->vc_mode;
409 nsvc->sig_weight = 1;
410 nsvc->data_weight = 1;
411
412 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx);
413 if (!nsvc->ctrg) {
414 goto err;
415 }
416 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx);
417 if (!nsvc->statg)
418 goto err_group;
419 if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater))
420 goto err_statg;
421
422 bind->nsi->rate_ctr_idx++;
423
424 llist_add(&nsvc->list, &nse->nsvc);
425 llist_add(&nsvc->blist, &bind->nsvc);
426
427 return nsvc;
428
429err_statg:
430 osmo_stat_item_group_free(nsvc->statg);
431err_group:
432 rate_ctr_group_free(nsvc->ctrg);
433err:
434 talloc_free(nsvc);
435
436 return NULL;
437}
438
Harald Welte5bef2cc2020-09-18 22:33:24 +0200439/*! Destroy/release given NS-VC.
440 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200441void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
442{
443 if (!nsvc)
444 return;
445
Daniel Willmann15c09a82020-11-03 23:05:43 +0100446 ns2_prim_status_ind(nsvc->nse, nsvc, 0, NS_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200447
448 llist_del(&nsvc->list);
449 llist_del(&nsvc->blist);
450
451 /* notify nse this nsvc is unavailable */
452 ns2_nse_notify_unblocked(nsvc, false);
453
454 /* check if sns is using this VC */
455 ns2_sns_free_nsvc(nsvc);
456 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
457
458 /* let the driver/bind clean up it's internal state */
459 if (nsvc->priv && nsvc->bind->free_vc)
460 nsvc->bind->free_vc(nsvc);
461
462 osmo_stat_item_group_free(nsvc->statg);
463 rate_ctr_group_free(nsvc->ctrg);
464
465 talloc_free(nsvc);
466}
467
Harald Welte5bef2cc2020-09-18 22:33:24 +0200468/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200469struct msgb *gprs_ns2_msgb_alloc(void)
470{
471 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
472 "GPRS/NS");
473 if (!msg) {
474 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
475 NS_ALLOC_SIZE);
476 }
477 return msg;
478}
479
Harald Welte5bef2cc2020-09-18 22:33:24 +0200480/*! Create a status message to be sent over a new connection.
481 * \param[in] orig_msg the original message
482 * \param[in] tp TLVP parsed of the original message
483 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
484 * \param[in] cause Cause for the rejection
485 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200486static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
487{
488 struct msgb *msg = gprs_ns2_msgb_alloc();
489 struct gprs_ns_hdr *nsh;
490 bool have_vci = false;
491 uint8_t _cause = cause;
492 uint16_t nsei = 0;
493
494 if (!msg)
495 return -ENOMEM;
496
497 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
498 nsei = tlvp_val16be(tp, NS_IE_NSEI);
499
500 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
501 nsei, gprs_ns2_cause_str(cause));
502 }
503
504 msg->l2h = msgb_put(msg, sizeof(*nsh));
505 nsh = (struct gprs_ns_hdr *) msg->l2h;
506 nsh->pdu_type = NS_PDUT_STATUS;
507
508 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
509 have_vci = TLVP_PRESENT(tp, NS_IE_VCI);
510
511 /* Section 9.2.7.1: Static conditions for NS-VCI */
512 if (cause == NS_CAUSE_NSVC_BLOCKED ||
513 cause == NS_CAUSE_NSVC_UNKNOWN) {
514 if (!have_vci) {
515 msgb_free(msg);
516 return -EINVAL;
517 }
518
519 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
520 }
521
522 /* Section 9.2.7.2: Static conditions for NS PDU */
523 switch (cause) {
524 case NS_CAUSE_SEM_INCORR_PDU:
525 case NS_CAUSE_PDU_INCOMP_PSTATE:
526 case NS_CAUSE_PROTO_ERR_UNSPEC:
527 case NS_CAUSE_INVAL_ESSENT_IE:
528 case NS_CAUSE_MISSING_ESSENT_IE:
529 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
530 orig_msg->l2h);
531 break;
532 default:
533 break;
534 }
535
536 *reject = msg;
537 return 0;
538}
539
Harald Welte5bef2cc2020-09-18 22:33:24 +0200540/*! Resolve a NS Entity based on its NSEI.
541 * \param[in] nsi NS Instance in which we do the look-up
542 * \param[in] nsei NSEI to look up
543 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200544struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
545{
546 struct gprs_ns2_nse *nse;
547
548 llist_for_each_entry(nse, &nsi->nse, list) {
549 if (nse->nsei == nsei)
550 return nse;
551 }
552
553 return NULL;
554}
555
Harald Welte5bef2cc2020-09-18 22:33:24 +0200556/*! Resolve a NS-VC Entity based on its NS-VCI.
557 * \param[in] nsi NS Instance in which we do the look-up
558 * \param[in] nsvci NS-VCI to look up
559 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200560struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
561{
562 struct gprs_ns2_nse *nse;
563 struct gprs_ns2_vc *nsvc;
564
565 llist_for_each_entry(nse, &nsi->nse, list) {
566 llist_for_each_entry(nsvc, &nse->nsvc, list) {
567 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
568 return nsvc;
569 }
570 }
571
572 return NULL;
573}
574
Harald Welte5bef2cc2020-09-18 22:33:24 +0200575/*! Create a NS Entity within given NS instance.
576 * \param[in] nsi NS instance in which to create NS Entity
577 * \param[in] nsei NS Entity Identifier of to-be-created NSE
578 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200579struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei)
580{
581 struct gprs_ns2_nse *nse;
582
583 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
584 if (nse) {
585 LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei);
586 return nse;
587 }
588
589 nse = talloc_zero(nsi, struct gprs_ns2_nse);
590 if (!nse)
591 return NULL;
592
593 nse->nsei = nsei;
594 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200595 nse->first = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200596 llist_add(&nse->list, &nsi->nse);
597 INIT_LLIST_HEAD(&nse->nsvc);
598
599 return nse;
600}
601
Alexander Couzens05e7f7d2020-10-11 19:51:46 +0200602/*! Return the NSEI
603 * \param[in] nse NS Entity
604 * \return the nsei.
605 */
606uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse)
607{
608 return nse->nsei;
609}
610
Harald Welte5bef2cc2020-09-18 22:33:24 +0200611/*! Destroy given NS Entity.
612 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200613void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
614{
615 struct gprs_ns2_vc *nsvc, *tmp;
616
617 if (!nse)
618 return;
619
620 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
621 gprs_ns2_free_nsvc(nsvc);
622 }
623
Daniel Willmann15c09a82020-11-03 23:05:43 +0100624 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200625
626 llist_del(&nse->list);
627 if (nse->bss_sns_fi)
628 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
629 talloc_free(nse);
630}
631
Alexander Couzens4b6c8af2020-10-11 20:15:25 +0200632void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi)
633{
634 struct gprs_ns2_nse *nse, *ntmp;
635
636 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
637 gprs_ns2_free_nse(nse);
638 }
639}
640
Alexander Couzens6a161492020-07-12 13:45:50 +0200641static inline int ns2_tlv_parse(struct tlv_parsed *dec,
642 const uint8_t *buf, int buf_len, uint8_t lv_tag,
643 uint8_t lv_tag2)
644{
645 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
646 * See comment of ns_att_tlvdef1. */
647 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
648 if (rc < 0)
649 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
650 return rc;
651}
652
653
Harald Welte5bef2cc2020-09-18 22:33:24 +0200654/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
655 * \param[in] bind the bind through which msg was received
656 * \param[in] msg the actual received message
657 * \param[in] logname A name to describe the VC. E.g. ip address pair
658 * \param[out] reject A message filled to be sent back. Only used in failure cases.
659 * \param[out] success A pointer which will be set to the new VC on success
660 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzens6a161492020-07-12 13:45:50 +0200661enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
662 struct msgb *msg,
663 const char *logname,
664 struct msgb **reject,
665 struct gprs_ns2_vc **success)
666{
667 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
668 struct tlv_parsed tp;
669 struct gprs_ns2_vc *nsvc;
670 struct gprs_ns2_nse *nse;
671 uint16_t nsvci;
672 uint16_t nsei;
673
674 int rc;
675
676 if (msg->len < sizeof(struct gprs_ns_hdr))
677 return GPRS_NS2_CS_ERROR;
678
679 if (nsh->pdu_type == NS_PDUT_STATUS) {
680 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
681 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
682 "for non-existing NS-VC\n",
683 logname);
684 return GPRS_NS2_CS_SKIPPED;
685 }
686
687 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
688 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
689 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
690 "for non-existing NS-VC\n",
691 logname);
692 return GPRS_NS2_CS_SKIPPED;
693 }
694
695 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
696 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
697 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
698 "for non-existing NS-VC\n",
699 logname);
700 return GPRS_NS2_CS_SKIPPED;
701 }
702
Alexander Couzens48f63862020-09-12 02:19:19 +0200703 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
704 /* Only the RESET procedure creates a new NSVC */
705 if (nsh->pdu_type != NS_PDUT_RESET) {
706 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200707
Alexander Couzens48f63862020-09-12 02:19:19 +0200708 if (rc < 0) {
709 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
710 return rc;
711 }
712 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200713 }
Alexander Couzens48f63862020-09-12 02:19:19 +0200714 } else { /* NS2_VC_MODE_ALIVE */
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200715 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens48f63862020-09-12 02:19:19 +0200716
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200717 if (rc < 0) {
718 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
719 return rc;
Alexander Couzens48f63862020-09-12 02:19:19 +0200720 }
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200721 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200722 }
723
724 rc = ns2_tlv_parse(&tp, nsh->data,
725 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
726 if (rc < 0) {
727 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
728 "TLV Parse\n", rc);
729 /* TODO: send invalid message back */
730 return GPRS_NS2_CS_REJECTED;
731 }
732
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200733 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
734 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
735 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
736 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
737 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200738 }
739
740 /* find or create NSE */
741 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
742 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
743 if (!nse) {
744 if (!bind->nsi->create_nse) {
745 return GPRS_NS2_CS_SKIPPED;
746 }
747
748 nse = gprs_ns2_create_nse(bind->nsi, nsei);
749 if (!nse) {
750 return GPRS_NS2_CS_ERROR;
751 }
752 }
753
754 nsvc = ns2_vc_alloc(bind, nse, false);
755 if (!nsvc)
756 return GPRS_NS2_CS_SKIPPED;
757
758 nsvc->ll = GPRS_NS_LL_UDP;
759
760 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
761 nsvc->nsvci = nsvci;
762 nsvc->nsvci_is_valid = true;
763
764 *success = nsvc;
765
766 return GPRS_NS2_CS_CREATED;
767}
768
Harald Welte5bef2cc2020-09-18 22:33:24 +0200769/*! Create, and connect an inactive, new IP-based NS-VC
770 * \param[in] bind bind in which the new NS-VC is to be created
771 * \param[in] remote remote address to which to connect
772 * \param[in] nse NS Entity in which the NS-VC is to be created
773 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
774 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200775struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700776 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200777 struct gprs_ns2_nse *nse,
778 uint16_t nsvci)
779{
780 struct gprs_ns2_vc *nsvc;
781
782 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
783 if (!nsvc)
784 return NULL;
785
786 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
787 nsvc->nsvci = nsvci;
788 nsvc->nsvci_is_valid = true;
789 }
790
791 return nsvc;
792}
793
Harald Welte5bef2cc2020-09-18 22:33:24 +0200794/*! Create, connect and activate a new IP-based NS-VC
795 * \param[in] bind bind in which the new NS-VC is to be created
796 * \param[in] remote remote address to which to connect
797 * \param[in] nse NS Entity in which the NS-VC is to be created
798 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
799 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200800struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700801 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200802 struct gprs_ns2_nse *nse,
803 uint16_t nsvci)
804{
805 struct gprs_ns2_vc *nsvc;
806 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
807 if (!nsvc)
808 return NULL;
809
810 gprs_ns2_vc_fsm_start(nsvc);
811
812 return nsvc;
813}
814
Harald Welte5bef2cc2020-09-18 22:33:24 +0200815/*! Create, connect and activate a new IP-based NS-VC
816 * \param[in] bind bind in which the new NS-VC is to be created
817 * \param[in] remote remote address to which to connect
818 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
819 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
820 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200821struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700822 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200823 uint16_t nsei,
824 uint16_t nsvci)
825{
826 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
827
828 if (!nse) {
829 nse = gprs_ns2_create_nse(bind->nsi, nsei);
830 if (!nse)
831 return NULL;
832 }
833
834 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
835}
836
Harald Welte5bef2cc2020-09-18 22:33:24 +0200837/*! Create, connect and activate a new IP-SNS NSE.
838 * \param[in] bind bind in which the new NS-VC is to be created
839 * \param[in] remote remote address to which to connect
840 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
841 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200842int gprs_ns2_ip_connect_sns(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700843 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200844 uint16_t nsei)
845{
846 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
847 struct gprs_ns2_vc *nsvc;
848
849 if (!nse) {
850 nse = gprs_ns2_create_nse(bind->nsi, nsei);
851 if (!nse)
852 return -1;
853 }
854
855 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
856 if (!nsvc)
857 return -1;
858
859 if (!nse->bss_sns_fi)
860 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, NULL);
861
862 if (!nse->bss_sns_fi)
863 return -1;
864
865 return ns2_sns_bss_fsm_start(nse, nsvc, remote);
866}
867
Harald Welte5bef2cc2020-09-18 22:33:24 +0200868/*! Find NS-VC for given socket address.
869 * \param[in] nse NS Entity in which to search
870 * \param[in] sockaddr socket address to search for
871 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +0200872struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700873 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +0200874{
875 struct gprs_ns2_vc *nsvc;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200876 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200877
878 OSMO_ASSERT(nse);
879 OSMO_ASSERT(sockaddr);
880
881 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200882 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200883 if (!osmo_sockaddr_cmp(sockaddr, remote))
884 return nsvc;
885 }
886
887 return NULL;
888}
889
Alexander Couzens6cb5d5f2020-10-11 23:23:31 +0200890/*!
891 * Iterate over all nsvc of a NS Entity and call the callback.
892 * If the callback returns < 0 it aborts the loop and returns the callback return code.
893 * \param[in] nse NS Entity to iterate over all nsvcs
894 * \param[in] cb the callback to call
895 * \param[inout] cb_data the private data of the callback
896 * \return 0 if the loop completes. If a callback returns < 0 it will returns this value.
897 */
898int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data)
899{
900 struct gprs_ns2_vc *nsvc, *tmp;
901 int rc = 0;
902 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
903 rc = cb(nsvc, cb_data);
904 if (rc < 0)
905 return rc;
906 }
907
908 return 0;
909}
910
911
Alexander Couzens6a161492020-07-12 13:45:50 +0200912
Harald Welte5bef2cc2020-09-18 22:33:24 +0200913/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +0200914 * \param[in] nsvc NS-VC for which the message was received
915 * \param msg the received message. Ownership is trasnferred, caller must not free it!
916 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +0200917int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200918 struct msgb *msg)
919{
920 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
921 struct tlv_parsed tp;
922 int rc = 0;
923
924 if (msg->len < sizeof(struct gprs_ns_hdr))
925 return -EINVAL;
926
927 switch (nsh->pdu_type) {
928 case SNS_PDUT_CONFIG:
929 /* one additional byte ('end flag') before the TLV part starts */
930 rc = ns2_tlv_parse(&tp, nsh->data+1,
931 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
932 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200933 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200934 return rc;
935 }
936 /* All sub-network service related message types */
937 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
938 break;
939 case SNS_PDUT_ACK:
940 case SNS_PDUT_ADD:
941 case SNS_PDUT_CHANGE_WEIGHT:
942 case SNS_PDUT_DELETE:
943 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +0200944 rc = ns2_tlv_parse(&tp, nsh->data+5,
945 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200946 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200947 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200948 return rc;
949 }
950 tp.lv[NS_IE_NSEI].val = nsh->data+2;
951 tp.lv[NS_IE_NSEI].len = 2;
952 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
953 tp.lv[NS_IE_TRANS_ID].len = 1;
954 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
955 break;
956 case SNS_PDUT_CONFIG_ACK:
957 case SNS_PDUT_SIZE:
958 case SNS_PDUT_SIZE_ACK:
959 rc = ns2_tlv_parse(&tp, nsh->data,
960 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
961 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200962 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200963 return rc;
964 }
965 /* All sub-network service related message types */
966 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
967 break;
968
969 case NS_PDUT_UNITDATA:
970 rc = gprs_ns2_vc_rx(nsvc, msg, NULL);
971 break;
972 default:
973 rc = ns2_tlv_parse(&tp, nsh->data,
974 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
975 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200976 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200977 if (nsh->pdu_type != NS_PDUT_STATUS)
978 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
979 return rc;
980 }
981 rc = gprs_ns2_vc_rx(nsvc, msg, &tp);
982 break;
983 }
984
985 return rc;
986}
987
Harald Welte5bef2cc2020-09-18 22:33:24 +0200988/*! Notify a nse about the change of a NS-VC.
989 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
990 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +0200991void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
992{
993 struct gprs_ns2_nse *nse = nsvc->nse;
994 struct gprs_ns2_vc *tmp;
995
996 if (unblocked == nse->alive)
997 return;
998
999 if (unblocked) {
1000 /* this is the first unblocked NSVC on an unavailable NSE */
1001 nse->alive = true;
Daniel Willmann15c09a82020-11-03 23:05:43 +01001002 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +02001003 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +02001004 return;
1005 }
1006
1007 /* check if there are any remaining alive vcs */
1008 llist_for_each_entry(tmp, &nse->nsvc, list) {
1009 if (tmp == nsvc)
1010 continue;
1011
1012 if (gprs_ns2_vc_is_unblocked(tmp)) {
1013 /* there is at least one remaining alive NSVC */
1014 return;
1015 }
1016 }
1017
1018 /* nse became unavailable */
1019 nse->alive = false;
Daniel Willmann15c09a82020-11-03 23:05:43 +01001020 ns2_prim_status_ind(nse, NULL, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001021}
1022
1023/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +02001024 * \param[in] ctx a talloc context to allocate NS instance from
Alexander Couzenscce88282020-10-26 00:25:50 +01001025 * \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 +02001026 * \param[in] cb_data transparent user data passed to Call-back
1027 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001028struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
1029{
1030 struct gprs_ns2_inst *nsi;
1031
1032 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
1033 if (!nsi)
1034 return NULL;
1035
1036 nsi->cb = cb;
1037 nsi->cb_data = cb_data;
1038 INIT_LLIST_HEAD(&nsi->binding);
1039 INIT_LLIST_HEAD(&nsi->nse);
1040
1041 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1042 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1043 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1044 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1045 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1046 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1047 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
1048 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
1049
1050 return nsi;
1051}
1052
Harald Welte5bef2cc2020-09-18 22:33:24 +02001053/*! Destroy a NS Instance (including all its NSEs, binds, ...).
1054 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001055void gprs_ns2_free(struct gprs_ns2_inst *nsi)
1056{
Alexander Couzens6a161492020-07-12 13:45:50 +02001057 if (!nsi)
1058 return;
1059
Alexander Couzens4b6c8af2020-10-11 20:15:25 +02001060 gprs_ns2_free_nses(nsi);
Alexander Couzens896fcd52020-10-11 19:52:36 +02001061 gprs_ns2_free_binds(nsi);
Alexander Couzens35315042020-10-10 03:28:17 +02001062
1063 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001064}
1065
Harald Welte5bef2cc2020-09-18 22:33:24 +02001066/*! Configure whether a NS Instance should dynamically create NSEs based on incoming traffic.
1067 * \param nsi the instance to modify
1068 * \param create_nse if NSE can be created on receiving package. SGSN set this.
1069 * \return 0 on success; negative on error
Alexander Couzens6a161492020-07-12 13:45:50 +02001070 */
1071int gprs_ns2_dynamic_create_nse(struct gprs_ns2_inst *nsi, bool create_nse)
1072{
1073 nsi->create_nse = create_nse;
1074
1075 return 0;
1076}
1077
Harald Welte5bef2cc2020-09-18 22:33:24 +02001078/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1079 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001080void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1081{
1082 struct gprs_ns2_vc *nsvc;
1083 OSMO_ASSERT(nse);
1084
1085 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1086 if (nsvc->sns_only)
1087 continue;
1088
1089 gprs_ns2_vc_fsm_start(nsvc);
1090 }
1091}
1092
Harald Welte5bef2cc2020-09-18 22:33:24 +02001093/*! Set the mode of given bind.
1094 * \param[in] bind the bind we want to set the mode of
1095 * \param[in] modde mode to set bind to */
Alexander Couzens6a161492020-07-12 13:45:50 +02001096void gprs_ns2_bind_set_mode(struct gprs_ns2_vc_bind *bind, enum gprs_ns2_vc_mode mode)
1097{
1098 bind->vc_mode = mode;
1099}
1100
Harald Welte5bef2cc2020-09-18 22:33:24 +02001101/*! Destroy a given bind.
1102 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001103void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1104{
1105 struct gprs_ns2_vc *nsvc, *tmp;
1106 if (!bind)
1107 return;
1108
1109 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1110 gprs_ns2_free_nsvc(nsvc);
1111 }
1112
1113 if (bind->driver->free_bind)
1114 bind->driver->free_bind(bind);
1115
1116 llist_del(&bind->list);
1117 talloc_free(bind);
1118}
Alexander Couzens896fcd52020-10-11 19:52:36 +02001119
1120void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi)
1121{
1122 struct gprs_ns2_vc_bind *bind, *tbind;
1123
1124 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1125 gprs_ns2_free_bind(bind);
1126 }
1127}
Alexander Couzens6a161492020-07-12 13:45:50 +02001128/*! @} */