blob: 403b8b7789ef699f3a238ee34b0003b70904bc4a [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
62 * - Only UDP is supported as of now, no frame relay support
63 * - There are no BLOCK and UNBLOCK timers (yet?)
64 *
65 * \file gprs_ns2.c */
66
67#include <stdlib.h>
68#include <unistd.h>
69#include <errno.h>
70#include <stdint.h>
71
72#include <sys/types.h>
73#include <sys/socket.h>
74#include <arpa/inet.h>
75
76#include <osmocom/core/fsm.h>
77#include <osmocom/core/msgb.h>
78#include <osmocom/core/rate_ctr.h>
79#include <osmocom/core/socket.h>
80#include <osmocom/core/sockaddr_str.h>
81#include <osmocom/core/stats.h>
82#include <osmocom/core/stat_item.h>
83#include <osmocom/core/talloc.h>
84#include <osmocom/gprs/gprs_msgb.h>
85#include <osmocom/gsm/prim.h>
86#include <osmocom/gsm/tlv.h>
87
88#include "gprs_ns2_internal.h"
89
90#define ns_set_state(ns_, st_) ns_set_state_with_log(ns_, st_, false, __FILE__, __LINE__)
91#define ns_set_remote_state(ns_, st_) ns_set_state_with_log(ns_, st_, true, __FILE__, __LINE__)
92#define ns_mark_blocked(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_BLOCKED)
93#define ns_mark_unblocked(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_BLOCKED));
94#define ns_mark_alive(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_ALIVE)
95#define ns_mark_dead(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_ALIVE));
96
97/* HACK: The NS_IE_IP_ADDR does not follow any known TLV rules.
98 * Since it's a hard ABI break to implement 16 bit tag with fixed length entries to workaround it,
99 * the parser will be called with ns_att_tlvdef1 and if it's failed with ns_att_tlvdef2.
100 * The TLV parser depends on 8bit tag in many places.
101 * The NS_IE_IP_ADDR is only valid for SNS_ACK SNS_ADD and SNS_DELETE.
102 */
103static const struct tlv_definition ns_att_tlvdef1 = {
104 .def = {
105 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
106 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
107 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
108 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
109 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
110 [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 },
111 [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 },
112 [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 },
113 [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 },
114 [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 },
115 [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 },
116 /* NS_IE_IP_ADDR in the IPv4 version */
117 [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 5 },
118 },
119};
120
121static const struct tlv_definition ns_att_tlvdef2 = {
122 .def = {
123 [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 },
124 [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 },
125 [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 },
126 [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 },
127 [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 },
128 [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 },
129 [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 },
130 [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 },
131 [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 },
132 [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 },
133 [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 },
134 /* NS_IE_IP_ADDR in the IPv6 version */
135 [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 17 },
136 },
137};
138
139
140/* Section 10.3.2, Table 13 */
Alexander Couzensb3b837c2020-10-27 15:12:25 +0100141const struct value_string gprs_ns2_cause_strs[] = {
Alexander Couzens6a161492020-07-12 13:45:50 +0200142 { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" },
143 { NS_CAUSE_OM_INTERVENTION, "O&M intervention" },
144 { NS_CAUSE_EQUIP_FAIL, "Equipment failure" },
145 { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" },
146 { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" },
147 { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" },
148 { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
149 { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" },
150 { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
151 { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" },
152 { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" },
153 { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" },
154 { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" },
155 { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" },
156 { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" },
157 { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" },
158 { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" },
159 { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" },
160 { 0, NULL }
161};
162
Alexander Couzens6a161492020-07-12 13:45:50 +0200163static const struct rate_ctr_desc nsvc_ctr_description[] = {
164 { "packets:in", "Packets at NS Level ( In)" },
165 { "packets:out","Packets at NS Level (Out)" },
166 { "bytes:in", "Bytes at NS Level ( In)" },
167 { "bytes:out", "Bytes at NS Level (Out)" },
168 { "blocked", "NS-VC Block count " },
169 { "dead", "NS-VC gone dead count " },
170 { "replaced", "NS-VC replaced other count" },
171 { "nsei-chg", "NS-VC changed NSEI count " },
172 { "inv-nsvci", "NS-VCI was invalid count " },
173 { "inv-nsei", "NSEI was invalid count " },
174 { "lost:alive", "ALIVE ACK missing count " },
175 { "lost:reset", "RESET ACK missing count " },
176};
177
178static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
179 .group_name_prefix = "ns:nsvc",
180 .group_description = "NSVC Peer Statistics",
181 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
182 .ctr_desc = nsvc_ctr_description,
183 .class_id = OSMO_STATS_CLASS_PEER,
184};
185
186
187static const struct osmo_stat_item_desc nsvc_stat_description[] = {
188 { "alive.delay", "ALIVE response time ", "ms", 16, 0 },
189};
190
191static const struct osmo_stat_item_group_desc nsvc_statg_desc = {
192 .group_name_prefix = "ns.nsvc",
193 .group_description = "NSVC Peer Statistics",
194 .num_items = ARRAY_SIZE(nsvc_stat_description),
195 .item_desc = nsvc_stat_description,
196 .class_id = OSMO_STATS_CLASS_PEER,
197};
198
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100199const struct value_string gprs_ns2_aff_cause_prim_strs[] = {
200 { NS_AFF_CAUSE_VC_FAILURE, "NSVC failure" },
201 { NS_AFF_CAUSE_VC_RECOVERY, "NSVC recovery" },
202 { NS_AFF_CAUSE_FAILURE, "NSE failure" },
203 { NS_AFF_CAUSE_RECOVERY, "NSE recovery" },
204 { NS_AFF_CAUSE_SNS_CONFIGURED, "NSE SNS configured" },
205 { NS_AFF_CAUSE_SNS_FAILURE, "NSE SNS failure" },
206 { 0, NULL }
207};
208
209const struct value_string ns2_prim_str[] = {
210 { PRIM_NS_UNIT_DATA, "UNIT DATA" },
211 { PRIM_NS_CONGESTION, "CONGESTION" },
212 { PRIM_NS_STATUS, "STATUS" },
213 { 0, NULL }
214};
215
Harald Welte5bef2cc2020-09-18 22:33:24 +0200216/*! string-format a given NS-VC into a user-supplied buffer.
217 * \param[in] buf user-allocated output buffer
218 * \param[in] buf_len size of user-allocated output buffer in bytes
219 * \param[in] nsvc NS-VC to be string-formatted
220 * \return pointer to buf on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200221char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc)
222{
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200223 const struct osmo_sockaddr *local;
224 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200225 struct osmo_sockaddr_str local_str;
226 struct osmo_sockaddr_str remote_str;
227
228 if (!buf_len)
Harald Welte92ad0292020-09-18 22:34:24 +0200229 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200230
231 switch (nsvc->ll) {
232 case GPRS_NS_LL_UDP:
233 if (!gprs_ns2_is_ip_bind(nsvc->bind)) {
234 buf[0] = '\0';
235 return buf;
236 }
237
238 local = gprs_ns2_ip_bind_sockaddr(nsvc->bind);
Alexander Couzensc4229a42020-10-11 20:58:04 +0200239 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200240 if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas))
241 strcpy(local_str.ip, "invalid");
242 if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas))
243 strcpy(remote_str.ip, "invalid");
244
245 if (nsvc->nsvci_is_valid)
246 snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u",
247 local_str.ip, local_str.port,
248 nsvc->nsvci,
249 remote_str.ip, remote_str.port);
250 else
251 snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u",
252 local_str.ip, local_str.port,
253 remote_str.ip, remote_str.port);
254 break;
255 case GPRS_NS_LL_FR_GRE:
256 snprintf(buf, buf_len, "frgre)");
257 break;
258 case GPRS_NS_LL_E1:
259 snprintf(buf, buf_len, "e1)");
260 break;
261 default:
262 buf[0] = '\0';
263 break;
264 }
265
266 buf[buf_len - 1] = '\0';
267
268 return buf;
269}
270
271/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
272#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
273
Harald Welte5bef2cc2020-09-18 22:33:24 +0200274/*! string-format a given NS-VC to a thread-local static buffer.
275 * \param[in] nsvc NS-VC to be string-formatted
276 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200277const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
278{
279 static __thread char buf[NS2_LL_MAX_STR];
280 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
281}
282
Harald Welte5bef2cc2020-09-18 22:33:24 +0200283/*! string-format a given NS-VC to a dynamically allocated string.
284 * \param[in] ctx talloc context from which to allocate
285 * \param[in] nsvc NS-VC to be string-formatted
286 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200287char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
288{
289 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
290 if (!buf)
291 return buf;
292 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
293}
294
Harald Welte5bef2cc2020-09-18 22:33:24 +0200295/*! Receive a primitive from the NS User (Gb).
296 * \param[in] nsi NS instance to which the primitive is issued
297 * \param[in] oph The primitive
298 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200299int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
300{
301 /* TODO: implement load distribution function */
302 /* TODO: implement resource distribution */
303 /* TODO: check for empty PDUs which can be sent to Request/Confirm
304 * the IP endpoint */
305 struct osmo_gprs_ns2_prim *nsp;
306 struct gprs_ns2_nse *nse = NULL;
307 struct gprs_ns2_vc *nsvc = NULL, *tmp;
308 uint16_t bvci, nsei;
309 uint8_t sducontrol = 0;
310
311 if (oph->sap != SAP_NS)
312 return -EINVAL;
313
314 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
315
316 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != PRIM_NS_UNIT_DATA)
317 return -EINVAL;
318
319 if (!oph->msg)
320 return -EINVAL;
321
322 bvci = nsp->bvci;
323 nsei = nsp->nsei;
324
325 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
326 if (!nse)
327 return -EINVAL;
328
329 llist_for_each_entry(tmp, &nse->nsvc, list) {
330 if (!gprs_ns2_vc_is_unblocked(tmp))
331 continue;
332 if (bvci == 0 && tmp->sig_weight == 0)
333 continue;
334 if (bvci != 0 && tmp->data_weight == 0)
335 continue;
336
337 nsvc = tmp;
338 }
339
340 /* TODO: send a status primitive back */
341 if (!nsvc)
342 return 0;
343
344 if (nsp->u.unitdata.change == NS_ENDPOINT_REQUEST_CHANGE)
345 sducontrol = 1;
346 else if (nsp->u.unitdata.change == NS_ENDPOINT_CONFIRM_CHANGE)
347 sducontrol = 2;
348
349 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
350}
351
Harald Welte5bef2cc2020-09-18 22:33:24 +0200352/*! Send a STATUS.ind primitive to the specified NS instance user.
353 * \param[in] nsi NS instance on which we operate
354 * \param[in] nsei NSEI to which the statue relates
355 * \param[in] bvci BVCI to which the status relates
356 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200357void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
358 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200359 enum gprs_ns2_affecting_cause cause)
360{
361 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200362 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200363 nsp.bvci = bvci;
364 nsp.u.status.cause = cause;
365 nsp.u.status.transfer = -1;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200366 nsp.u.status.first = nse->first;
367 nsp.u.status.persistent = nse->persistent;
Alexander Couzens6a161492020-07-12 13:45:50 +0200368 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS,
369 PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200370 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200371}
372
Harald Welte5bef2cc2020-09-18 22:33:24 +0200373/*! Allocate a NS-VC within the given bind + NSE.
374 * \param[in] bind The 'bind' on which we operate
375 * \param[in] nse The NS Entity on which we operate
376 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
377 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200378struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater)
379{
380 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
381
382 if (!nsvc)
383 return NULL;
384
385 nsvc->bind = bind;
386 nsvc->nse = nse;
387 nsvc->mode = bind->vc_mode;
388 nsvc->sig_weight = 1;
389 nsvc->data_weight = 1;
390
391 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx);
392 if (!nsvc->ctrg) {
393 goto err;
394 }
395 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx);
396 if (!nsvc->statg)
397 goto err_group;
398 if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater))
399 goto err_statg;
400
401 bind->nsi->rate_ctr_idx++;
402
403 llist_add(&nsvc->list, &nse->nsvc);
404 llist_add(&nsvc->blist, &bind->nsvc);
405
406 return nsvc;
407
408err_statg:
409 osmo_stat_item_group_free(nsvc->statg);
410err_group:
411 rate_ctr_group_free(nsvc->ctrg);
412err:
413 talloc_free(nsvc);
414
415 return NULL;
416}
417
Harald Welte5bef2cc2020-09-18 22:33:24 +0200418/*! Destroy/release given NS-VC.
419 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200420void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
421{
422 if (!nsvc)
423 return;
424
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200425 ns2_prim_status_ind(nsvc->nse, 0, NS_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200426
427 llist_del(&nsvc->list);
428 llist_del(&nsvc->blist);
429
430 /* notify nse this nsvc is unavailable */
431 ns2_nse_notify_unblocked(nsvc, false);
432
433 /* check if sns is using this VC */
434 ns2_sns_free_nsvc(nsvc);
435 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
436
437 /* let the driver/bind clean up it's internal state */
438 if (nsvc->priv && nsvc->bind->free_vc)
439 nsvc->bind->free_vc(nsvc);
440
441 osmo_stat_item_group_free(nsvc->statg);
442 rate_ctr_group_free(nsvc->ctrg);
443
444 talloc_free(nsvc);
445}
446
Harald Welte5bef2cc2020-09-18 22:33:24 +0200447/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200448struct msgb *gprs_ns2_msgb_alloc(void)
449{
450 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
451 "GPRS/NS");
452 if (!msg) {
453 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
454 NS_ALLOC_SIZE);
455 }
456 return msg;
457}
458
Harald Welte5bef2cc2020-09-18 22:33:24 +0200459/*! Create a status message to be sent over a new connection.
460 * \param[in] orig_msg the original message
461 * \param[in] tp TLVP parsed of the original message
462 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
463 * \param[in] cause Cause for the rejection
464 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200465static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
466{
467 struct msgb *msg = gprs_ns2_msgb_alloc();
468 struct gprs_ns_hdr *nsh;
469 bool have_vci = false;
470 uint8_t _cause = cause;
471 uint16_t nsei = 0;
472
473 if (!msg)
474 return -ENOMEM;
475
476 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
477 nsei = tlvp_val16be(tp, NS_IE_NSEI);
478
479 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
480 nsei, gprs_ns2_cause_str(cause));
481 }
482
483 msg->l2h = msgb_put(msg, sizeof(*nsh));
484 nsh = (struct gprs_ns_hdr *) msg->l2h;
485 nsh->pdu_type = NS_PDUT_STATUS;
486
487 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
488 have_vci = TLVP_PRESENT(tp, NS_IE_VCI);
489
490 /* Section 9.2.7.1: Static conditions for NS-VCI */
491 if (cause == NS_CAUSE_NSVC_BLOCKED ||
492 cause == NS_CAUSE_NSVC_UNKNOWN) {
493 if (!have_vci) {
494 msgb_free(msg);
495 return -EINVAL;
496 }
497
498 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
499 }
500
501 /* Section 9.2.7.2: Static conditions for NS PDU */
502 switch (cause) {
503 case NS_CAUSE_SEM_INCORR_PDU:
504 case NS_CAUSE_PDU_INCOMP_PSTATE:
505 case NS_CAUSE_PROTO_ERR_UNSPEC:
506 case NS_CAUSE_INVAL_ESSENT_IE:
507 case NS_CAUSE_MISSING_ESSENT_IE:
508 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
509 orig_msg->l2h);
510 break;
511 default:
512 break;
513 }
514
515 *reject = msg;
516 return 0;
517}
518
Harald Welte5bef2cc2020-09-18 22:33:24 +0200519/*! Resolve a NS Entity based on its NSEI.
520 * \param[in] nsi NS Instance in which we do the look-up
521 * \param[in] nsei NSEI to look up
522 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200523struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
524{
525 struct gprs_ns2_nse *nse;
526
527 llist_for_each_entry(nse, &nsi->nse, list) {
528 if (nse->nsei == nsei)
529 return nse;
530 }
531
532 return NULL;
533}
534
Harald Welte5bef2cc2020-09-18 22:33:24 +0200535/*! Resolve a NS-VC Entity based on its NS-VCI.
536 * \param[in] nsi NS Instance in which we do the look-up
537 * \param[in] nsvci NS-VCI to look up
538 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200539struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
540{
541 struct gprs_ns2_nse *nse;
542 struct gprs_ns2_vc *nsvc;
543
544 llist_for_each_entry(nse, &nsi->nse, list) {
545 llist_for_each_entry(nsvc, &nse->nsvc, list) {
546 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
547 return nsvc;
548 }
549 }
550
551 return NULL;
552}
553
Harald Welte5bef2cc2020-09-18 22:33:24 +0200554/*! Create a NS Entity within given NS instance.
555 * \param[in] nsi NS instance in which to create NS Entity
556 * \param[in] nsei NS Entity Identifier of to-be-created NSE
557 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200558struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei)
559{
560 struct gprs_ns2_nse *nse;
561
562 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
563 if (nse) {
564 LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei);
565 return nse;
566 }
567
568 nse = talloc_zero(nsi, struct gprs_ns2_nse);
569 if (!nse)
570 return NULL;
571
572 nse->nsei = nsei;
573 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200574 nse->first = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200575 llist_add(&nse->list, &nsi->nse);
576 INIT_LLIST_HEAD(&nse->nsvc);
577
578 return nse;
579}
580
Alexander Couzens05e7f7d2020-10-11 19:51:46 +0200581/*! Return the NSEI
582 * \param[in] nse NS Entity
583 * \return the nsei.
584 */
585uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse)
586{
587 return nse->nsei;
588}
589
Harald Welte5bef2cc2020-09-18 22:33:24 +0200590/*! Destroy given NS Entity.
591 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200592void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
593{
594 struct gprs_ns2_vc *nsvc, *tmp;
595
596 if (!nse)
597 return;
598
599 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
600 gprs_ns2_free_nsvc(nsvc);
601 }
602
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200603 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200604
605 llist_del(&nse->list);
606 if (nse->bss_sns_fi)
607 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
608 talloc_free(nse);
609}
610
Alexander Couzens4b6c8af2020-10-11 20:15:25 +0200611void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi)
612{
613 struct gprs_ns2_nse *nse, *ntmp;
614
615 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
616 gprs_ns2_free_nse(nse);
617 }
618}
619
Alexander Couzens6a161492020-07-12 13:45:50 +0200620static inline int ns2_tlv_parse(struct tlv_parsed *dec,
621 const uint8_t *buf, int buf_len, uint8_t lv_tag,
622 uint8_t lv_tag2)
623{
624 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
625 * See comment of ns_att_tlvdef1. */
626 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
627 if (rc < 0)
628 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
629 return rc;
630}
631
632
Harald Welte5bef2cc2020-09-18 22:33:24 +0200633/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
634 * \param[in] bind the bind through which msg was received
635 * \param[in] msg the actual received message
636 * \param[in] logname A name to describe the VC. E.g. ip address pair
637 * \param[out] reject A message filled to be sent back. Only used in failure cases.
638 * \param[out] success A pointer which will be set to the new VC on success
639 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzens6a161492020-07-12 13:45:50 +0200640enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
641 struct msgb *msg,
642 const char *logname,
643 struct msgb **reject,
644 struct gprs_ns2_vc **success)
645{
646 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
647 struct tlv_parsed tp;
648 struct gprs_ns2_vc *nsvc;
649 struct gprs_ns2_nse *nse;
650 uint16_t nsvci;
651 uint16_t nsei;
652
653 int rc;
654
655 if (msg->len < sizeof(struct gprs_ns_hdr))
656 return GPRS_NS2_CS_ERROR;
657
658 if (nsh->pdu_type == NS_PDUT_STATUS) {
659 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
660 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
661 "for non-existing NS-VC\n",
662 logname);
663 return GPRS_NS2_CS_SKIPPED;
664 }
665
666 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
667 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
668 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
669 "for non-existing NS-VC\n",
670 logname);
671 return GPRS_NS2_CS_SKIPPED;
672 }
673
674 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
675 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
676 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
677 "for non-existing NS-VC\n",
678 logname);
679 return GPRS_NS2_CS_SKIPPED;
680 }
681
Alexander Couzens48f63862020-09-12 02:19:19 +0200682 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
683 /* Only the RESET procedure creates a new NSVC */
684 if (nsh->pdu_type != NS_PDUT_RESET) {
685 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200686
Alexander Couzens48f63862020-09-12 02:19:19 +0200687 if (rc < 0) {
688 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
689 return rc;
690 }
691 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200692 }
Alexander Couzens48f63862020-09-12 02:19:19 +0200693 } else { /* NS2_VC_MODE_ALIVE */
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200694 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens48f63862020-09-12 02:19:19 +0200695
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200696 if (rc < 0) {
697 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
698 return rc;
Alexander Couzens48f63862020-09-12 02:19:19 +0200699 }
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200700 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200701 }
702
703 rc = ns2_tlv_parse(&tp, nsh->data,
704 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
705 if (rc < 0) {
706 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
707 "TLV Parse\n", rc);
708 /* TODO: send invalid message back */
709 return GPRS_NS2_CS_REJECTED;
710 }
711
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200712 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
713 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
714 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
715 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
716 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200717 }
718
719 /* find or create NSE */
720 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
721 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
722 if (!nse) {
723 if (!bind->nsi->create_nse) {
724 return GPRS_NS2_CS_SKIPPED;
725 }
726
727 nse = gprs_ns2_create_nse(bind->nsi, nsei);
728 if (!nse) {
729 return GPRS_NS2_CS_ERROR;
730 }
731 }
732
733 nsvc = ns2_vc_alloc(bind, nse, false);
734 if (!nsvc)
735 return GPRS_NS2_CS_SKIPPED;
736
737 nsvc->ll = GPRS_NS_LL_UDP;
738
739 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
740 nsvc->nsvci = nsvci;
741 nsvc->nsvci_is_valid = true;
742
743 *success = nsvc;
744
745 return GPRS_NS2_CS_CREATED;
746}
747
Harald Welte5bef2cc2020-09-18 22:33:24 +0200748/*! Create, and connect an inactive, new IP-based NS-VC
749 * \param[in] bind bind in which the new NS-VC is to be created
750 * \param[in] remote remote address to which to connect
751 * \param[in] nse NS Entity in which the NS-VC is to be created
752 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
753 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200754struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700755 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200756 struct gprs_ns2_nse *nse,
757 uint16_t nsvci)
758{
759 struct gprs_ns2_vc *nsvc;
760
761 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
762 if (!nsvc)
763 return NULL;
764
765 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
766 nsvc->nsvci = nsvci;
767 nsvc->nsvci_is_valid = true;
768 }
769
770 return nsvc;
771}
772
Harald Welte5bef2cc2020-09-18 22:33:24 +0200773/*! Create, connect and activate a new IP-based NS-VC
774 * \param[in] bind bind in which the new NS-VC is to be created
775 * \param[in] remote remote address to which to connect
776 * \param[in] nse NS Entity in which the NS-VC is to be created
777 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
778 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200779struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700780 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200781 struct gprs_ns2_nse *nse,
782 uint16_t nsvci)
783{
784 struct gprs_ns2_vc *nsvc;
785 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
786 if (!nsvc)
787 return NULL;
788
789 gprs_ns2_vc_fsm_start(nsvc);
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] nsei NSEI of the 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_connect2(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 uint16_t nsei,
803 uint16_t nsvci)
804{
805 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
806
807 if (!nse) {
808 nse = gprs_ns2_create_nse(bind->nsi, nsei);
809 if (!nse)
810 return NULL;
811 }
812
813 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
814}
815
Harald Welte5bef2cc2020-09-18 22:33:24 +0200816/*! Create, connect and activate a new IP-SNS NSE.
817 * \param[in] bind bind in which the new NS-VC is to be created
818 * \param[in] remote remote address to which to connect
819 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
820 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200821int gprs_ns2_ip_connect_sns(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{
825 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
826 struct gprs_ns2_vc *nsvc;
827
828 if (!nse) {
829 nse = gprs_ns2_create_nse(bind->nsi, nsei);
830 if (!nse)
831 return -1;
832 }
833
834 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
835 if (!nsvc)
836 return -1;
837
838 if (!nse->bss_sns_fi)
839 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, NULL);
840
841 if (!nse->bss_sns_fi)
842 return -1;
843
844 return ns2_sns_bss_fsm_start(nse, nsvc, remote);
845}
846
Harald Welte5bef2cc2020-09-18 22:33:24 +0200847/*! Find NS-VC for given socket address.
848 * \param[in] nse NS Entity in which to search
849 * \param[in] sockaddr socket address to search for
850 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +0200851struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700852 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +0200853{
854 struct gprs_ns2_vc *nsvc;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200855 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200856
857 OSMO_ASSERT(nse);
858 OSMO_ASSERT(sockaddr);
859
860 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200861 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200862 if (!osmo_sockaddr_cmp(sockaddr, remote))
863 return nsvc;
864 }
865
866 return NULL;
867}
868
Alexander Couzens6cb5d5f2020-10-11 23:23:31 +0200869/*!
870 * Iterate over all nsvc of a NS Entity and call the callback.
871 * If the callback returns < 0 it aborts the loop and returns the callback return code.
872 * \param[in] nse NS Entity to iterate over all nsvcs
873 * \param[in] cb the callback to call
874 * \param[inout] cb_data the private data of the callback
875 * \return 0 if the loop completes. If a callback returns < 0 it will returns this value.
876 */
877int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data)
878{
879 struct gprs_ns2_vc *nsvc, *tmp;
880 int rc = 0;
881 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
882 rc = cb(nsvc, cb_data);
883 if (rc < 0)
884 return rc;
885 }
886
887 return 0;
888}
889
890
Alexander Couzens6a161492020-07-12 13:45:50 +0200891
Harald Welte5bef2cc2020-09-18 22:33:24 +0200892/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +0200893 * \param[in] nsvc NS-VC for which the message was received
894 * \param msg the received message. Ownership is trasnferred, caller must not free it!
895 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +0200896int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200897 struct msgb *msg)
898{
899 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
900 struct tlv_parsed tp;
901 int rc = 0;
902
903 if (msg->len < sizeof(struct gprs_ns_hdr))
904 return -EINVAL;
905
906 switch (nsh->pdu_type) {
907 case SNS_PDUT_CONFIG:
908 /* one additional byte ('end flag') before the TLV part starts */
909 rc = ns2_tlv_parse(&tp, nsh->data+1,
910 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
911 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200912 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200913 return rc;
914 }
915 /* All sub-network service related message types */
916 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
917 break;
918 case SNS_PDUT_ACK:
919 case SNS_PDUT_ADD:
920 case SNS_PDUT_CHANGE_WEIGHT:
921 case SNS_PDUT_DELETE:
922 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +0200923 rc = ns2_tlv_parse(&tp, nsh->data+5,
924 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200925 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200926 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200927 return rc;
928 }
929 tp.lv[NS_IE_NSEI].val = nsh->data+2;
930 tp.lv[NS_IE_NSEI].len = 2;
931 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
932 tp.lv[NS_IE_TRANS_ID].len = 1;
933 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
934 break;
935 case SNS_PDUT_CONFIG_ACK:
936 case SNS_PDUT_SIZE:
937 case SNS_PDUT_SIZE_ACK:
938 rc = ns2_tlv_parse(&tp, nsh->data,
939 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
940 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200941 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200942 return rc;
943 }
944 /* All sub-network service related message types */
945 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
946 break;
947
948 case NS_PDUT_UNITDATA:
949 rc = gprs_ns2_vc_rx(nsvc, msg, NULL);
950 break;
951 default:
952 rc = ns2_tlv_parse(&tp, nsh->data,
953 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
954 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200955 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200956 if (nsh->pdu_type != NS_PDUT_STATUS)
957 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
958 return rc;
959 }
960 rc = gprs_ns2_vc_rx(nsvc, msg, &tp);
961 break;
962 }
963
964 return rc;
965}
966
Harald Welte5bef2cc2020-09-18 22:33:24 +0200967/*! Notify a nse about the change of a NS-VC.
968 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
969 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +0200970void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
971{
972 struct gprs_ns2_nse *nse = nsvc->nse;
973 struct gprs_ns2_vc *tmp;
974
975 if (unblocked == nse->alive)
976 return;
977
978 if (unblocked) {
979 /* this is the first unblocked NSVC on an unavailable NSE */
980 nse->alive = true;
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200981 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +0200982 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200983 return;
984 }
985
986 /* check if there are any remaining alive vcs */
987 llist_for_each_entry(tmp, &nse->nsvc, list) {
988 if (tmp == nsvc)
989 continue;
990
991 if (gprs_ns2_vc_is_unblocked(tmp)) {
992 /* there is at least one remaining alive NSVC */
993 return;
994 }
995 }
996
997 /* nse became unavailable */
998 nse->alive = false;
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200999 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001000}
1001
1002/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +02001003 * \param[in] ctx a talloc context to allocate NS instance from
Alexander Couzenscce88282020-10-26 00:25:50 +01001004 * \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 +02001005 * \param[in] cb_data transparent user data passed to Call-back
1006 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001007struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
1008{
1009 struct gprs_ns2_inst *nsi;
1010
1011 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
1012 if (!nsi)
1013 return NULL;
1014
1015 nsi->cb = cb;
1016 nsi->cb_data = cb_data;
1017 INIT_LLIST_HEAD(&nsi->binding);
1018 INIT_LLIST_HEAD(&nsi->nse);
1019
1020 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1021 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1022 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1023 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1024 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1025 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1026 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
1027 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
1028
1029 return nsi;
1030}
1031
Harald Welte5bef2cc2020-09-18 22:33:24 +02001032/*! Destroy a NS Instance (including all its NSEs, binds, ...).
1033 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001034void gprs_ns2_free(struct gprs_ns2_inst *nsi)
1035{
Alexander Couzens6a161492020-07-12 13:45:50 +02001036 if (!nsi)
1037 return;
1038
Alexander Couzens4b6c8af2020-10-11 20:15:25 +02001039 gprs_ns2_free_nses(nsi);
Alexander Couzens896fcd52020-10-11 19:52:36 +02001040 gprs_ns2_free_binds(nsi);
Alexander Couzens35315042020-10-10 03:28:17 +02001041
1042 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001043}
1044
Harald Welte5bef2cc2020-09-18 22:33:24 +02001045/*! Configure whether a NS Instance should dynamically create NSEs based on incoming traffic.
1046 * \param nsi the instance to modify
1047 * \param create_nse if NSE can be created on receiving package. SGSN set this.
1048 * \return 0 on success; negative on error
Alexander Couzens6a161492020-07-12 13:45:50 +02001049 */
1050int gprs_ns2_dynamic_create_nse(struct gprs_ns2_inst *nsi, bool create_nse)
1051{
1052 nsi->create_nse = create_nse;
1053
1054 return 0;
1055}
1056
Harald Welte5bef2cc2020-09-18 22:33:24 +02001057/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1058 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001059void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1060{
1061 struct gprs_ns2_vc *nsvc;
1062 OSMO_ASSERT(nse);
1063
1064 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1065 if (nsvc->sns_only)
1066 continue;
1067
1068 gprs_ns2_vc_fsm_start(nsvc);
1069 }
1070}
1071
Harald Welte5bef2cc2020-09-18 22:33:24 +02001072/*! Set the mode of given bind.
1073 * \param[in] bind the bind we want to set the mode of
1074 * \param[in] modde mode to set bind to */
Alexander Couzens6a161492020-07-12 13:45:50 +02001075void gprs_ns2_bind_set_mode(struct gprs_ns2_vc_bind *bind, enum gprs_ns2_vc_mode mode)
1076{
1077 bind->vc_mode = mode;
1078}
1079
Harald Welte5bef2cc2020-09-18 22:33:24 +02001080/*! Destroy a given bind.
1081 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001082void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1083{
1084 struct gprs_ns2_vc *nsvc, *tmp;
1085 if (!bind)
1086 return;
1087
1088 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1089 gprs_ns2_free_nsvc(nsvc);
1090 }
1091
1092 if (bind->driver->free_bind)
1093 bind->driver->free_bind(bind);
1094
1095 llist_del(&bind->list);
1096 talloc_free(bind);
1097}
Alexander Couzens896fcd52020-10-11 19:52:36 +02001098
1099void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi)
1100{
1101 struct gprs_ns2_vc_bind *bind, *tbind;
1102
1103 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1104 gprs_ns2_free_bind(bind);
1105 }
1106}
Alexander Couzens6a161492020-07-12 13:45:50 +02001107/*! @} */