blob: 334989c442d3fe78901390774c2f7773f1dff3e5 [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
Harald Welte5bef2cc2020-09-18 22:33:24 +0200206/*! string-format a given NS-VC into a user-supplied buffer.
207 * \param[in] buf user-allocated output buffer
208 * \param[in] buf_len size of user-allocated output buffer in bytes
209 * \param[in] nsvc NS-VC to be string-formatted
210 * \return pointer to buf on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200211char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc)
212{
213 struct osmo_sockaddr *local;
214 struct osmo_sockaddr *remote;
215 struct osmo_sockaddr_str local_str;
216 struct osmo_sockaddr_str remote_str;
217
218 if (!buf_len)
Harald Welte92ad0292020-09-18 22:34:24 +0200219 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200220
221 switch (nsvc->ll) {
222 case GPRS_NS_LL_UDP:
223 if (!gprs_ns2_is_ip_bind(nsvc->bind)) {
224 buf[0] = '\0';
225 return buf;
226 }
227
228 local = gprs_ns2_ip_bind_sockaddr(nsvc->bind);
229 remote = gprs_ns2_ip_vc_sockaddr(nsvc);
230 if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas))
231 strcpy(local_str.ip, "invalid");
232 if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas))
233 strcpy(remote_str.ip, "invalid");
234
235 if (nsvc->nsvci_is_valid)
236 snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u",
237 local_str.ip, local_str.port,
238 nsvc->nsvci,
239 remote_str.ip, remote_str.port);
240 else
241 snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u",
242 local_str.ip, local_str.port,
243 remote_str.ip, remote_str.port);
244 break;
245 case GPRS_NS_LL_FR_GRE:
246 snprintf(buf, buf_len, "frgre)");
247 break;
248 case GPRS_NS_LL_E1:
249 snprintf(buf, buf_len, "e1)");
250 break;
251 default:
252 buf[0] = '\0';
253 break;
254 }
255
256 buf[buf_len - 1] = '\0';
257
258 return buf;
259}
260
261/* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */
262#define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8
263
Harald Welte5bef2cc2020-09-18 22:33:24 +0200264/*! string-format a given NS-VC to a thread-local static buffer.
265 * \param[in] nsvc NS-VC to be string-formatted
266 * \return pointer to the string on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200267const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc)
268{
269 static __thread char buf[NS2_LL_MAX_STR];
270 return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc);
271}
272
Harald Welte5bef2cc2020-09-18 22:33:24 +0200273/*! string-format a given NS-VC to a dynamically allocated string.
274 * \param[in] ctx talloc context from which to allocate
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 +0200277char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc)
278{
279 char *buf = talloc_size(ctx, NS2_LL_MAX_STR);
280 if (!buf)
281 return buf;
282 return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc);
283}
284
Harald Welte5bef2cc2020-09-18 22:33:24 +0200285/*! Receive a primitive from the NS User (Gb).
286 * \param[in] nsi NS instance to which the primitive is issued
287 * \param[in] oph The primitive
288 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200289int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph)
290{
291 /* TODO: implement load distribution function */
292 /* TODO: implement resource distribution */
293 /* TODO: check for empty PDUs which can be sent to Request/Confirm
294 * the IP endpoint */
295 struct osmo_gprs_ns2_prim *nsp;
296 struct gprs_ns2_nse *nse = NULL;
297 struct gprs_ns2_vc *nsvc = NULL, *tmp;
298 uint16_t bvci, nsei;
299 uint8_t sducontrol = 0;
300
301 if (oph->sap != SAP_NS)
302 return -EINVAL;
303
304 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
305
306 if (oph->operation != PRIM_OP_REQUEST || oph->primitive != PRIM_NS_UNIT_DATA)
307 return -EINVAL;
308
309 if (!oph->msg)
310 return -EINVAL;
311
312 bvci = nsp->bvci;
313 nsei = nsp->nsei;
314
315 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
316 if (!nse)
317 return -EINVAL;
318
319 llist_for_each_entry(tmp, &nse->nsvc, list) {
320 if (!gprs_ns2_vc_is_unblocked(tmp))
321 continue;
322 if (bvci == 0 && tmp->sig_weight == 0)
323 continue;
324 if (bvci != 0 && tmp->data_weight == 0)
325 continue;
326
327 nsvc = tmp;
328 }
329
330 /* TODO: send a status primitive back */
331 if (!nsvc)
332 return 0;
333
334 if (nsp->u.unitdata.change == NS_ENDPOINT_REQUEST_CHANGE)
335 sducontrol = 1;
336 else if (nsp->u.unitdata.change == NS_ENDPOINT_CONFIRM_CHANGE)
337 sducontrol = 2;
338
339 return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg);
340}
341
Harald Welte5bef2cc2020-09-18 22:33:24 +0200342/*! Send a STATUS.ind primitive to the specified NS instance user.
343 * \param[in] nsi NS instance on which we operate
344 * \param[in] nsei NSEI to which the statue relates
345 * \param[in] bvci BVCI to which the status relates
346 * \param[in] cause The cause of the status */
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200347void ns2_prim_status_ind(struct gprs_ns2_nse *nse,
348 uint16_t bvci,
Alexander Couzens6a161492020-07-12 13:45:50 +0200349 enum gprs_ns2_affecting_cause cause)
350{
351 struct osmo_gprs_ns2_prim nsp = {};
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200352 nsp.nsei = nse->nsei;
Alexander Couzens6a161492020-07-12 13:45:50 +0200353 nsp.bvci = bvci;
354 nsp.u.status.cause = cause;
355 nsp.u.status.transfer = -1;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200356 nsp.u.status.first = nse->first;
357 nsp.u.status.persistent = nse->persistent;
Alexander Couzens6a161492020-07-12 13:45:50 +0200358 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS,
359 PRIM_OP_INDICATION, NULL);
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200360 nse->nsi->cb(&nsp.oph, nse->nsi->cb_data);
Alexander Couzens6a161492020-07-12 13:45:50 +0200361}
362
Harald Welte5bef2cc2020-09-18 22:33:24 +0200363/*! Allocate a NS-VC within the given bind + NSE.
364 * \param[in] bind The 'bind' on which we operate
365 * \param[in] nse The NS Entity on which we operate
366 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
367 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200368struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater)
369{
370 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
371
372 if (!nsvc)
373 return NULL;
374
375 nsvc->bind = bind;
376 nsvc->nse = nse;
377 nsvc->mode = bind->vc_mode;
378 nsvc->sig_weight = 1;
379 nsvc->data_weight = 1;
380
381 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx);
382 if (!nsvc->ctrg) {
383 goto err;
384 }
385 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx);
386 if (!nsvc->statg)
387 goto err_group;
388 if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater))
389 goto err_statg;
390
391 bind->nsi->rate_ctr_idx++;
392
393 llist_add(&nsvc->list, &nse->nsvc);
394 llist_add(&nsvc->blist, &bind->nsvc);
395
396 return nsvc;
397
398err_statg:
399 osmo_stat_item_group_free(nsvc->statg);
400err_group:
401 rate_ctr_group_free(nsvc->ctrg);
402err:
403 talloc_free(nsvc);
404
405 return NULL;
406}
407
Harald Welte5bef2cc2020-09-18 22:33:24 +0200408/*! Destroy/release given NS-VC.
409 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200410void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
411{
412 if (!nsvc)
413 return;
414
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200415 ns2_prim_status_ind(nsvc->nse, 0, NS_AFF_CAUSE_VC_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200416
417 llist_del(&nsvc->list);
418 llist_del(&nsvc->blist);
419
420 /* notify nse this nsvc is unavailable */
421 ns2_nse_notify_unblocked(nsvc, false);
422
423 /* check if sns is using this VC */
424 ns2_sns_free_nsvc(nsvc);
425 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
426
427 /* let the driver/bind clean up it's internal state */
428 if (nsvc->priv && nsvc->bind->free_vc)
429 nsvc->bind->free_vc(nsvc);
430
431 osmo_stat_item_group_free(nsvc->statg);
432 rate_ctr_group_free(nsvc->ctrg);
433
434 talloc_free(nsvc);
435}
436
Harald Welte5bef2cc2020-09-18 22:33:24 +0200437/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200438struct msgb *gprs_ns2_msgb_alloc(void)
439{
440 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
441 "GPRS/NS");
442 if (!msg) {
443 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
444 NS_ALLOC_SIZE);
445 }
446 return msg;
447}
448
Harald Welte5bef2cc2020-09-18 22:33:24 +0200449/*! Create a status message to be sent over a new connection.
450 * \param[in] orig_msg the original message
451 * \param[in] tp TLVP parsed of the original message
452 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
453 * \param[in] cause Cause for the rejection
454 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200455static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
456{
457 struct msgb *msg = gprs_ns2_msgb_alloc();
458 struct gprs_ns_hdr *nsh;
459 bool have_vci = false;
460 uint8_t _cause = cause;
461 uint16_t nsei = 0;
462
463 if (!msg)
464 return -ENOMEM;
465
466 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
467 nsei = tlvp_val16be(tp, NS_IE_NSEI);
468
469 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
470 nsei, gprs_ns2_cause_str(cause));
471 }
472
473 msg->l2h = msgb_put(msg, sizeof(*nsh));
474 nsh = (struct gprs_ns_hdr *) msg->l2h;
475 nsh->pdu_type = NS_PDUT_STATUS;
476
477 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
478 have_vci = TLVP_PRESENT(tp, NS_IE_VCI);
479
480 /* Section 9.2.7.1: Static conditions for NS-VCI */
481 if (cause == NS_CAUSE_NSVC_BLOCKED ||
482 cause == NS_CAUSE_NSVC_UNKNOWN) {
483 if (!have_vci) {
484 msgb_free(msg);
485 return -EINVAL;
486 }
487
488 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
489 }
490
491 /* Section 9.2.7.2: Static conditions for NS PDU */
492 switch (cause) {
493 case NS_CAUSE_SEM_INCORR_PDU:
494 case NS_CAUSE_PDU_INCOMP_PSTATE:
495 case NS_CAUSE_PROTO_ERR_UNSPEC:
496 case NS_CAUSE_INVAL_ESSENT_IE:
497 case NS_CAUSE_MISSING_ESSENT_IE:
498 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
499 orig_msg->l2h);
500 break;
501 default:
502 break;
503 }
504
505 *reject = msg;
506 return 0;
507}
508
Harald Welte5bef2cc2020-09-18 22:33:24 +0200509/*! Resolve a NS Entity based on its NSEI.
510 * \param[in] nsi NS Instance in which we do the look-up
511 * \param[in] nsei NSEI to look up
512 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200513struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
514{
515 struct gprs_ns2_nse *nse;
516
517 llist_for_each_entry(nse, &nsi->nse, list) {
518 if (nse->nsei == nsei)
519 return nse;
520 }
521
522 return NULL;
523}
524
Harald Welte5bef2cc2020-09-18 22:33:24 +0200525/*! Resolve a NS-VC Entity based on its NS-VCI.
526 * \param[in] nsi NS Instance in which we do the look-up
527 * \param[in] nsvci NS-VCI to look up
528 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200529struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
530{
531 struct gprs_ns2_nse *nse;
532 struct gprs_ns2_vc *nsvc;
533
534 llist_for_each_entry(nse, &nsi->nse, list) {
535 llist_for_each_entry(nsvc, &nse->nsvc, list) {
536 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
537 return nsvc;
538 }
539 }
540
541 return NULL;
542}
543
Harald Welte5bef2cc2020-09-18 22:33:24 +0200544/*! Create a NS Entity within given NS instance.
545 * \param[in] nsi NS instance in which to create NS Entity
546 * \param[in] nsei NS Entity Identifier of to-be-created NSE
547 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200548struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei)
549{
550 struct gprs_ns2_nse *nse;
551
552 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
553 if (nse) {
554 LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei);
555 return nse;
556 }
557
558 nse = talloc_zero(nsi, struct gprs_ns2_nse);
559 if (!nse)
560 return NULL;
561
562 nse->nsei = nsei;
563 nse->nsi = nsi;
Alexander Couzensda0a2852020-10-01 23:24:07 +0200564 nse->first = true;
Alexander Couzens6a161492020-07-12 13:45:50 +0200565 llist_add(&nse->list, &nsi->nse);
566 INIT_LLIST_HEAD(&nse->nsvc);
567
568 return nse;
569}
570
Harald Welte5bef2cc2020-09-18 22:33:24 +0200571/*! Destroy given NS Entity.
572 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200573void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
574{
575 struct gprs_ns2_vc *nsvc, *tmp;
576
577 if (!nse)
578 return;
579
580 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
581 gprs_ns2_free_nsvc(nsvc);
582 }
583
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200584 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200585
586 llist_del(&nse->list);
587 if (nse->bss_sns_fi)
588 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
589 talloc_free(nse);
590}
591
592static inline int ns2_tlv_parse(struct tlv_parsed *dec,
593 const uint8_t *buf, int buf_len, uint8_t lv_tag,
594 uint8_t lv_tag2)
595{
596 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
597 * See comment of ns_att_tlvdef1. */
598 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
599 if (rc < 0)
600 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
601 return rc;
602}
603
604
Harald Welte5bef2cc2020-09-18 22:33:24 +0200605/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
606 * \param[in] bind the bind through which msg was received
607 * \param[in] msg the actual received message
608 * \param[in] logname A name to describe the VC. E.g. ip address pair
609 * \param[out] reject A message filled to be sent back. Only used in failure cases.
610 * \param[out] success A pointer which will be set to the new VC on success
611 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzens6a161492020-07-12 13:45:50 +0200612enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
613 struct msgb *msg,
614 const char *logname,
615 struct msgb **reject,
616 struct gprs_ns2_vc **success)
617{
618 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
619 struct tlv_parsed tp;
620 struct gprs_ns2_vc *nsvc;
621 struct gprs_ns2_nse *nse;
622 uint16_t nsvci;
623 uint16_t nsei;
624
625 int rc;
626
627 if (msg->len < sizeof(struct gprs_ns_hdr))
628 return GPRS_NS2_CS_ERROR;
629
630 if (nsh->pdu_type == NS_PDUT_STATUS) {
631 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
632 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
633 "for non-existing NS-VC\n",
634 logname);
635 return GPRS_NS2_CS_SKIPPED;
636 }
637
638 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
639 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
640 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
641 "for non-existing NS-VC\n",
642 logname);
643 return GPRS_NS2_CS_SKIPPED;
644 }
645
646 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
647 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
648 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
649 "for non-existing NS-VC\n",
650 logname);
651 return GPRS_NS2_CS_SKIPPED;
652 }
653
Alexander Couzens48f63862020-09-12 02:19:19 +0200654 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
655 /* Only the RESET procedure creates a new NSVC */
656 if (nsh->pdu_type != NS_PDUT_RESET) {
657 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200658
Alexander Couzens48f63862020-09-12 02:19:19 +0200659 if (rc < 0) {
660 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
661 return rc;
662 }
663 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200664 }
Alexander Couzens48f63862020-09-12 02:19:19 +0200665 } else { /* NS2_VC_MODE_ALIVE */
666 /* Only the ALIVE procedure creates a new NSVC */
667 if (nsh->pdu_type != NS_PDUT_ALIVE) {
668 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
669
670 if (rc < 0) {
671 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
672 return rc;
673 }
674 return GPRS_NS2_CS_REJECTED;
675 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200676 }
677
678 rc = ns2_tlv_parse(&tp, nsh->data,
679 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
680 if (rc < 0) {
681 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
682 "TLV Parse\n", rc);
683 /* TODO: send invalid message back */
684 return GPRS_NS2_CS_REJECTED;
685 }
686
Alexander Couzens48f63862020-09-12 02:19:19 +0200687 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
688 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
689 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
690 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
691 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
692 return GPRS_NS2_CS_REJECTED;
693 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200694 }
695
696 /* find or create NSE */
697 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
698 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
699 if (!nse) {
700 if (!bind->nsi->create_nse) {
701 return GPRS_NS2_CS_SKIPPED;
702 }
703
704 nse = gprs_ns2_create_nse(bind->nsi, nsei);
705 if (!nse) {
706 return GPRS_NS2_CS_ERROR;
707 }
708 }
709
710 nsvc = ns2_vc_alloc(bind, nse, false);
711 if (!nsvc)
712 return GPRS_NS2_CS_SKIPPED;
713
714 nsvc->ll = GPRS_NS_LL_UDP;
715
716 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
717 nsvc->nsvci = nsvci;
718 nsvc->nsvci_is_valid = true;
719
720 *success = nsvc;
721
722 return GPRS_NS2_CS_CREATED;
723}
724
Harald Welte5bef2cc2020-09-18 22:33:24 +0200725/*! Create, and connect an inactive, new IP-based NS-VC
726 * \param[in] bind bind in which the new NS-VC is to be created
727 * \param[in] remote remote address to which to connect
728 * \param[in] nse NS Entity in which the NS-VC is to be created
729 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
730 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200731struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700732 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200733 struct gprs_ns2_nse *nse,
734 uint16_t nsvci)
735{
736 struct gprs_ns2_vc *nsvc;
737
738 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
739 if (!nsvc)
740 return NULL;
741
742 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
743 nsvc->nsvci = nsvci;
744 nsvc->nsvci_is_valid = true;
745 }
746
747 return nsvc;
748}
749
Harald Welte5bef2cc2020-09-18 22:33:24 +0200750/*! Create, connect and activate a new IP-based NS-VC
751 * \param[in] bind bind in which the new NS-VC is to be created
752 * \param[in] remote remote address to which to connect
753 * \param[in] nse NS Entity in which the NS-VC is to be created
754 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
755 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200756struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700757 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200758 struct gprs_ns2_nse *nse,
759 uint16_t nsvci)
760{
761 struct gprs_ns2_vc *nsvc;
762 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
763 if (!nsvc)
764 return NULL;
765
766 gprs_ns2_vc_fsm_start(nsvc);
767
768 return nsvc;
769}
770
Harald Welte5bef2cc2020-09-18 22:33:24 +0200771/*! Create, connect and activate a new IP-based NS-VC
772 * \param[in] bind bind in which the new NS-VC is to be created
773 * \param[in] remote remote address to which to connect
774 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
775 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
776 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200777struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700778 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200779 uint16_t nsei,
780 uint16_t nsvci)
781{
782 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
783
784 if (!nse) {
785 nse = gprs_ns2_create_nse(bind->nsi, nsei);
786 if (!nse)
787 return NULL;
788 }
789
790 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
791}
792
Harald Welte5bef2cc2020-09-18 22:33:24 +0200793/*! Create, connect and activate a new IP-SNS NSE.
794 * \param[in] bind bind in which the new NS-VC is to be created
795 * \param[in] remote remote address to which to connect
796 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
797 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200798int gprs_ns2_ip_connect_sns(struct gprs_ns2_vc_bind *bind,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700799 const struct osmo_sockaddr *remote,
Alexander Couzens6a161492020-07-12 13:45:50 +0200800 uint16_t nsei)
801{
802 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
803 struct gprs_ns2_vc *nsvc;
804
805 if (!nse) {
806 nse = gprs_ns2_create_nse(bind->nsi, nsei);
807 if (!nse)
808 return -1;
809 }
810
811 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
812 if (!nsvc)
813 return -1;
814
815 if (!nse->bss_sns_fi)
816 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, NULL);
817
818 if (!nse->bss_sns_fi)
819 return -1;
820
821 return ns2_sns_bss_fsm_start(nse, nsvc, remote);
822}
823
Harald Welte5bef2cc2020-09-18 22:33:24 +0200824/*! Find NS-VC for given socket address.
825 * \param[in] nse NS Entity in which to search
826 * \param[in] sockaddr socket address to search for
827 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens38b19e82020-09-23 23:56:37 +0200828struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700829 const struct osmo_sockaddr *sockaddr)
Alexander Couzens6a161492020-07-12 13:45:50 +0200830{
831 struct gprs_ns2_vc *nsvc;
832 struct osmo_sockaddr *remote;
833
834 OSMO_ASSERT(nse);
835 OSMO_ASSERT(sockaddr);
836
837 llist_for_each_entry(nsvc, &nse->nsvc, list) {
838 remote = gprs_ns2_ip_vc_sockaddr(nsvc);
839 if (!osmo_sockaddr_cmp(sockaddr, remote))
840 return nsvc;
841 }
842
843 return NULL;
844}
845
846
Harald Welte5bef2cc2020-09-18 22:33:24 +0200847/*! Bottom-side entry-point for received NS PDU from the driver/bind
Harald Welte5bef2cc2020-09-18 22:33:24 +0200848 * \param[in] nsvc NS-VC for which the message was received
849 * \param msg the received message. Ownership is trasnferred, caller must not free it!
850 * \return 0 on success; negative on error */
Alexander Couzensffd49d02020-09-24 00:47:17 +0200851int ns2_recv_vc(struct gprs_ns2_vc *nsvc,
Alexander Couzens6a161492020-07-12 13:45:50 +0200852 struct msgb *msg)
853{
854 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
855 struct tlv_parsed tp;
856 int rc = 0;
857
858 if (msg->len < sizeof(struct gprs_ns_hdr))
859 return -EINVAL;
860
861 switch (nsh->pdu_type) {
862 case SNS_PDUT_CONFIG:
863 /* one additional byte ('end flag') before the TLV part starts */
864 rc = ns2_tlv_parse(&tp, nsh->data+1,
865 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
866 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200867 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200868 return rc;
869 }
870 /* All sub-network service related message types */
871 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
872 break;
873 case SNS_PDUT_ACK:
874 case SNS_PDUT_ADD:
875 case SNS_PDUT_CHANGE_WEIGHT:
876 case SNS_PDUT_DELETE:
877 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
Harald Welte36be9d82020-10-09 15:39:25 +0200878 rc = ns2_tlv_parse(&tp, nsh->data+5,
879 msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +0200880 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200881 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200882 return rc;
883 }
884 tp.lv[NS_IE_NSEI].val = nsh->data+2;
885 tp.lv[NS_IE_NSEI].len = 2;
886 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
887 tp.lv[NS_IE_TRANS_ID].len = 1;
888 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
889 break;
890 case SNS_PDUT_CONFIG_ACK:
891 case SNS_PDUT_SIZE:
892 case SNS_PDUT_SIZE_ACK:
893 rc = ns2_tlv_parse(&tp, nsh->data,
894 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
895 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200896 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
Alexander Couzens6a161492020-07-12 13:45:50 +0200897 return rc;
898 }
899 /* All sub-network service related message types */
900 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
901 break;
902
903 case NS_PDUT_UNITDATA:
904 rc = gprs_ns2_vc_rx(nsvc, msg, NULL);
905 break;
906 default:
907 rc = ns2_tlv_parse(&tp, nsh->data,
908 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
909 if (rc < 0) {
Alexander Couzensbb0a53b2020-10-12 04:18:03 +0200910 LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200911 if (nsh->pdu_type != NS_PDUT_STATUS)
912 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
913 return rc;
914 }
915 rc = gprs_ns2_vc_rx(nsvc, msg, &tp);
916 break;
917 }
918
919 return rc;
920}
921
Harald Welte5bef2cc2020-09-18 22:33:24 +0200922/*! Notify a nse about the change of a NS-VC.
923 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
924 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +0200925void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
926{
927 struct gprs_ns2_nse *nse = nsvc->nse;
928 struct gprs_ns2_vc *tmp;
929
930 if (unblocked == nse->alive)
931 return;
932
933 if (unblocked) {
934 /* this is the first unblocked NSVC on an unavailable NSE */
935 nse->alive = true;
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200936 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_RECOVERY);
Alexander Couzensda0a2852020-10-01 23:24:07 +0200937 nse->first = false;
Alexander Couzens6a161492020-07-12 13:45:50 +0200938 return;
939 }
940
941 /* check if there are any remaining alive vcs */
942 llist_for_each_entry(tmp, &nse->nsvc, list) {
943 if (tmp == nsvc)
944 continue;
945
946 if (gprs_ns2_vc_is_unblocked(tmp)) {
947 /* there is at least one remaining alive NSVC */
948 return;
949 }
950 }
951
952 /* nse became unavailable */
953 nse->alive = false;
Alexander Couzensbf95f0f2020-10-01 22:56:03 +0200954 ns2_prim_status_ind(nse, 0, NS_AFF_CAUSE_FAILURE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200955}
956
957/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +0200958 * \param[in] ctx a talloc context to allocate NS instance from
959 * \param[in] cb Call-back function for dispatching primitives to the user
960 * \param[in] cb_data transparent user data passed to Call-back
961 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200962struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
963{
964 struct gprs_ns2_inst *nsi;
965
966 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
967 if (!nsi)
968 return NULL;
969
970 nsi->cb = cb;
971 nsi->cb_data = cb_data;
972 INIT_LLIST_HEAD(&nsi->binding);
973 INIT_LLIST_HEAD(&nsi->nse);
974
975 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
976 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
977 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
978 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
979 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
980 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
981 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
982 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
983
984 return nsi;
985}
986
Harald Welte5bef2cc2020-09-18 22:33:24 +0200987/*! Destroy a NS Instance (including all its NSEs, binds, ...).
988 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200989void gprs_ns2_free(struct gprs_ns2_inst *nsi)
990{
991 struct gprs_ns2_vc_bind *bind, *tbind;
992 struct gprs_ns2_nse *nse, *ntmp;
993
994 if (!nsi)
995 return;
996
997 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
998 gprs_ns2_free_nse(nse);
999 }
1000
1001 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1002 gprs_ns2_free_bind(bind);
1003 }
Alexander Couzens35315042020-10-10 03:28:17 +02001004
1005 talloc_free(nsi);
Alexander Couzens6a161492020-07-12 13:45:50 +02001006}
1007
Harald Welte5bef2cc2020-09-18 22:33:24 +02001008/*! Configure whether a NS Instance should dynamically create NSEs based on incoming traffic.
1009 * \param nsi the instance to modify
1010 * \param create_nse if NSE can be created on receiving package. SGSN set this.
1011 * \return 0 on success; negative on error
Alexander Couzens6a161492020-07-12 13:45:50 +02001012 */
1013int gprs_ns2_dynamic_create_nse(struct gprs_ns2_inst *nsi, bool create_nse)
1014{
1015 nsi->create_nse = create_nse;
1016
1017 return 0;
1018}
1019
Harald Welte5bef2cc2020-09-18 22:33:24 +02001020/*! Start the NS-ALIVE FSM in all NS-VCs of given NSE.
1021 * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */
Alexander Couzens6a161492020-07-12 13:45:50 +02001022void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse)
1023{
1024 struct gprs_ns2_vc *nsvc;
1025 OSMO_ASSERT(nse);
1026
1027 llist_for_each_entry(nsvc, &nse->nsvc, list) {
1028 if (nsvc->sns_only)
1029 continue;
1030
1031 gprs_ns2_vc_fsm_start(nsvc);
1032 }
1033}
1034
Harald Welte5bef2cc2020-09-18 22:33:24 +02001035/*! Set the mode of given bind.
1036 * \param[in] bind the bind we want to set the mode of
1037 * \param[in] modde mode to set bind to */
Alexander Couzens6a161492020-07-12 13:45:50 +02001038void gprs_ns2_bind_set_mode(struct gprs_ns2_vc_bind *bind, enum gprs_ns2_vc_mode mode)
1039{
1040 bind->vc_mode = mode;
1041}
1042
Harald Welte5bef2cc2020-09-18 22:33:24 +02001043/*! Destroy a given bind.
1044 * \param[in] bind the bind we want to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +02001045void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind)
1046{
1047 struct gprs_ns2_vc *nsvc, *tmp;
1048 if (!bind)
1049 return;
1050
1051 llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) {
1052 gprs_ns2_free_nsvc(nsvc);
1053 }
1054
1055 if (bind->driver->free_bind)
1056 bind->driver->free_bind(bind);
1057
1058 llist_del(&bind->list);
1059 talloc_free(bind);
1060}
1061/*! @} */