blob: 6401d7711bde9bad54ab907e7ae6df3f5c59e7f2 [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 */
141static const struct value_string ns2_cause_str[] = {
142 { 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
163/*! Obtain a human-readable string for NS cause value */
164const char *gprs_ns2_cause_str(int cause)
165{
166 enum ns_cause _cause = cause;
167 return get_value_string(ns2_cause_str, _cause);
168}
169
170static const struct rate_ctr_desc nsvc_ctr_description[] = {
171 { "packets:in", "Packets at NS Level ( In)" },
172 { "packets:out","Packets at NS Level (Out)" },
173 { "bytes:in", "Bytes at NS Level ( In)" },
174 { "bytes:out", "Bytes at NS Level (Out)" },
175 { "blocked", "NS-VC Block count " },
176 { "dead", "NS-VC gone dead count " },
177 { "replaced", "NS-VC replaced other count" },
178 { "nsei-chg", "NS-VC changed NSEI count " },
179 { "inv-nsvci", "NS-VCI was invalid count " },
180 { "inv-nsei", "NSEI was invalid count " },
181 { "lost:alive", "ALIVE ACK missing count " },
182 { "lost:reset", "RESET ACK missing count " },
183};
184
185static const struct rate_ctr_group_desc nsvc_ctrg_desc = {
186 .group_name_prefix = "ns:nsvc",
187 .group_description = "NSVC Peer Statistics",
188 .num_ctr = ARRAY_SIZE(nsvc_ctr_description),
189 .ctr_desc = nsvc_ctr_description,
190 .class_id = OSMO_STATS_CLASS_PEER,
191};
192
193
194static const struct osmo_stat_item_desc nsvc_stat_description[] = {
195 { "alive.delay", "ALIVE response time ", "ms", 16, 0 },
196};
197
198static const struct osmo_stat_item_group_desc nsvc_statg_desc = {
199 .group_name_prefix = "ns.nsvc",
200 .group_description = "NSVC Peer Statistics",
201 .num_items = ARRAY_SIZE(nsvc_stat_description),
202 .item_desc = nsvc_stat_description,
203 .class_id = OSMO_STATS_CLASS_PEER,
204};
205
Alexander Couzens2498f1d2020-10-27 01:09:01 +0100206const struct value_string gprs_ns2_aff_cause_prim_strs[] = {
207 { NS_AFF_CAUSE_VC_FAILURE, "NSVC failure" },
208 { NS_AFF_CAUSE_VC_RECOVERY, "NSVC recovery" },
209 { NS_AFF_CAUSE_FAILURE, "NSE failure" },
210 { NS_AFF_CAUSE_RECOVERY, "NSE recovery" },
211 { NS_AFF_CAUSE_SNS_CONFIGURED, "NSE SNS configured" },
212 { NS_AFF_CAUSE_SNS_FAILURE, "NSE SNS failure" },
213 { 0, NULL }
214};
215
216const struct value_string ns2_prim_str[] = {
217 { PRIM_NS_UNIT_DATA, "UNIT DATA" },
218 { PRIM_NS_CONGESTION, "CONGESTION" },
219 { PRIM_NS_STATUS, "STATUS" },
220 { 0, NULL }
221};
222
Harald Welte5bef2cc2020-09-18 22:33:24 +0200223/*! string-format a given NS-VC into a user-supplied buffer.
224 * \param[in] buf user-allocated output buffer
225 * \param[in] buf_len size of user-allocated output buffer in bytes
226 * \param[in] nsvc NS-VC to be string-formatted
227 * \return pointer to buf on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200228char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc)
229{
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200230 const struct osmo_sockaddr *local;
231 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200232 struct osmo_sockaddr_str local_str;
233 struct osmo_sockaddr_str remote_str;
234
235 if (!buf_len)
Harald Welte92ad0292020-09-18 22:34:24 +0200236 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200237
238 switch (nsvc->ll) {
239 case GPRS_NS_LL_UDP:
240 if (!gprs_ns2_is_ip_bind(nsvc->bind)) {
241 buf[0] = '\0';
242 return buf;
243 }
244
245 local = gprs_ns2_ip_bind_sockaddr(nsvc->bind);
Alexander Couzensc4229a42020-10-11 20:58:04 +0200246 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200247 if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas))
248 strcpy(local_str.ip, "invalid");
249 if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas))
250 strcpy(remote_str.ip, "invalid");
251
252 if (nsvc->nsvci_is_valid)
253 snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u",
254 local_str.ip, local_str.port,
255 nsvc->nsvci,
256 remote_str.ip, remote_str.port);
257 else
258 snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u",
259 local_str.ip, local_str.port,
260 remote_str.ip, remote_str.port);
261 break;
262 case GPRS_NS_LL_FR_GRE:
263 snprintf(buf, buf_len, "frgre)");
264 break;
265 case GPRS_NS_LL_E1:
266 snprintf(buf, buf_len, "e1)");
267 break;
268 default:
269 buf[0] = '\0';
270 break;
271 }
272
273 buf[buf_len - 1] = '\0';
274
275 return buf;
276}
277
278/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
279#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
280
Harald Welte5bef2cc2020-09-18 22:33:24 +0200281/*! string-format a given NS-VC to a thread-local static buffer.
282 * \param[in] nsvc NS-VC to be string-formatted
283 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200284const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
285{
286 static __thread char buf[NS2_LL_MAX_STR];
287 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
288}
289
Harald Welte5bef2cc2020-09-18 22:33:24 +0200290/*! string-format a given NS-VC to a dynamically allocated string.
291 * \param[in] ctx talloc context from which to allocate
292 * \param[in] nsvc NS-VC to be string-formatted
293 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200294char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
295{
296 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
297 if (!buf)
298 return buf;
299 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
300}
301
Harald Welte5bef2cc2020-09-18 22:33:24 +0200302/*! Receive a primitive from the NS User (Gb).
303 * \param[in] nsi NS instance to which the primitive is issued
304 * \param[in] oph The primitive
305 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200306int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
307{
308 /* TODO: implement load distribution function */
309 /* TODO: implement resource distribution */
310 /* TODO: check for empty PDUs which can be sent to Request/Confirm
311 * the IP endpoint */
312 struct osmo_gprs_ns2_prim *nsp;
313 struct gprs_ns2_nse *nse = NULL;
314 struct gprs_ns2_vc *nsvc = NULL, *tmp;
315 uint16_t bvci, nsei;
316 uint8_t sducontrol = 0;
317
318 if (oph->sap != SAP_NS)
319 return -EINVAL;
320
321 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
322
323 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != PRIM_NS_UNIT_DATA)
324 return -EINVAL;
325
326 if (!oph->msg)
327 return -EINVAL;
328
329 bvci = nsp->bvci;
330 nsei = nsp->nsei;
331
332 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
333 if (!nse)
334 return -EINVAL;
335
336 llist_for_each_entry(tmp, &nse->nsvc, list) {
337 if (!gprs_ns2_vc_is_unblocked(tmp))
338 continue;
339 if (bvci == 0 && tmp->sig_weight == 0)
340 continue;
341 if (bvci != 0 && tmp->data_weight == 0)
342 continue;
343
344 nsvc = tmp;
345 }
346
347 /* TODO: send a status primitive back */
348 if (!nsvc)
349 return 0;
350
351 if (nsp->u.unitdata.change == NS_ENDPOINT_REQUEST_CHANGE)
352 sducontrol = 1;
353 else if (nsp->u.unitdata.change == NS_ENDPOINT_CONFIRM_CHANGE)
354 sducontrol = 2;
355
356 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
357}
358
Harald Welte5bef2cc2020-09-18 22:33:24 +0200359/*! Send a STATUS.ind primitive to the specified NS instance user.
360 * \param[in] nsi NS instance on which we operate
361 * \param[in] nsei NSEI to which the statue relates
362 * \param[in] bvci BVCI to which the status relates
363 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200364void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
365 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200366 enum gprs_ns2_affecting_cause cause)
367{
368 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200369 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200370 nsp.bvci = bvci;
371 nsp.u.status.cause = cause;
372 nsp.u.status.transfer = -1;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200373 nsp.u.status.first = nse->first;
374 nsp.u.status.persistent = nse->persistent;
Alexander Couzens6a161492020-07-12 13:45:50 +0200375 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS,
376 PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200377 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200378}
379
Harald Welte5bef2cc2020-09-18 22:33:24 +0200380/*! Allocate a NS-VC within the given bind + NSE.
381 * \param[in] bind The 'bind' on which we operate
382 * \param[in] nse The NS Entity on which we operate
383 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
384 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200385struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater)
386{
387 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
388
389 if (!nsvc)
390 return NULL;
391
392 nsvc->bind = bind;
393 nsvc->nse = nse;
394 nsvc->mode = bind->vc_mode;
395 nsvc->sig_weight = 1;
396 nsvc->data_weight = 1;
397
398 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx);
399 if (!nsvc->ctrg) {
400 goto err;
401 }
402 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx);
403 if (!nsvc->statg)
404 goto err_group;
405 if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater))
406 goto err_statg;
407
408 bind->nsi->rate_ctr_idx++;
409
410 llist_add(&nsvc->list, &nse->nsvc);
411 llist_add(&nsvc->blist, &bind->nsvc);
412
413 return nsvc;
414
415err_statg:
416 osmo_stat_item_group_free(nsvc->statg);
417err_group:
418 rate_ctr_group_free(nsvc->ctrg);
419err:
420 talloc_free(nsvc);
421
422 return NULL;
423}
424
Harald Welte5bef2cc2020-09-18 22:33:24 +0200425/*! Destroy/release given NS-VC.
426 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200427void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
428{
429 if (!nsvc)
430 return;
431
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200432 ns2_prim_status_ind(nsvc->nse, 0, NS_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200433
434 llist_del(&nsvc->list);
435 llist_del(&nsvc->blist);
436
437 /* notify nse this nsvc is unavailable */
438 ns2_nse_notify_unblocked(nsvc, false);
439
440 /* check if sns is using this VC */
441 ns2_sns_free_nsvc(nsvc);
442 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
443
444 /* let the driver/bind clean up it's internal state */
445 if (nsvc->priv && nsvc->bind->free_vc)
446 nsvc->bind->free_vc(nsvc);
447
448 osmo_stat_item_group_free(nsvc->statg);
449 rate_ctr_group_free(nsvc->ctrg);
450
451 talloc_free(nsvc);
452}
453
Harald Welte5bef2cc2020-09-18 22:33:24 +0200454/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200455struct msgb *gprs_ns2_msgb_alloc(void)
456{
457 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
458 "GPRS/NS");
459 if (!msg) {
460 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
461 NS_ALLOC_SIZE);
462 }
463 return msg;
464}
465
Harald Welte5bef2cc2020-09-18 22:33:24 +0200466/*! Create a status message to be sent over a new connection.
467 * \param[in] orig_msg the original message
468 * \param[in] tp TLVP parsed of the original message
469 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
470 * \param[in] cause Cause for the rejection
471 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200472static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
473{
474 struct msgb *msg = gprs_ns2_msgb_alloc();
475 struct gprs_ns_hdr *nsh;
476 bool have_vci = false;
477 uint8_t _cause = cause;
478 uint16_t nsei = 0;
479
480 if (!msg)
481 return -ENOMEM;
482
483 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
484 nsei = tlvp_val16be(tp, NS_IE_NSEI);
485
486 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
487 nsei, gprs_ns2_cause_str(cause));
488 }
489
490 msg->l2h = msgb_put(msg, sizeof(*nsh));
491 nsh = (struct gprs_ns_hdr *) msg->l2h;
492 nsh->pdu_type = NS_PDUT_STATUS;
493
494 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
495 have_vci = TLVP_PRESENT(tp, NS_IE_VCI);
496
497 /* Section 9.2.7.1: Static conditions for NS-VCI */
498 if (cause == NS_CAUSE_NSVC_BLOCKED ||
499 cause == NS_CAUSE_NSVC_UNKNOWN) {
500 if (!have_vci) {
501 msgb_free(msg);
502 return -EINVAL;
503 }
504
505 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
506 }
507
508 /* Section 9.2.7.2: Static conditions for NS PDU */
509 switch (cause) {
510 case NS_CAUSE_SEM_INCORR_PDU:
511 case NS_CAUSE_PDU_INCOMP_PSTATE:
512 case NS_CAUSE_PROTO_ERR_UNSPEC:
513 case NS_CAUSE_INVAL_ESSENT_IE:
514 case NS_CAUSE_MISSING_ESSENT_IE:
515 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
516 orig_msg->l2h);
517 break;
518 default:
519 break;
520 }
521
522 *reject = msg;
523 return 0;
524}
525
Harald Welte5bef2cc2020-09-18 22:33:24 +0200526/*! Resolve a NS Entity based on its NSEI.
527 * \param[in] nsi NS Instance in which we do the look-up
528 * \param[in] nsei NSEI to look up
529 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200530struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
531{
532 struct gprs_ns2_nse *nse;
533
534 llist_for_each_entry(nse, &nsi->nse, list) {
535 if (nse->nsei == nsei)
536 return nse;
537 }
538
539 return NULL;
540}
541
Harald Welte5bef2cc2020-09-18 22:33:24 +0200542/*! Resolve a NS-VC Entity based on its NS-VCI.
543 * \param[in] nsi NS Instance in which we do the look-up
544 * \param[in] nsvci NS-VCI to look up
545 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200546struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
547{
548 struct gprs_ns2_nse *nse;
549 struct gprs_ns2_vc *nsvc;
550
551 llist_for_each_entry(nse, &nsi->nse, list) {
552 llist_for_each_entry(nsvc, &nse->nsvc, list) {
553 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
554 return nsvc;
555 }
556 }
557
558 return NULL;
559}
560
Harald Welte5bef2cc2020-09-18 22:33:24 +0200561/*! Create a NS Entity within given NS instance.
562 * \param[in] nsi NS instance in which to create NS Entity
563 * \param[in] nsei NS Entity Identifier of to-be-created NSE
564 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200565struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei)
566{
567 struct gprs_ns2_nse *nse;
568
569 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
570 if (nse) {
571 LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei);
572 return nse;
573 }
574
575 nse = talloc_zero(nsi, struct gprs_ns2_nse);
576 if (!nse)
577 return NULL;
578
579 nse->nsei = nsei;
580 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200581 nse->first = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200582 llist_add(&nse->list, &nsi->nse);
583 INIT_LLIST_HEAD(&nse->nsvc);
584
585 return nse;
586}
587
Alexander Couzens05e7f7d2020-10-11 19:51:46 +0200588/*! Return the NSEI
589 * \param[in] nse NS Entity
590 * \return the nsei.
591 */
592uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse)
593{
594 return nse->nsei;
595}
596
Harald Welte5bef2cc2020-09-18 22:33:24 +0200597/*! Destroy given NS Entity.
598 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200599void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
600{
601 struct gprs_ns2_vc *nsvc, *tmp;
602
603 if (!nse)
604 return;
605
606 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
607 gprs_ns2_free_nsvc(nsvc);
608 }
609
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200610 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200611
612 llist_del(&nse->list);
613 if (nse->bss_sns_fi)
614 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
615 talloc_free(nse);
616}
617
Alexander Couzens4b6c8af2020-10-11 20:15:25 +0200618void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi)
619{
620 struct gprs_ns2_nse *nse, *ntmp;
621
622 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
623 gprs_ns2_free_nse(nse);
624 }
625}
626
Alexander Couzens6a161492020-07-12 13:45:50 +0200627static inline int ns2_tlv_parse(struct tlv_parsed *dec,
628 const uint8_t *buf, int buf_len, uint8_t lv_tag,
629 uint8_t lv_tag2)
630{
631 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
632 * See comment of ns_att_tlvdef1. */
633 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
634 if (rc < 0)
635 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
636 return rc;
637}
638
639
Harald Welte5bef2cc2020-09-18 22:33:24 +0200640/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
641 * \param[in] bind the bind through which msg was received
642 * \param[in] msg the actual received message
643 * \param[in] logname A name to describe the VC. E.g. ip address pair
644 * \param[out] reject A message filled to be sent back. Only used in failure cases.
645 * \param[out] success A pointer which will be set to the new VC on success
646 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzens6a161492020-07-12 13:45:50 +0200647enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
648 struct msgb *msg,
649 const char *logname,
650 struct msgb **reject,
651 struct gprs_ns2_vc **success)
652{
653 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
654 struct tlv_parsed tp;
655 struct gprs_ns2_vc *nsvc;
656 struct gprs_ns2_nse *nse;
657 uint16_t nsvci;
658 uint16_t nsei;
659
660 int rc;
661
662 if (msg->len < sizeof(struct gprs_ns_hdr))
663 return GPRS_NS2_CS_ERROR;
664
665 if (nsh->pdu_type == NS_PDUT_STATUS) {
666 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
667 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
668 "for non-existing NS-VC\n",
669 logname);
670 return GPRS_NS2_CS_SKIPPED;
671 }
672
673 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
674 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
675 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
676 "for non-existing NS-VC\n",
677 logname);
678 return GPRS_NS2_CS_SKIPPED;
679 }
680
681 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
682 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
683 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
684 "for non-existing NS-VC\n",
685 logname);
686 return GPRS_NS2_CS_SKIPPED;
687 }
688
Alexander Couzens48f63862020-09-12 02:19:19 +0200689 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
690 /* Only the RESET procedure creates a new NSVC */
691 if (nsh->pdu_type != NS_PDUT_RESET) {
692 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200693
Alexander Couzens48f63862020-09-12 02:19:19 +0200694 if (rc < 0) {
695 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
696 return rc;
697 }
698 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200699 }
Alexander Couzens48f63862020-09-12 02:19:19 +0200700 } else { /* NS2_VC_MODE_ALIVE */
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200701 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens48f63862020-09-12 02:19:19 +0200702
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200703 if (rc < 0) {
704 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
705 return rc;
Alexander Couzens48f63862020-09-12 02:19:19 +0200706 }
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200707 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200708 }
709
710 rc = ns2_tlv_parse(&tp, nsh->data,
711 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
712 if (rc < 0) {
713 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
714 "TLV Parse\n", rc);
715 /* TODO: send invalid message back */
716 return GPRS_NS2_CS_REJECTED;
717 }
718
Alexander Couzens8ebc1ac2020-10-12 03:57:26 +0200719 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
720 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
721 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
722 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
723 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200724 }
725
726 /* find or create NSE */
727 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
728 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
729 if (!nse) {
730 if (!bind->nsi->create_nse) {
731 return GPRS_NS2_CS_SKIPPED;
732 }
733
734 nse = gprs_ns2_create_nse(bind->nsi, nsei);
735 if (!nse) {
736 return GPRS_NS2_CS_ERROR;
737 }
738 }
739
740 nsvc = ns2_vc_alloc(bind, nse, false);
741 if (!nsvc)
742 return GPRS_NS2_CS_SKIPPED;
743
744 nsvc->ll = GPRS_NS_LL_UDP;
745
746 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
747 nsvc->nsvci = nsvci;
748 nsvc->nsvci_is_valid = true;
749
750 *success = nsvc;
751
752 return GPRS_NS2_CS_CREATED;
753}
754
Harald Welte5bef2cc2020-09-18 22:33:24 +0200755/*! Create, and connect an inactive, new IP-based NS-VC
756 * \param[in] bind bind in which the new NS-VC is to be created
757 * \param[in] remote remote address to which to connect
758 * \param[in] nse NS Entity in which the NS-VC is to be created
759 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
760 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200761struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700762 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200763 struct gprs_ns2_nse *nse,
764 uint16_t nsvci)
765{
766 struct gprs_ns2_vc *nsvc;
767
768 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
769 if (!nsvc)
770 return NULL;
771
772 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
773 nsvc->nsvci = nsvci;
774 nsvc->nsvci_is_valid = true;
775 }
776
777 return nsvc;
778}
779
Harald Welte5bef2cc2020-09-18 22:33:24 +0200780/*! Create, connect and activate a new IP-based NS-VC
781 * \param[in] bind bind in which the new NS-VC is to be created
782 * \param[in] remote remote address to which to connect
783 * \param[in] nse NS Entity in which the NS-VC is to be created
784 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
785 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200786struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700787 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200788 struct gprs_ns2_nse *nse,
789 uint16_t nsvci)
790{
791 struct gprs_ns2_vc *nsvc;
792 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
793 if (!nsvc)
794 return NULL;
795
796 gprs_ns2_vc_fsm_start(nsvc);
797
798 return nsvc;
799}
800
Harald Welte5bef2cc2020-09-18 22:33:24 +0200801/*! Create, connect and activate a new IP-based NS-VC
802 * \param[in] bind bind in which the new NS-VC is to be created
803 * \param[in] remote remote address to which to connect
804 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
805 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
806 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200807struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700808 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200809 uint16_t nsei,
810 uint16_t nsvci)
811{
812 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
813
814 if (!nse) {
815 nse = gprs_ns2_create_nse(bind->nsi, nsei);
816 if (!nse)
817 return NULL;
818 }
819
820 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
821}
822
Harald Welte5bef2cc2020-09-18 22:33:24 +0200823/*! Create, connect and activate a new IP-SNS NSE.
824 * \param[in] bind bind in which the new NS-VC is to be created
825 * \param[in] remote remote address to which to connect
826 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
827 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200828int gprs_ns2_ip_connect_sns(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700829 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200830 uint16_t nsei)
831{
832 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
833 struct gprs_ns2_vc *nsvc;
834
835 if (!nse) {
836 nse = gprs_ns2_create_nse(bind->nsi, nsei);
837 if (!nse)
838 return -1;
839 }
840
841 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
842 if (!nsvc)
843 return -1;
844
845 if (!nse->bss_sns_fi)
846 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, NULL);
847
848 if (!nse->bss_sns_fi)
849 return -1;
850
851 return ns2_sns_bss_fsm_start(nse, nsvc, remote);
852}
853
Harald Welte5bef2cc2020-09-18 22:33:24 +0200854/*! Find NS-VC for given socket address.
855 * \param[in] nse NS Entity in which to search
856 * \param[in] sockaddr socket address to search for
857 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +0200858struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700859 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +0200860{
861 struct gprs_ns2_vc *nsvc;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200862 const struct osmo_sockaddr *remote;
Alexander Couzens6a161492020-07-12 13:45:50 +0200863
864 OSMO_ASSERT(nse);
865 OSMO_ASSERT(sockaddr);
866
867 llist_for_each_entry(nsvc, &nse->nsvc, list) {
Alexander Couzensc4229a42020-10-11 20:58:04 +0200868 remote = gprs_ns2_ip_vc_remote(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200869 if (!osmo_sockaddr_cmp(sockaddr, remote))
870 return nsvc;
871 }
872
873 return NULL;
874}
875
Alexander Couzens6cb5d5f2020-10-11 23:23:31 +0200876/*!
877 * Iterate over all nsvc of a NS Entity and call the callback.
878 * If the callback returns < 0 it aborts the loop and returns the callback return code.
879 * \param[in] nse NS Entity to iterate over all nsvcs
880 * \param[in] cb the callback to call
881 * \param[inout] cb_data the private data of the callback
882 * \return 0 if the loop completes. If a callback returns < 0 it will returns this value.
883 */
884int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data)
885{
886 struct gprs_ns2_vc *nsvc, *tmp;
887 int rc = 0;
888 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
889 rc = cb(nsvc, cb_data);
890 if (rc < 0)
891 return rc;
892 }
893
894 return 0;
895}
896
897
Alexander Couzens6a161492020-07-12 13:45:50 +0200898
Harald Welte5bef2cc2020-09-18 22:33:24 +0200899/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +0200900 * \param[in] nsvc NS-VC for which the message was received
901 * \param msg the received message. Ownership is trasnferred, caller must not free it!
902 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +0200903int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200904 struct msgb *msg)
905{
906 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
907 struct tlv_parsed tp;
908 int rc = 0;
909
910 if (msg->len < sizeof(struct gprs_ns_hdr))
911 return -EINVAL;
912
913 switch (nsh->pdu_type) {
914 case SNS_PDUT_CONFIG:
915 /* one additional byte ('end flag') before the TLV part starts */
916 rc = ns2_tlv_parse(&tp, nsh->data+1,
917 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
918 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200919 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200920 return rc;
921 }
922 /* All sub-network service related message types */
923 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
924 break;
925 case SNS_PDUT_ACK:
926 case SNS_PDUT_ADD:
927 case SNS_PDUT_CHANGE_WEIGHT:
928 case SNS_PDUT_DELETE:
929 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +0200930 rc = ns2_tlv_parse(&tp, nsh->data+5,
931 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200932 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 tp.lv[NS_IE_NSEI].val = nsh->data+2;
937 tp.lv[NS_IE_NSEI].len = 2;
938 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
939 tp.lv[NS_IE_TRANS_ID].len = 1;
940 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
941 break;
942 case SNS_PDUT_CONFIG_ACK:
943 case SNS_PDUT_SIZE:
944 case SNS_PDUT_SIZE_ACK:
945 rc = ns2_tlv_parse(&tp, nsh->data,
946 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
947 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200948 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200949 return rc;
950 }
951 /* All sub-network service related message types */
952 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
953 break;
954
955 case NS_PDUT_UNITDATA:
956 rc = gprs_ns2_vc_rx(nsvc, msg, NULL);
957 break;
958 default:
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\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200963 if (nsh->pdu_type != NS_PDUT_STATUS)
964 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
965 return rc;
966 }
967 rc = gprs_ns2_vc_rx(nsvc, msg, &tp);
968 break;
969 }
970
971 return rc;
972}
973
Harald Welte5bef2cc2020-09-18 22:33:24 +0200974/*! Notify a nse about the change of a NS-VC.
975 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
976 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +0200977void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
978{
979 struct gprs_ns2_nse *nse = nsvc->nse;
980 struct gprs_ns2_vc *tmp;
981
982 if (unblocked == nse->alive)
983 return;
984
985 if (unblocked) {
986 /* this is the first unblocked NSVC on an unavailable NSE */
987 nse->alive = true;
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200988 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +0200989 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200990 return;
991 }
992
993 /* check if there are any remaining alive vcs */
994 llist_for_each_entry(tmp, &nse->nsvc, list) {
995 if (tmp == nsvc)
996 continue;
997
998 if (gprs_ns2_vc_is_unblocked(tmp)) {
999 /* there is at least one remaining alive NSVC */
1000 return;
1001 }
1002 }
1003
1004 /* nse became unavailable */
1005 nse->alive = false;
Alexander Couzensbf95f0f2020-10-01 22:56:03 +02001006 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +02001007}
1008
1009/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +02001010 * \param[in] ctx a talloc context to allocate NS instance from
1011 * \param[in] cb Call-back function for dispatching primitives to the user
1012 * \param[in] cb_data transparent user data passed to Call-back
1013 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +02001014struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
1015{
1016 struct gprs_ns2_inst *nsi;
1017
1018 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
1019 if (!nsi)
1020 return NULL;
1021
1022 nsi->cb = cb;
1023 nsi->cb_data = cb_data;
1024 INIT_LLIST_HEAD(&nsi->binding);
1025 INIT_LLIST_HEAD(&nsi->nse);
1026
1027 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
1028 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
1029 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
1030 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
1031 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
1032 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
1033 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
1034 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
1035
1036 return nsi;
1037}
1038
Harald Welte5bef2cc2020-09-18 22:33:24 +02001039/*! Destroy a NS Instance (including all its NSEs, binds, ...).
1040 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001041void gprs_ns2_free(struct gprs_ns2_inst *nsi)
1042{
Alexander Couzens6a161492020-07-12 13:45:50 +02001043 if (!nsi)
1044 return;
1045
Alexander Couzens4b6c8af2020-10-11 20:15:25 +02001046 gprs_ns2_free_nses(nsi);
Alexander Couzens896fcd52020-10-11 19:52:36 +02001047 gprs_ns2_free_binds(nsi);
Alexander Couzens35315042020-10-10 03:28:17 +02001048
1049 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001050}
1051
Harald Welte5bef2cc2020-09-18 22:33:24 +02001052/*! Configure whether a NS Instance should dynamically create NSEs based on incoming traffic.
1053 * \param nsi the instance to modify
1054 * \param create_nse if NSE can be created on receiving package. SGSN set this.
1055 * \return 0 on success; negative on error
Alexander Couzens6a161492020-07-12 13:45:50 +02001056 */
1057int gprs_ns2_dynamic_create_nse(struct gprs_ns2_inst *nsi, bool create_nse)
1058{
1059 nsi->create_nse = create_nse;
1060
1061 return 0;
1062}
1063
Harald Welte5bef2cc2020-09-18 22:33:24 +02001064/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1065 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001066void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1067{
1068 struct gprs_ns2_vc *nsvc;
1069 OSMO_ASSERT(nse);
1070
1071 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1072 if (nsvc->sns_only)
1073 continue;
1074
1075 gprs_ns2_vc_fsm_start(nsvc);
1076 }
1077}
1078
Harald Welte5bef2cc2020-09-18 22:33:24 +02001079/*! Set the mode of given bind.
1080 * \param[in] bind the bind we want to set the mode of
1081 * \param[in] modde mode to set bind to */
Alexander Couzens6a161492020-07-12 13:45:50 +02001082void gprs_ns2_bind_set_mode(struct gprs_ns2_vc_bind *bind, enum gprs_ns2_vc_mode mode)
1083{
1084 bind->vc_mode = mode;
1085}
1086
Harald Welte5bef2cc2020-09-18 22:33:24 +02001087/*! Destroy a given bind.
1088 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001089void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1090{
1091 struct gprs_ns2_vc *nsvc, *tmp;
1092 if (!bind)
1093 return;
1094
1095 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1096 gprs_ns2_free_nsvc(nsvc);
1097 }
1098
1099 if (bind->driver->free_bind)
1100 bind->driver->free_bind(bind);
1101
1102 llist_del(&bind->list);
1103 talloc_free(bind);
1104}
Alexander Couzens896fcd52020-10-11 19:52:36 +02001105
1106void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi)
1107{
1108 struct gprs_ns2_vc_bind *bind, *tbind;
1109
1110 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1111 gprs_ns2_free_bind(bind);
1112 }
1113}
Alexander Couzens6a161492020-07-12 13:45:50 +02001114/*! @} */