blob: b50daf4c2a9014a866dcc831beebe3e930f98730 [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
Daniel Willmannf1286542020-11-03 23:03:33 +0100295/*! Return the current state name of a given NS-VC to a thread-local static buffer.
296 * \param[in] nsvc NS-VC to return the state of
297 * \return pointer to the string on success; NULL on error */
298const char *gprs_ns2_nsvc_state_name(struct gprs_ns2_vc *nsvc)
299{
300 return osmo_fsm_inst_state_name(nsvc->fi);
301}
302
Harald Welte5bef2cc2020-09-18 22:33:24 +0200303/*! Receive a primitive from the NS User (Gb).
304 * \param[in] nsi NS instance to which the primitive is issued
305 * \param[in] oph The primitive
306 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200307int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
308{
309 /* TODO: implement load distribution function */
310 /* TODO: implement resource distribution */
311 /* TODO: check for empty PDUs which can be sent to Request/Confirm
312 * the IP endpoint */
313 struct osmo_gprs_ns2_prim *nsp;
314 struct gprs_ns2_nse *nse = NULL;
315 struct gprs_ns2_vc *nsvc = NULL, *tmp;
316 uint16_t bvci, nsei;
317 uint8_t sducontrol = 0;
318
319 if (oph->sap != SAP_NS)
320 return -EINVAL;
321
322 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
323
324 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != PRIM_NS_UNIT_DATA)
325 return -EINVAL;
326
327 if (!oph->msg)
328 return -EINVAL;
329
330 bvci = nsp->bvci;
331 nsei = nsp->nsei;
332
333 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
334 if (!nse)
335 return -EINVAL;
336
337 llist_for_each_entry(tmp, &nse->nsvc, list) {
338 if (!gprs_ns2_vc_is_unblocked(tmp))
339 continue;
340 if (bvci == 0 && tmp->sig_weight == 0)
341 continue;
342 if (bvci != 0 && tmp->data_weight == 0)
343 continue;
344
345 nsvc = tmp;
346 }
347
348 /* TODO: send a status primitive back */
349 if (!nsvc)
350 return 0;
351
352 if (nsp->u.unitdata.change == NS_ENDPOINT_REQUEST_CHANGE)
353 sducontrol = 1;
354 else if (nsp->u.unitdata.change == NS_ENDPOINT_CONFIRM_CHANGE)
355 sducontrol = 2;
356
357 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
358}
359
Harald Welte5bef2cc2020-09-18 22:33:24 +0200360/*! Send a STATUS.ind primitive to the specified NS instance user.
361 * \param[in] nsi NS instance on which we operate
362 * \param[in] nsei NSEI to which the statue relates
363 * \param[in] bvci BVCI to which the status relates
364 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200365void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
366 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200367 enum gprs_ns2_affecting_cause cause)
368{
369 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200370 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200371 nsp.bvci = bvci;
372 nsp.u.status.cause = cause;
373 nsp.u.status.transfer = -1;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200374 nsp.u.status.first = nse->first;
375 nsp.u.status.persistent = nse->persistent;
Alexander Couzens6a161492020-07-12 13:45:50 +0200376 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS,
377 PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200378 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200379}
380
Harald Welte5bef2cc2020-09-18 22:33:24 +0200381/*! Allocate a NS-VC within the given bind + NSE.
382 * \param[in] bind The 'bind' on which we operate
383 * \param[in] nse The NS Entity on which we operate
384 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
385 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200386struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater)
387{
388 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
389
390 if (!nsvc)
391 return NULL;
392
393 nsvc->bind = bind;
394 nsvc->nse = nse;
395 nsvc->mode = bind->vc_mode;
396 nsvc->sig_weight = 1;
397 nsvc->data_weight = 1;
398
399 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx);
400 if (!nsvc->ctrg) {
401 goto err;
402 }
403 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx);
404 if (!nsvc->statg)
405 goto err_group;
406 if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater))
407 goto err_statg;
408
409 bind->nsi->rate_ctr_idx++;
410
411 llist_add(&nsvc->list, &nse->nsvc);
412 llist_add(&nsvc->blist, &bind->nsvc);
413
414 return nsvc;
415
416err_statg:
417 osmo_stat_item_group_free(nsvc->statg);
418err_group:
419 rate_ctr_group_free(nsvc->ctrg);
420err:
421 talloc_free(nsvc);
422
423 return NULL;
424}
425
Harald Welte5bef2cc2020-09-18 22:33:24 +0200426/*! Destroy/release given NS-VC.
427 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200428void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
429{
430 if (!nsvc)
431 return;
432
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200433 ns2_prim_status_ind(nsvc->nse, 0, NS_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200434
435 llist_del(&nsvc->list);
436 llist_del(&nsvc->blist);
437
438 /* notify nse this nsvc is unavailable */
439 ns2_nse_notify_unblocked(nsvc, false);
440
441 /* check if sns is using this VC */
442 ns2_sns_free_nsvc(nsvc);
443 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
444
445 /* let the driver/bind clean up it's internal state */
446 if (nsvc->priv && nsvc->bind->free_vc)
447 nsvc->bind->free_vc(nsvc);
448
449 osmo_stat_item_group_free(nsvc->statg);
450 rate_ctr_group_free(nsvc->ctrg);
451
452 talloc_free(nsvc);
453}
454
Harald Welte5bef2cc2020-09-18 22:33:24 +0200455/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200456struct msgb *gprs_ns2_msgb_alloc(void)
457{
458 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
459 "GPRS/NS");
460 if (!msg) {
461 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
462 NS_ALLOC_SIZE);
463 }
464 return msg;
465}
466
Harald Welte5bef2cc2020-09-18 22:33:24 +0200467/*! Create a status message to be sent over a new connection.
468 * \param[in] orig_msg the original message
469 * \param[in] tp TLVP parsed of the original message
470 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
471 * \param[in] cause Cause for the rejection
472 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200473static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
474{
475 struct msgb *msg = gprs_ns2_msgb_alloc();
476 struct gprs_ns_hdr *nsh;
477 bool have_vci = false;
478 uint8_t _cause = cause;
479 uint16_t nsei = 0;
480
481 if (!msg)
482 return -ENOMEM;
483
484 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
485 nsei = tlvp_val16be(tp, NS_IE_NSEI);
486
487 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
488 nsei, gprs_ns2_cause_str(cause));
489 }
490
491 msg->l2h = msgb_put(msg, sizeof(*nsh));
492 nsh = (struct gprs_ns_hdr *) msg->l2h;
493 nsh->pdu_type = NS_PDUT_STATUS;
494
495 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
496 have_vci = TLVP_PRESENT(tp, NS_IE_VCI);
497
498 /* Section 9.2.7.1: Static conditions for NS-VCI */
499 if (cause == NS_CAUSE_NSVC_BLOCKED ||
500 cause == NS_CAUSE_NSVC_UNKNOWN) {
501 if (!have_vci) {
502 msgb_free(msg);
503 return -EINVAL;
504 }
505
506 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
507 }
508
509 /* Section 9.2.7.2: Static conditions for NS PDU */
510 switch (cause) {
511 case NS_CAUSE_SEM_INCORR_PDU:
512 case NS_CAUSE_PDU_INCOMP_PSTATE:
513 case NS_CAUSE_PROTO_ERR_UNSPEC:
514 case NS_CAUSE_INVAL_ESSENT_IE:
515 case NS_CAUSE_MISSING_ESSENT_IE:
516 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
517 orig_msg->l2h);
518 break;
519 default:
520 break;
521 }
522
523 *reject = msg;
524 return 0;
525}
526
Harald Welte5bef2cc2020-09-18 22:33:24 +0200527/*! Resolve a NS Entity based on its NSEI.
528 * \param[in] nsi NS Instance in which we do the look-up
529 * \param[in] nsei NSEI to look up
530 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200531struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
532{
533 struct gprs_ns2_nse *nse;
534
535 llist_for_each_entry(nse, &nsi->nse, list) {
536 if (nse->nsei == nsei)
537 return nse;
538 }
539
540 return NULL;
541}
542
Harald Welte5bef2cc2020-09-18 22:33:24 +0200543/*! Resolve a NS-VC Entity based on its NS-VCI.
544 * \param[in] nsi NS Instance in which we do the look-up
545 * \param[in] nsvci NS-VCI to look up
546 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200547struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
548{
549 struct gprs_ns2_nse *nse;
550 struct gprs_ns2_vc *nsvc;
551
552 llist_for_each_entry(nse, &nsi->nse, list) {
553 llist_for_each_entry(nsvc, &nse->nsvc, list) {
554 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
555 return nsvc;
556 }
557 }
558
559 return NULL;
560}
561
Harald Welte5bef2cc2020-09-18 22:33:24 +0200562/*! Create a NS Entity within given NS instance.
563 * \param[in] nsi NS instance in which to create NS Entity
564 * \param[in] nsei NS Entity Identifier of to-be-created NSE
565 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200566struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei)
567{
568 struct gprs_ns2_nse *nse;
569
570 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
571 if (nse) {
572 LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei);
573 return nse;
574 }
575
576 nse = talloc_zero(nsi, struct gprs_ns2_nse);
577 if (!nse)
578 return NULL;
579
580 nse->nsei = nsei;
581 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200582 nse->first = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200583 llist_add(&nse->list, &nsi->nse);
584 INIT_LLIST_HEAD(&nse->nsvc);
585
586 return nse;
587}
588
Alexander Couzens05e7f7d2020-10-11 19:51:46 +0200589/*! Return the NSEI
590 * \param[in] nse NS Entity
591 * \return the nsei.
592 */
593uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse)
594{
595 return nse->nsei;
596}
597
Harald Welte5bef2cc2020-09-18 22:33:24 +0200598/*! Destroy given NS Entity.
599 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200600void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
601{
602 struct gprs_ns2_vc *nsvc, *tmp;
603
604 if (!nse)
605 return;
606
607 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
608 gprs_ns2_free_nsvc(nsvc);
609 }
610
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200611 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200612
613 llist_del(&nse->list);
614 if (nse->bss_sns_fi)
615 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
616 talloc_free(nse);
617}
618
Alexander Couzens4b6c8af2020-10-11 20:15:25 +0200619void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi)
620{
621 struct gprs_ns2_nse *nse, *ntmp;
622
623 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
624 gprs_ns2_free_nse(nse);
625 }
626}
627
Alexander Couzens6a161492020-07-12 13:45:50 +0200628static inline int ns2_tlv_parse(struct tlv_parsed *dec,
629 const uint8_t *buf, int buf_len, uint8_t lv_tag,
630 uint8_t lv_tag2)
631{
632 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
633 * See comment of ns_att_tlvdef1. */
634 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
635 if (rc < 0)
636 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
637 return rc;
638}
639
640
Harald Welte5bef2cc2020-09-18 22:33:24 +0200641/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
642 * \param[in] bind the bind through which msg was received
643 * \param[in] msg the actual received message
644 * \param[in] logname A name to describe the VC. E.g. ip address pair
645 * \param[out] reject A message filled to be sent back. Only used in failure cases.
646 * \param[out] success A pointer which will be set to the new VC on success
647 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzens6a161492020-07-12 13:45:50 +0200648enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
649 struct msgb *msg,
650 const char *logname,
651 struct msgb **reject,
652 struct gprs_ns2_vc **success)
653{
654 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
655 struct tlv_parsed tp;
656 struct gprs_ns2_vc *nsvc;
657 struct gprs_ns2_nse *nse;
658 uint16_t nsvci;
659 uint16_t nsei;
660
661 int rc;
662
663 if (msg->len < sizeof(struct gprs_ns_hdr))
664 return GPRS_NS2_CS_ERROR;
665
666 if (nsh->pdu_type == NS_PDUT_STATUS) {
667 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
668 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS 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_ALIVE_ACK) {
675 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
676 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
677 "for non-existing NS-VC\n",
678 logname);
679 return GPRS_NS2_CS_SKIPPED;
680 }
681
682 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
683 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
684 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
685 "for non-existing NS-VC\n",
686 logname);
687 return GPRS_NS2_CS_SKIPPED;
688 }
689
Alexander Couzens48f63862020-09-12 02:19:19 +0200690 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
691 /* Only the RESET procedure creates a new NSVC */
692 if (nsh->pdu_type != NS_PDUT_RESET) {
693 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200694
Alexander Couzens48f63862020-09-12 02:19:19 +0200695 if (rc < 0) {
696 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
697 return rc;
698 }
699 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200700 }
Alexander Couzens48f63862020-09-12 02:19:19 +0200701 } else { /* NS2_VC_MODE_ALIVE */
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200702 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens48f63862020-09-12 02:19:19 +0200703
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200704 if (rc < 0) {
705 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
706 return rc;
Alexander Couzens48f63862020-09-12 02:19:19 +0200707 }
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200708 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200709 }
710
711 rc = ns2_tlv_parse(&tp, nsh->data,
712 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
713 if (rc < 0) {
714 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
715 "TLV Parse\n", rc);
716 /* TODO: send invalid message back */
717 return GPRS_NS2_CS_REJECTED;
718 }
719
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200720 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
721 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
722 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
723 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
724 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200725 }
726
727 /* find or create NSE */
728 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
729 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
730 if (!nse) {
731 if (!bind->nsi->create_nse) {
732 return GPRS_NS2_CS_SKIPPED;
733 }
734
735 nse = gprs_ns2_create_nse(bind->nsi, nsei);
736 if (!nse) {
737 return GPRS_NS2_CS_ERROR;
738 }
739 }
740
741 nsvc = ns2_vc_alloc(bind, nse, false);
742 if (!nsvc)
743 return GPRS_NS2_CS_SKIPPED;
744
745 nsvc->ll = GPRS_NS_LL_UDP;
746
747 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
748 nsvc->nsvci = nsvci;
749 nsvc->nsvci_is_valid = true;
750
751 *success = nsvc;
752
753 return GPRS_NS2_CS_CREATED;
754}
755
Harald Welte5bef2cc2020-09-18 22:33:24 +0200756/*! Create, and connect an inactive, new IP-based NS-VC
757 * \param[in] bind bind in which the new NS-VC is to be created
758 * \param[in] remote remote address to which to connect
759 * \param[in] nse NS Entity in which the NS-VC is to be created
760 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
761 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200762struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700763 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200764 struct gprs_ns2_nse *nse,
765 uint16_t nsvci)
766{
767 struct gprs_ns2_vc *nsvc;
768
769 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
770 if (!nsvc)
771 return NULL;
772
773 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
774 nsvc->nsvci = nsvci;
775 nsvc->nsvci_is_valid = true;
776 }
777
778 return nsvc;
779}
780
Harald Welte5bef2cc2020-09-18 22:33:24 +0200781/*! Create, connect and activate a new IP-based NS-VC
782 * \param[in] bind bind in which the new NS-VC is to be created
783 * \param[in] remote remote address to which to connect
784 * \param[in] nse NS Entity in which the NS-VC is to be created
785 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
786 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200787struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700788 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200789 struct gprs_ns2_nse *nse,
790 uint16_t nsvci)
791{
792 struct gprs_ns2_vc *nsvc;
793 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
794 if (!nsvc)
795 return NULL;
796
797 gprs_ns2_vc_fsm_start(nsvc);
798
799 return nsvc;
800}
801
Harald Welte5bef2cc2020-09-18 22:33:24 +0200802/*! Create, connect and activate a new IP-based NS-VC
803 * \param[in] bind bind in which the new NS-VC is to be created
804 * \param[in] remote remote address to which to connect
805 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
806 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
807 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200808struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700809 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200810 uint16_t nsei,
811 uint16_t nsvci)
812{
813 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
814
815 if (!nse) {
816 nse = gprs_ns2_create_nse(bind->nsi, nsei);
817 if (!nse)
818 return NULL;
819 }
820
821 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
822}
823
Harald Welte5bef2cc2020-09-18 22:33:24 +0200824/*! Create, connect and activate a new IP-SNS NSE.
825 * \param[in] bind bind in which the new NS-VC is to be created
826 * \param[in] remote remote address to which to connect
827 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
828 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200829int gprs_ns2_ip_connect_sns(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700830 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200831 uint16_t nsei)
832{
833 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
834 struct gprs_ns2_vc *nsvc;
835
836 if (!nse) {
837 nse = gprs_ns2_create_nse(bind->nsi, nsei);
838 if (!nse)
839 return -1;
840 }
841
842 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
843 if (!nsvc)
844 return -1;
845
846 if (!nse->bss_sns_fi)
847 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, NULL);
848
849 if (!nse->bss_sns_fi)
850 return -1;
851
852 return ns2_sns_bss_fsm_start(nse, nsvc, remote);
853}
854
Harald Welte5bef2cc2020-09-18 22:33:24 +0200855/*! Find NS-VC for given socket address.
856 * \param[in] nse NS Entity in which to search
857 * \param[in] sockaddr socket address to search for
858 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +0200859struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700860 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +0200861{
862 struct gprs_ns2_vc *nsvc;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200863 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200864
865 OSMO_ASSERT(nse);
866 OSMO_ASSERT(sockaddr);
867
868 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200869 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200870 if (!osmo_sockaddr_cmp(sockaddr, remote))
871 return nsvc;
872 }
873
874 return NULL;
875}
876
Alexander Couzens6cb5d5f2020-10-11 23:23:31 +0200877/*!
878 * Iterate over all nsvc of a NS Entity and call the callback.
879 * If the callback returns < 0 it aborts the loop and returns the callback return code.
880 * \param[in] nse NS Entity to iterate over all nsvcs
881 * \param[in] cb the callback to call
882 * \param[inout] cb_data the private data of the callback
883 * \return 0 if the loop completes. If a callback returns < 0 it will returns this value.
884 */
885int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data)
886{
887 struct gprs_ns2_vc *nsvc, *tmp;
888 int rc = 0;
889 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
890 rc = cb(nsvc, cb_data);
891 if (rc < 0)
892 return rc;
893 }
894
895 return 0;
896}
897
898
Alexander Couzens6a161492020-07-12 13:45:50 +0200899
Harald Welte5bef2cc2020-09-18 22:33:24 +0200900/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +0200901 * \param[in] nsvc NS-VC for which the message was received
902 * \param msg the received message. Ownership is trasnferred, caller must not free it!
903 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +0200904int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200905 struct msgb *msg)
906{
907 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
908 struct tlv_parsed tp;
909 int rc = 0;
910
911 if (msg->len < sizeof(struct gprs_ns_hdr))
912 return -EINVAL;
913
914 switch (nsh->pdu_type) {
915 case SNS_PDUT_CONFIG:
916 /* one additional byte ('end flag') before the TLV part starts */
917 rc = ns2_tlv_parse(&tp, nsh->data+1,
918 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
919 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200920 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200921 return rc;
922 }
923 /* All sub-network service related message types */
924 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
925 break;
926 case SNS_PDUT_ACK:
927 case SNS_PDUT_ADD:
928 case SNS_PDUT_CHANGE_WEIGHT:
929 case SNS_PDUT_DELETE:
930 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +0200931 rc = ns2_tlv_parse(&tp, nsh->data+5,
932 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200933 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200934 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200935 return rc;
936 }
937 tp.lv[NS_IE_NSEI].val = nsh->data+2;
938 tp.lv[NS_IE_NSEI].len = 2;
939 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
940 tp.lv[NS_IE_TRANS_ID].len = 1;
941 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
942 break;
943 case SNS_PDUT_CONFIG_ACK:
944 case SNS_PDUT_SIZE:
945 case SNS_PDUT_SIZE_ACK:
946 rc = ns2_tlv_parse(&tp, nsh->data,
947 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
948 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200949 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200950 return rc;
951 }
952 /* All sub-network service related message types */
953 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
954 break;
955
956 case NS_PDUT_UNITDATA:
957 rc = gprs_ns2_vc_rx(nsvc, msg, NULL);
958 break;
959 default:
960 rc = ns2_tlv_parse(&tp, nsh->data,
961 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
962 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200963 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200964 if (nsh->pdu_type != NS_PDUT_STATUS)
965 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
966 return rc;
967 }
968 rc = gprs_ns2_vc_rx(nsvc, msg, &tp);
969 break;
970 }
971
972 return rc;
973}
974
Harald Welte5bef2cc2020-09-18 22:33:24 +0200975/*! Notify a nse about the change of a NS-VC.
976 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
977 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +0200978void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
979{
980 struct gprs_ns2_nse *nse = nsvc->nse;
981 struct gprs_ns2_vc *tmp;
982
983 if (unblocked == nse->alive)
984 return;
985
986 if (unblocked) {
987 /* this is the first unblocked NSVC on an unavailable NSE */
988 nse->alive = true;
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200989 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +0200990 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200991 return;
992 }
993
994 /* check if there are any remaining alive vcs */
995 llist_for_each_entry(tmp, &nse->nsvc, list) {
996 if (tmp == nsvc)
997 continue;
998
999 if (gprs_ns2_vc_is_unblocked(tmp)) {
1000 /* there is at least one remaining alive NSVC */
1001 return;
1002 }
1003 }
1004
1005 /* nse became unavailable */
1006 nse->alive = false;
Alexander Couzensbf95f0f2020-10-01 22:56:03 +02001007 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001008}
1009
1010/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +02001011 * \param[in] ctx a talloc context to allocate NS instance from
Alexander Couzenscce88282020-10-26 00:25:50 +01001012 * \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 +02001013 * \param[in] cb_data transparent user data passed to Call-back
1014 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001015struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
1016{
1017 struct gprs_ns2_inst *nsi;
1018
1019 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
1020 if (!nsi)
1021 return NULL;
1022
1023 nsi->cb = cb;
1024 nsi->cb_data = cb_data;
1025 INIT_LLIST_HEAD(&nsi->binding);
1026 INIT_LLIST_HEAD(&nsi->nse);
1027
1028 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1029 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1030 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1031 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1032 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1033 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1034 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
1035 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
1036
1037 return nsi;
1038}
1039
Harald Welte5bef2cc2020-09-18 22:33:24 +02001040/*! Destroy a NS Instance (including all its NSEs, binds, ...).
1041 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001042void gprs_ns2_free(struct gprs_ns2_inst *nsi)
1043{
Alexander Couzens6a161492020-07-12 13:45:50 +02001044 if (!nsi)
1045 return;
1046
Alexander Couzens4b6c8af2020-10-11 20:15:25 +02001047 gprs_ns2_free_nses(nsi);
Alexander Couzens896fcd52020-10-11 19:52:36 +02001048 gprs_ns2_free_binds(nsi);
Alexander Couzens35315042020-10-10 03:28:17 +02001049
1050 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001051}
1052
Harald Welte5bef2cc2020-09-18 22:33:24 +02001053/*! Configure whether a NS Instance should dynamically create NSEs based on incoming traffic.
1054 * \param nsi the instance to modify
1055 * \param create_nse if NSE can be created on receiving package. SGSN set this.
1056 * \return 0 on success; negative on error
Alexander Couzens6a161492020-07-12 13:45:50 +02001057 */
1058int gprs_ns2_dynamic_create_nse(struct gprs_ns2_inst *nsi, bool create_nse)
1059{
1060 nsi->create_nse = create_nse;
1061
1062 return 0;
1063}
1064
Harald Welte5bef2cc2020-09-18 22:33:24 +02001065/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1066 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001067void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1068{
1069 struct gprs_ns2_vc *nsvc;
1070 OSMO_ASSERT(nse);
1071
1072 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1073 if (nsvc->sns_only)
1074 continue;
1075
1076 gprs_ns2_vc_fsm_start(nsvc);
1077 }
1078}
1079
Harald Welte5bef2cc2020-09-18 22:33:24 +02001080/*! Set the mode of given bind.
1081 * \param[in] bind the bind we want to set the mode of
1082 * \param[in] modde mode to set bind to */
Alexander Couzens6a161492020-07-12 13:45:50 +02001083void gprs_ns2_bind_set_mode(struct gprs_ns2_vc_bind *bind, enum gprs_ns2_vc_mode mode)
1084{
1085 bind->vc_mode = mode;
1086}
1087
Harald Welte5bef2cc2020-09-18 22:33:24 +02001088/*! Destroy a given bind.
1089 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001090void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1091{
1092 struct gprs_ns2_vc *nsvc, *tmp;
1093 if (!bind)
1094 return;
1095
1096 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1097 gprs_ns2_free_nsvc(nsvc);
1098 }
1099
1100 if (bind->driver->free_bind)
1101 bind->driver->free_bind(bind);
1102
1103 llist_del(&bind->list);
1104 talloc_free(bind);
1105}
Alexander Couzens896fcd52020-10-11 19:52:36 +02001106
1107void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi)
1108{
1109 struct gprs_ns2_vc_bind *bind, *tbind;
1110
1111 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1112 gprs_ns2_free_bind(bind);
1113 }
1114}
Alexander Couzens6a161492020-07-12 13:45:50 +02001115/*! @} */