blob: 21a45a136835510beeed952314000a591799e364 [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)
219 return 0;
220
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 Couzens6a161492020-07-12 13:45:50 +0200347void ns2_prim_status_ind(struct gprs_ns2_inst *nsi,
348 uint16_t nsei, uint16_t bvci,
349 enum gprs_ns2_affecting_cause cause)
350{
351 struct osmo_gprs_ns2_prim nsp = {};
352 nsp.nsei = nsei;
353 nsp.bvci = bvci;
354 nsp.u.status.cause = cause;
355 nsp.u.status.transfer = -1;
356 osmo_prim_init(&nsp.oph, SAP_NS, PRIM_NS_STATUS,
357 PRIM_OP_INDICATION, NULL);
358 nsi->cb(&nsp.oph, nsi->cb_data);
359}
360
Harald Welte5bef2cc2020-09-18 22:33:24 +0200361/*! Allocate a NS-VC within the given bind + NSE.
362 * \param[in] bind The 'bind' on which we operate
363 * \param[in] nse The NS Entity on which we operate
364 * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater)
365 * \return newly allocated NS-VC on success; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200366struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater)
367{
368 struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc);
369
370 if (!nsvc)
371 return NULL;
372
373 nsvc->bind = bind;
374 nsvc->nse = nse;
375 nsvc->mode = bind->vc_mode;
376 nsvc->sig_weight = 1;
377 nsvc->data_weight = 1;
378
379 nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx);
380 if (!nsvc->ctrg) {
381 goto err;
382 }
383 nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx);
384 if (!nsvc->statg)
385 goto err_group;
386 if (!gprs_ns2_vc_fsm_alloc(nsvc, NULL, initiater))
387 goto err_statg;
388
389 bind->nsi->rate_ctr_idx++;
390
391 llist_add(&nsvc->list, &nse->nsvc);
392 llist_add(&nsvc->blist, &bind->nsvc);
393
394 return nsvc;
395
396err_statg:
397 osmo_stat_item_group_free(nsvc->statg);
398err_group:
399 rate_ctr_group_free(nsvc->ctrg);
400err:
401 talloc_free(nsvc);
402
403 return NULL;
404}
405
Harald Welte5bef2cc2020-09-18 22:33:24 +0200406/*! Destroy/release given NS-VC.
407 * \param[in] nsvc NS-VC to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200408void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc)
409{
410 if (!nsvc)
411 return;
412
413 ns2_prim_status_ind(nsvc->nse->nsi, nsvc->nse->nsei,
414 0, NS_AFF_CAUSE_VC_FAILURE);
415
416 llist_del(&nsvc->list);
417 llist_del(&nsvc->blist);
418
419 /* notify nse this nsvc is unavailable */
420 ns2_nse_notify_unblocked(nsvc, false);
421
422 /* check if sns is using this VC */
423 ns2_sns_free_nsvc(nsvc);
424 osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL);
425
426 /* let the driver/bind clean up it's internal state */
427 if (nsvc->priv && nsvc->bind->free_vc)
428 nsvc->bind->free_vc(nsvc);
429
430 osmo_stat_item_group_free(nsvc->statg);
431 rate_ctr_group_free(nsvc->ctrg);
432
433 talloc_free(nsvc);
434}
435
Harald Welte5bef2cc2020-09-18 22:33:24 +0200436/*! Allocate a message buffer for use with the NS2 stack. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200437struct msgb *gprs_ns2_msgb_alloc(void)
438{
439 struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM,
440 "GPRS/NS");
441 if (!msg) {
442 LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n",
443 NS_ALLOC_SIZE);
444 }
445 return msg;
446}
447
Harald Welte5bef2cc2020-09-18 22:33:24 +0200448/*! Create a status message to be sent over a new connection.
449 * \param[in] orig_msg the original message
450 * \param[in] tp TLVP parsed of the original message
451 * \param[out] reject callee-allocated message buffer of the generated NS-STATUS
452 * \param[in] cause Cause for the rejection
453 * \return 0 on success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200454static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause)
455{
456 struct msgb *msg = gprs_ns2_msgb_alloc();
457 struct gprs_ns_hdr *nsh;
458 bool have_vci = false;
459 uint8_t _cause = cause;
460 uint16_t nsei = 0;
461
462 if (!msg)
463 return -ENOMEM;
464
465 if (TLVP_PRESENT(tp, NS_IE_NSEI)) {
466 nsei = tlvp_val16be(tp, NS_IE_NSEI);
467
468 LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n",
469 nsei, gprs_ns2_cause_str(cause));
470 }
471
472 msg->l2h = msgb_put(msg, sizeof(*nsh));
473 nsh = (struct gprs_ns_hdr *) msg->l2h;
474 nsh->pdu_type = NS_PDUT_STATUS;
475
476 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause);
477 have_vci = TLVP_PRESENT(tp, NS_IE_VCI);
478
479 /* Section 9.2.7.1: Static conditions for NS-VCI */
480 if (cause == NS_CAUSE_NSVC_BLOCKED ||
481 cause == NS_CAUSE_NSVC_UNKNOWN) {
482 if (!have_vci) {
483 msgb_free(msg);
484 return -EINVAL;
485 }
486
487 msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI));
488 }
489
490 /* Section 9.2.7.2: Static conditions for NS PDU */
491 switch (cause) {
492 case NS_CAUSE_SEM_INCORR_PDU:
493 case NS_CAUSE_PDU_INCOMP_PSTATE:
494 case NS_CAUSE_PROTO_ERR_UNSPEC:
495 case NS_CAUSE_INVAL_ESSENT_IE:
496 case NS_CAUSE_MISSING_ESSENT_IE:
497 msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg),
498 orig_msg->l2h);
499 break;
500 default:
501 break;
502 }
503
504 *reject = msg;
505 return 0;
506}
507
Harald Welte5bef2cc2020-09-18 22:33:24 +0200508/*! Resolve a NS Entity based on its NSEI.
509 * \param[in] nsi NS Instance in which we do the look-up
510 * \param[in] nsei NSEI to look up
511 * \return NS Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200512struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei)
513{
514 struct gprs_ns2_nse *nse;
515
516 llist_for_each_entry(nse, &nsi->nse, list) {
517 if (nse->nsei == nsei)
518 return nse;
519 }
520
521 return NULL;
522}
523
Harald Welte5bef2cc2020-09-18 22:33:24 +0200524/*! Resolve a NS-VC Entity based on its NS-VCI.
525 * \param[in] nsi NS Instance in which we do the look-up
526 * \param[in] nsvci NS-VCI to look up
527 * \return NS-VC Entity in successful case; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200528struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci)
529{
530 struct gprs_ns2_nse *nse;
531 struct gprs_ns2_vc *nsvc;
532
533 llist_for_each_entry(nse, &nsi->nse, list) {
534 llist_for_each_entry(nsvc, &nse->nsvc, list) {
535 if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci)
536 return nsvc;
537 }
538 }
539
540 return NULL;
541}
542
Harald Welte5bef2cc2020-09-18 22:33:24 +0200543/*! Create a NS Entity within given NS instance.
544 * \param[in] nsi NS instance in which to create NS Entity
545 * \param[in] nsei NS Entity Identifier of to-be-created NSE
546 * \returns newly-allocated NS-E in successful case; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200547struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei)
548{
549 struct gprs_ns2_nse *nse;
550
551 nse = gprs_ns2_nse_by_nsei(nsi, nsei);
552 if (nse) {
553 LOGP(DLNS, LOGL_ERROR, "NSEI:%u Can not create a NSE with already taken NSEI\n", nsei);
554 return nse;
555 }
556
557 nse = talloc_zero(nsi, struct gprs_ns2_nse);
558 if (!nse)
559 return NULL;
560
561 nse->nsei = nsei;
562 nse->nsi = nsi;
563 llist_add(&nse->list, &nsi->nse);
564 INIT_LLIST_HEAD(&nse->nsvc);
565
566 return nse;
567}
568
Harald Welte5bef2cc2020-09-18 22:33:24 +0200569/*! Destroy given NS Entity.
570 * \param[in] nse NS Entity to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200571void gprs_ns2_free_nse(struct gprs_ns2_nse *nse)
572{
573 struct gprs_ns2_vc *nsvc, *tmp;
574
575 if (!nse)
576 return;
577
578 llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) {
579 gprs_ns2_free_nsvc(nsvc);
580 }
581
582 ns2_prim_status_ind(nse->nsi, nse->nsei,
583 0, NS_AFF_CAUSE_FAILURE);
584
585 llist_del(&nse->list);
586 if (nse->bss_sns_fi)
587 osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL);
588 talloc_free(nse);
589}
590
591static inline int ns2_tlv_parse(struct tlv_parsed *dec,
592 const uint8_t *buf, int buf_len, uint8_t lv_tag,
593 uint8_t lv_tag2)
594{
595 /* workaround for NS_IE_IP_ADDR not following any known TLV rules.
596 * See comment of ns_att_tlvdef1. */
597 int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2);
598 if (rc < 0)
599 return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2);
600 return rc;
601}
602
603
Harald Welte5bef2cc2020-09-18 22:33:24 +0200604/*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE.
605 * \param[in] bind the bind through which msg was received
606 * \param[in] msg the actual received message
607 * \param[in] logname A name to describe the VC. E.g. ip address pair
608 * \param[out] reject A message filled to be sent back. Only used in failure cases.
609 * \param[out] success A pointer which will be set to the new VC on success
610 * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */
Alexander Couzens6a161492020-07-12 13:45:50 +0200611enum gprs_ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind,
612 struct msgb *msg,
613 const char *logname,
614 struct msgb **reject,
615 struct gprs_ns2_vc **success)
616{
617 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h;
618 struct tlv_parsed tp;
619 struct gprs_ns2_vc *nsvc;
620 struct gprs_ns2_nse *nse;
621 uint16_t nsvci;
622 uint16_t nsei;
623
624 int rc;
625
626 if (msg->len < sizeof(struct gprs_ns_hdr))
627 return GPRS_NS2_CS_ERROR;
628
629 if (nsh->pdu_type == NS_PDUT_STATUS) {
630 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
631 LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s "
632 "for non-existing NS-VC\n",
633 logname);
634 return GPRS_NS2_CS_SKIPPED;
635 }
636
637 if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) {
638 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
639 LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s "
640 "for non-existing NS-VC\n",
641 logname);
642 return GPRS_NS2_CS_SKIPPED;
643 }
644
645 if (nsh->pdu_type == NS_PDUT_RESET_ACK) {
646 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
647 LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s "
648 "for non-existing NS-VC\n",
649 logname);
650 return GPRS_NS2_CS_SKIPPED;
651 }
652
Alexander Couzens48f63862020-09-12 02:19:19 +0200653 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
654 /* Only the RESET procedure creates a new NSVC */
655 if (nsh->pdu_type != NS_PDUT_RESET) {
656 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
Alexander Couzens6a161492020-07-12 13:45:50 +0200657
Alexander Couzens48f63862020-09-12 02:19:19 +0200658 if (rc < 0) {
659 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
660 return rc;
661 }
662 return GPRS_NS2_CS_REJECTED;
Alexander Couzens6a161492020-07-12 13:45:50 +0200663 }
Alexander Couzens48f63862020-09-12 02:19:19 +0200664 } else { /* NS2_VC_MODE_ALIVE */
665 /* Only the ALIVE procedure creates a new NSVC */
666 if (nsh->pdu_type != NS_PDUT_ALIVE) {
667 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE);
668
669 if (rc < 0) {
670 LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc);
671 return rc;
672 }
673 return GPRS_NS2_CS_REJECTED;
674 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200675 }
676
677 rc = ns2_tlv_parse(&tp, nsh->data,
678 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
679 if (rc < 0) {
680 LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during "
681 "TLV Parse\n", rc);
682 /* TODO: send invalid message back */
683 return GPRS_NS2_CS_REJECTED;
684 }
685
Alexander Couzens48f63862020-09-12 02:19:19 +0200686 if (bind->vc_mode == NS2_VC_MODE_BLOCKRESET) {
687 if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) ||
688 !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) {
689 LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n");
690 rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE);
691 return GPRS_NS2_CS_REJECTED;
692 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200693 }
694
695 /* find or create NSE */
696 nsei = tlvp_val16be(&tp, NS_IE_NSEI);
697 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
698 if (!nse) {
699 if (!bind->nsi->create_nse) {
700 return GPRS_NS2_CS_SKIPPED;
701 }
702
703 nse = gprs_ns2_create_nse(bind->nsi, nsei);
704 if (!nse) {
705 return GPRS_NS2_CS_ERROR;
706 }
707 }
708
709 nsvc = ns2_vc_alloc(bind, nse, false);
710 if (!nsvc)
711 return GPRS_NS2_CS_SKIPPED;
712
713 nsvc->ll = GPRS_NS_LL_UDP;
714
715 nsvci = tlvp_val16be(&tp, NS_IE_VCI);
716 nsvc->nsvci = nsvci;
717 nsvc->nsvci_is_valid = true;
718
719 *success = nsvc;
720
721 return GPRS_NS2_CS_CREATED;
722}
723
Harald Welte5bef2cc2020-09-18 22:33:24 +0200724/*! Create, and connect an inactive, new IP-based NS-VC
725 * \param[in] bind bind in which the new NS-VC is to be created
726 * \param[in] remote remote address to which to connect
727 * \param[in] nse NS Entity in which the NS-VC is to be created
728 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
729 * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200730struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind,
731 struct osmo_sockaddr *remote,
732 struct gprs_ns2_nse *nse,
733 uint16_t nsvci)
734{
735 struct gprs_ns2_vc *nsvc;
736
737 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
738 if (!nsvc)
739 return NULL;
740
741 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET) {
742 nsvc->nsvci = nsvci;
743 nsvc->nsvci_is_valid = true;
744 }
745
746 return nsvc;
747}
748
Harald Welte5bef2cc2020-09-18 22:33:24 +0200749/*! Create, connect and activate a new IP-based NS-VC
750 * \param[in] bind bind in which the new NS-VC is to be created
751 * \param[in] remote remote address to which to connect
752 * \param[in] nse NS Entity in which the NS-VC is to be created
753 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
754 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200755struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind,
756 struct osmo_sockaddr *remote,
757 struct gprs_ns2_nse *nse,
758 uint16_t nsvci)
759{
760 struct gprs_ns2_vc *nsvc;
761 nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci);
762 if (!nsvc)
763 return NULL;
764
765 gprs_ns2_vc_fsm_start(nsvc);
766
767 return nsvc;
768}
769
Harald Welte5bef2cc2020-09-18 22:33:24 +0200770/*! Create, connect and activate a new IP-based NS-VC
771 * \param[in] bind bind in which the new NS-VC is to be created
772 * \param[in] remote remote address to which to connect
773 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
774 * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET
775 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200776struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind,
777 struct osmo_sockaddr *remote,
778 uint16_t nsei,
779 uint16_t nsvci)
780{
781 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
782
783 if (!nse) {
784 nse = gprs_ns2_create_nse(bind->nsi, nsei);
785 if (!nse)
786 return NULL;
787 }
788
789 return gprs_ns2_ip_connect(bind, remote, nse, nsvci);
790}
791
Harald Welte5bef2cc2020-09-18 22:33:24 +0200792/*! Create, connect and activate a new IP-SNS NSE.
793 * \param[in] bind bind in which the new NS-VC is to be created
794 * \param[in] remote remote address to which to connect
795 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
796 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200797int gprs_ns2_ip_connect_sns(struct gprs_ns2_vc_bind *bind,
798 struct osmo_sockaddr *remote,
799 uint16_t nsei)
800{
801 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
802 struct gprs_ns2_vc *nsvc;
803
804 if (!nse) {
805 nse = gprs_ns2_create_nse(bind->nsi, nsei);
806 if (!nse)
807 return -1;
808 }
809
810 nsvc = gprs_ns2_ip_bind_connect(bind, nse, remote);
811 if (!nsvc)
812 return -1;
813
814 if (!nse->bss_sns_fi)
815 nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, NULL);
816
817 if (!nse->bss_sns_fi)
818 return -1;
819
820 return ns2_sns_bss_fsm_start(nse, nsvc, remote);
821}
822
Harald Welte5bef2cc2020-09-18 22:33:24 +0200823/*! Find NS-VC for given socket address.
824 * \param[in] nse NS Entity in which to search
825 * \param[in] sockaddr socket address to search for
826 * \return NS-VC matching sockaddr; NULL if none found */
Alexander Couzens6a161492020-07-12 13:45:50 +0200827struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr(struct gprs_ns2_nse *nse,
828 struct osmo_sockaddr *sockaddr)
829{
830 struct gprs_ns2_vc *nsvc;
831 struct osmo_sockaddr *remote;
832
833 OSMO_ASSERT(nse);
834 OSMO_ASSERT(sockaddr);
835
836 llist_for_each_entry(nsvc, &nse->nsvc, list) {
837 remote = gprs_ns2_ip_vc_sockaddr(nsvc);
838 if (!osmo_sockaddr_cmp(sockaddr, remote))
839 return nsvc;
840 }
841
842 return NULL;
843}
844
845
Harald Welte5bef2cc2020-09-18 22:33:24 +0200846/*! Bottom-side entry-point for received NS PDU from the driver/bind
847 * \param[in] nsi NS instance
848 * \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 Couzens6a161492020-07-12 13:45:50 +0200851int ns2_recv_vc(struct gprs_ns2_inst *nsi,
852 struct gprs_ns2_vc *nsvc,
853 struct msgb *msg)
854{
855 struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h;
856 struct tlv_parsed tp;
857 int rc = 0;
858
859 if (msg->len < sizeof(struct gprs_ns_hdr))
860 return -EINVAL;
861
862 switch (nsh->pdu_type) {
863 case SNS_PDUT_CONFIG:
864 /* one additional byte ('end flag') before the TLV part starts */
865 rc = ns2_tlv_parse(&tp, nsh->data+1,
866 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
867 if (rc < 0) {
868 LOGPC(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
869 return rc;
870 }
871 /* All sub-network service related message types */
872 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
873 break;
874 case SNS_PDUT_ACK:
875 case SNS_PDUT_ADD:
876 case SNS_PDUT_CHANGE_WEIGHT:
877 case SNS_PDUT_DELETE:
878 /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */
879 rc = ns2_tlv_parse(&tp, nsh->data+1,
880 msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0);
881 if (rc < 0) {
882 LOGPC(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
883 return rc;
884 }
885 tp.lv[NS_IE_NSEI].val = nsh->data+2;
886 tp.lv[NS_IE_NSEI].len = 2;
887 tp.lv[NS_IE_TRANS_ID].val = nsh->data+4;
888 tp.lv[NS_IE_TRANS_ID].len = 1;
889 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
890 break;
891 case SNS_PDUT_CONFIG_ACK:
892 case SNS_PDUT_SIZE:
893 case SNS_PDUT_SIZE_ACK:
894 rc = ns2_tlv_parse(&tp, nsh->data,
895 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
896 if (rc < 0) {
897 LOGPC(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg));
898 return rc;
899 }
900 /* All sub-network service related message types */
901 rc = gprs_ns2_sns_rx(nsvc, msg, &tp);
902 break;
903
904 case NS_PDUT_UNITDATA:
905 rc = gprs_ns2_vc_rx(nsvc, msg, NULL);
906 break;
907 default:
908 rc = ns2_tlv_parse(&tp, nsh->data,
909 msgb_l2len(msg) - sizeof(*nsh), 0, 0);
910 if (rc < 0) {
911 LOGPC(DLNS, LOGL_NOTICE, "Error during TLV Parse\n");
912 if (nsh->pdu_type != NS_PDUT_STATUS)
913 ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg);
914 return rc;
915 }
916 rc = gprs_ns2_vc_rx(nsvc, msg, &tp);
917 break;
918 }
919
920 return rc;
921}
922
Harald Welte5bef2cc2020-09-18 22:33:24 +0200923/*! Notify a nse about the change of a NS-VC.
924 * \param[in] nsvc NS-VC which has detected the change (and shall not be notified).
925 * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */
Alexander Couzens6a161492020-07-12 13:45:50 +0200926void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked)
927{
928 struct gprs_ns2_nse *nse = nsvc->nse;
929 struct gprs_ns2_vc *tmp;
930
931 if (unblocked == nse->alive)
932 return;
933
934 if (unblocked) {
935 /* this is the first unblocked NSVC on an unavailable NSE */
936 nse->alive = true;
937 ns2_prim_status_ind(nse->nsi, nse->nsei,
938 0, NS_AFF_CAUSE_RECOVERY);
939 return;
940 }
941
942 /* check if there are any remaining alive vcs */
943 llist_for_each_entry(tmp, &nse->nsvc, list) {
944 if (tmp == nsvc)
945 continue;
946
947 if (gprs_ns2_vc_is_unblocked(tmp)) {
948 /* there is at least one remaining alive NSVC */
949 return;
950 }
951 }
952
953 /* nse became unavailable */
954 nse->alive = false;
955 ns2_prim_status_ind(nse->nsi, nse->nsei,
956 0, NS_AFF_CAUSE_FAILURE);
957}
958
959/*! Create a new GPRS NS instance
Harald Welte5bef2cc2020-09-18 22:33:24 +0200960 * \param[in] ctx a talloc context to allocate NS instance from
961 * \param[in] cb Call-back function for dispatching primitives to the user
962 * \param[in] cb_data transparent user data passed to Call-back
963 * \returns dynamically allocated gprs_ns_inst; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200964struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data)
965{
966 struct gprs_ns2_inst *nsi;
967
968 nsi = talloc_zero(ctx, struct gprs_ns2_inst);
969 if (!nsi)
970 return NULL;
971
972 nsi->cb = cb;
973 nsi->cb_data = cb_data;
974 INIT_LLIST_HEAD(&nsi->binding);
975 INIT_LLIST_HEAD(&nsi->nse);
976
977 nsi->timeout[NS_TOUT_TNS_BLOCK] = 3;
978 nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3;
979 nsi->timeout[NS_TOUT_TNS_RESET] = 3;
980 nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3;
981 nsi->timeout[NS_TOUT_TNS_TEST] = 30;
982 nsi->timeout[NS_TOUT_TNS_ALIVE] = 3;
983 nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10;
984 nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */
985
986 return nsi;
987}
988
Harald Welte5bef2cc2020-09-18 22:33:24 +0200989/*! Destroy a NS Instance (including all its NSEs, binds, ...).
990 * \param[in] nsi NS instance to destroy */
Alexander Couzens6a161492020-07-12 13:45:50 +0200991void gprs_ns2_free(struct gprs_ns2_inst *nsi)
992{
993 struct gprs_ns2_vc_bind *bind, *tbind;
994 struct gprs_ns2_nse *nse, *ntmp;
995
996 if (!nsi)
997 return;
998
999 llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) {
1000 gprs_ns2_free_nse(nse);
1001 }
1002
1003 llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) {
1004 gprs_ns2_free_bind(bind);
1005 }
1006}
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/*! @} */