blob: de30470b24b8edd94d19605fda6bf719155f49cf [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \file gprs_ns2_message.c
2 * NS-over-FR-over-GRE implementation.
3 * GPRS Networks Service (NS) messages on the Gb interface.
4 * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05)
5 * as well as its successor 3GPP TS 48.016 */
6
7/* (C) 2020 sysmocom - s.f.m.c. GmbH
8 * Author: Alexander Couzens <lynxis@fe80.eu>
9 *
10 * All Rights Reserved
11 *
12 * SPDX-License-Identifier: GPL-2.0+
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 *
27 */
28
29#include <errno.h>
30
31#include <osmocom/core/byteswap.h>
32#include <osmocom/core/rate_ctr.h>
33#include <osmocom/core/stat_item.h>
34#include <osmocom/core/stats.h>
35#include <osmocom/gsm/tlv.h>
36#include <osmocom/gprs/gprs_msgb.h>
37#include <osmocom/gprs/gprs_ns2.h>
38#include <osmocom/gprs/protocol/gsm_08_16.h>
39
40#include "gprs_ns2_internal.h"
41
42#define ERR_IF_NSVC_USES_SNS(nsvc, reason) \
43 do { \
44 if (!nsvc->nse->bss_sns_fi) \
45 break; \
Harald Weltef2949742021-01-20 14:54:14 +010046 LOGNSVC(nsvc, LOGL_DEBUG, "invalid packet %s with SNS\n", reason); \
Alexander Couzens6a161492020-07-12 13:45:50 +020047 } while (0)
48
Alexander Couzensf7e2cac2021-01-25 16:18:21 +010049static int ns2_validate_reset(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp, uint8_t *cause)
Alexander Couzens6a161492020-07-12 13:45:50 +020050{
Harald Welte798efea2020-12-03 16:01:02 +010051 if (!TLVP_PRES_LEN(tp, NS_IE_CAUSE, 1) ||
52 !TLVP_PRES_LEN(tp, NS_IE_VCI, 2) || !TLVP_PRES_LEN(tp, NS_IE_NSEI, 2)) {
Alexander Couzens6a161492020-07-12 13:45:50 +020053 *cause = NS_CAUSE_MISSING_ESSENT_IE;
54 return -1;
55 }
56
57 return 0;
58}
59
Alexander Couzensf7e2cac2021-01-25 16:18:21 +010060static int ns2_validate_reset_ack(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp, uint8_t *cause)
Alexander Couzens6a161492020-07-12 13:45:50 +020061{
Harald Welte798efea2020-12-03 16:01:02 +010062 if (!TLVP_PRES_LEN(tp, NS_IE_VCI, 2) || !TLVP_PRES_LEN(tp, NS_IE_NSEI, 2)) {
Alexander Couzens6a161492020-07-12 13:45:50 +020063 *cause = NS_CAUSE_MISSING_ESSENT_IE;
64 return -1;
65 }
66
67 return 0;
68}
69
Alexander Couzensf7e2cac2021-01-25 16:18:21 +010070static int ns2_validate_block(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp, uint8_t *cause)
Alexander Couzens6a161492020-07-12 13:45:50 +020071{
Harald Welte798efea2020-12-03 16:01:02 +010072 if (!TLVP_PRES_LEN(tp, NS_IE_VCI, 2) || !TLVP_PRES_LEN(tp, NS_IE_CAUSE, 1)) {
Alexander Couzens6a161492020-07-12 13:45:50 +020073 *cause = NS_CAUSE_MISSING_ESSENT_IE;
74 return -1;
75 }
76
77 return 0;
78}
79
Alexander Couzensf7e2cac2021-01-25 16:18:21 +010080static int ns2_validate_block_ack(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp, uint8_t *cause)
Alexander Couzens6a161492020-07-12 13:45:50 +020081{
Harald Welte798efea2020-12-03 16:01:02 +010082 if (!TLVP_PRES_LEN(tp, NS_IE_VCI, 2)) {
Alexander Couzens6a161492020-07-12 13:45:50 +020083 *cause = NS_CAUSE_MISSING_ESSENT_IE;
84 return -1;
85 }
86
87 return 0;
88}
89
Alexander Couzensf7e2cac2021-01-25 16:18:21 +010090static int ns2_validate_status(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp, uint8_t *cause)
Alexander Couzens6a161492020-07-12 13:45:50 +020091{
92
Harald Welte798efea2020-12-03 16:01:02 +010093 if (!TLVP_PRES_LEN(tp, NS_IE_CAUSE, 1)) {
Alexander Couzens6a161492020-07-12 13:45:50 +020094 *cause = NS_CAUSE_MISSING_ESSENT_IE;
95 return -1;
96 }
97
Alexander Couzenseec4f602021-09-06 18:17:15 +020098 uint8_t _cause = tlvp_val8(tp, NS_IE_CAUSE, 0);
Alexander Couzens6a161492020-07-12 13:45:50 +020099 switch (_cause) {
100 case NS_CAUSE_NSVC_BLOCKED:
101 case NS_CAUSE_NSVC_UNKNOWN:
Alexander Couzenseec4f602021-09-06 18:17:15 +0200102 if (!TLVP_PRES_LEN(tp, NS_IE_VCI, 1)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200103 *cause = NS_CAUSE_MISSING_ESSENT_IE;
104 return -1;
105 }
Alexander Couzensa2b846b2021-09-07 15:30:02 +0200106
107 if (nsvc->mode != GPRS_NS2_VC_MODE_BLOCKRESET) {
108 *cause = NS_CAUSE_PDU_INCOMP_PSTATE;
109 return -1;
110 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200111 break;
112 case NS_CAUSE_SEM_INCORR_PDU:
113 case NS_CAUSE_PDU_INCOMP_PSTATE:
114 case NS_CAUSE_PROTO_ERR_UNSPEC:
115 case NS_CAUSE_INVAL_ESSENT_IE:
116 case NS_CAUSE_MISSING_ESSENT_IE:
Alexander Couzenseec4f602021-09-06 18:17:15 +0200117 if (!TLVP_PRES_LEN(tp, NS_IE_PDU, 1)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200118 *cause = NS_CAUSE_MISSING_ESSENT_IE;
119 return -1;
120 }
121 break;
122 case NS_CAUSE_BVCI_UNKNOWN:
Harald Welte798efea2020-12-03 16:01:02 +0100123 if (!TLVP_PRES_LEN(tp, NS_IE_BVCI, 2)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200124 *cause = NS_CAUSE_MISSING_ESSENT_IE;
125 return -1;
126 }
127 break;
128 case NS_CAUSE_UNKN_IP_TEST_FAILED:
Harald Welte798efea2020-12-03 16:01:02 +0100129 if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST) && !TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200130 *cause = NS_CAUSE_MISSING_ESSENT_IE;
131 return -1;
132 }
133 break;
134 }
135
136 return 0;
137}
138
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100139int ns2_validate(struct gprs_ns2_vc *nsvc,
140 uint8_t pdu_type,
141 struct msgb *msg,
142 struct tlv_parsed *tp,
143 uint8_t *cause)
Alexander Couzens6a161492020-07-12 13:45:50 +0200144{
145 switch (pdu_type) {
146 case NS_PDUT_RESET:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100147 return ns2_validate_reset(nsvc, msg, tp, cause);
Alexander Couzens6a161492020-07-12 13:45:50 +0200148 case NS_PDUT_RESET_ACK:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100149 return ns2_validate_reset_ack(nsvc, msg, tp, cause);
Alexander Couzens6a161492020-07-12 13:45:50 +0200150 case NS_PDUT_BLOCK:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100151 return ns2_validate_block(nsvc, msg, tp, cause);
Alexander Couzens6a161492020-07-12 13:45:50 +0200152 case NS_PDUT_BLOCK_ACK:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100153 return ns2_validate_block_ack(nsvc, msg, tp, cause);
Alexander Couzens6a161492020-07-12 13:45:50 +0200154 case NS_PDUT_STATUS:
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100155 return ns2_validate_status(nsvc, msg, tp, cause);
Alexander Couzens6a161492020-07-12 13:45:50 +0200156
157 /* following PDUs doesn't have any payloads */
158 case NS_PDUT_ALIVE:
159 case NS_PDUT_ALIVE_ACK:
160 case NS_PDUT_UNBLOCK:
161 case NS_PDUT_UNBLOCK_ACK:
162 if (msgb_l2len(msg) != sizeof(struct gprs_ns_hdr)) {
163 *cause = NS_CAUSE_PROTO_ERR_UNSPEC;
164 return -1;
165 }
166 break;
167 }
168
169 return 0;
170}
171
172
Harald Welte5a5bf722021-01-30 21:49:28 +0100173static int ns_vc_tx(struct gprs_ns2_vc *nsvc, struct msgb *msg)
174{
Harald Weltef22ae5a2021-01-30 22:01:28 +0100175 unsigned int bytes = msgb_length(msg);
176 int rc;
Harald Weltee5f55f72021-01-30 21:51:15 +0100177
Harald Weltef22ae5a2021-01-30 22:01:28 +0100178
179 rc = nsvc->bind->send_vc(nsvc, msg);
180 if (rc < 0) {
Daniel Willmannefa64f92021-07-09 20:35:00 +0200181 RATE_CTR_INC_NS(nsvc, NS_CTR_PKTS_OUT_DROP);
182 RATE_CTR_ADD_NS(nsvc, NS_CTR_BYTES_OUT_DROP, bytes);
Harald Weltef22ae5a2021-01-30 22:01:28 +0100183 } else {
Daniel Willmannefa64f92021-07-09 20:35:00 +0200184 RATE_CTR_INC_NS(nsvc, NS_CTR_PKTS_OUT);
185 RATE_CTR_ADD_NS(nsvc, NS_CTR_BYTES_OUT, bytes);
Harald Weltef22ae5a2021-01-30 22:01:28 +0100186 }
187
188 return rc;
Harald Welte5a5bf722021-01-30 21:49:28 +0100189}
190
Alexander Couzens6a161492020-07-12 13:45:50 +0200191/* transmit functions */
192static int ns2_tx_simple(struct gprs_ns2_vc *nsvc, uint8_t pdu_type)
193{
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100194 struct msgb *msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200195 struct gprs_ns_hdr *nsh;
196
Daniel Willmann751977b2020-12-02 18:59:44 +0100197 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200198 log_set_context(LOG_CTX_GB_NSVC, nsvc);
199
200 if (!msg)
201 return -ENOMEM;
202
203 msg->l2h = msgb_put(msg, sizeof(*nsh));
204 nsh = (struct gprs_ns_hdr *) msg->l2h;
Alexander Couzens6a161492020-07-12 13:45:50 +0200205 nsh->pdu_type = pdu_type;
206
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100207 LOG_NS_TX_SIGNAL(nsvc, nsh->pdu_type);
Harald Welte5a5bf722021-01-30 21:49:28 +0100208 return ns_vc_tx(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200209}
210
Harald Welte5bef2cc2020-09-18 22:33:24 +0200211/*! Transmit a NS-BLOCK on a given NS-VC.
Alexander Couzens6a161492020-07-12 13:45:50 +0200212 * \param[in] vc NS-VC on which the NS-BLOCK is to be transmitted
213 * \param[in] cause Numeric NS Cause value
Alexander Couzens67cfc5d2021-09-07 01:10:38 +0200214 * \param[in] nsvci if given this NSVCI will be encoded. If NULL the nsvc->nsvci will be used.
Harald Welte5bef2cc2020-09-18 22:33:24 +0200215 * \returns 0 in case of success */
Alexander Couzens67cfc5d2021-09-07 01:10:38 +0200216int ns2_tx_block(struct gprs_ns2_vc *nsvc, uint8_t cause, uint16_t *nsvci)
Alexander Couzens6a161492020-07-12 13:45:50 +0200217{
218 struct msgb *msg;
219 struct gprs_ns_hdr *nsh;
Alexander Couzens67cfc5d2021-09-07 01:10:38 +0200220 uint16_t encoded_nsvci;
221
222 if (nsvci)
223 encoded_nsvci = osmo_htons(*nsvci);
224 else
225 encoded_nsvci = osmo_htons(nsvc->nsvci);
Alexander Couzens6a161492020-07-12 13:45:50 +0200226
Daniel Willmann751977b2020-12-02 18:59:44 +0100227 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200228 log_set_context(LOG_CTX_GB_NSVC, nsvc);
229
230 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS BLOCK");
231
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100232 msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200233 if (!msg)
234 return -ENOMEM;
235
Pau Espin Pedrol7b894a72021-06-04 18:17:12 +0200236 rate_ctr_inc(rate_ctr_group_get_ctr(nsvc->ctrg, NS_CTR_BLOCKED));
Alexander Couzens6a161492020-07-12 13:45:50 +0200237
238 msg->l2h = msgb_put(msg, sizeof(*nsh));
239 nsh = (struct gprs_ns_hdr *) msg->l2h;
240 nsh->pdu_type = NS_PDUT_BLOCK;
241
242 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
Alexander Couzens67cfc5d2021-09-07 01:10:38 +0200243 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &encoded_nsvci);
Alexander Couzens6a161492020-07-12 13:45:50 +0200244
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100245 LOG_NS_SIGNAL(nsvc, "Tx", nsh->pdu_type, LOGL_INFO, " cause=%s\n", gprs_ns2_cause_str(cause));
Harald Welte5a5bf722021-01-30 21:49:28 +0100246 return ns_vc_tx(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200247}
248
Harald Welte5bef2cc2020-09-18 22:33:24 +0200249/*! Transmit a NS-BLOCK-ACK on a given NS-VC.
Alexander Couzens6a161492020-07-12 13:45:50 +0200250 * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted
Alexander Couzens67cfc5d2021-09-07 01:10:38 +0200251 * \param[in] nsvci if given this NSVCI will be encoded. If NULL the nsvc->nsvci will be used.
Harald Welte5bef2cc2020-09-18 22:33:24 +0200252 * \returns 0 in case of success */
Alexander Couzens67cfc5d2021-09-07 01:10:38 +0200253int ns2_tx_block_ack(struct gprs_ns2_vc *nsvc, uint16_t *nsvci)
Alexander Couzens6a161492020-07-12 13:45:50 +0200254{
255 struct msgb *msg;
256 struct gprs_ns_hdr *nsh;
Alexander Couzens67cfc5d2021-09-07 01:10:38 +0200257 uint16_t encoded_nsvci;
258
259 if (nsvci)
260 encoded_nsvci = osmo_htons(*nsvci);
261 else
262 encoded_nsvci = osmo_htons(nsvc->nsvci);
Alexander Couzens6a161492020-07-12 13:45:50 +0200263
Daniel Willmann751977b2020-12-02 18:59:44 +0100264 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200265 log_set_context(LOG_CTX_GB_NSVC, nsvc);
266
267 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS BLOCK ACK");
268
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100269 msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200270 if (!msg)
271 return -ENOMEM;
272
Alexander Couzens6a161492020-07-12 13:45:50 +0200273 msg->l2h = msgb_put(msg, sizeof(*nsh));
274 nsh = (struct gprs_ns_hdr *) msg->l2h;
275 nsh->pdu_type = NS_PDUT_BLOCK_ACK;
276
Alexander Couzens67cfc5d2021-09-07 01:10:38 +0200277 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &encoded_nsvci);
Alexander Couzens6a161492020-07-12 13:45:50 +0200278
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100279 LOG_NS_TX_SIGNAL(nsvc, nsh->pdu_type);
Harald Welte5a5bf722021-01-30 21:49:28 +0100280 return ns_vc_tx(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200281}
282
Harald Welte5bef2cc2020-09-18 22:33:24 +0200283/*! Transmit a NS-RESET on a given NS-VC.
Alexander Couzens6a161492020-07-12 13:45:50 +0200284 * \param[in] nsvc NS-VC used for transmission
285 * \paam[in] cause Numeric NS cause value
Harald Welte5bef2cc2020-09-18 22:33:24 +0200286 * \returns 0 in case of success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200287int ns2_tx_reset(struct gprs_ns2_vc *nsvc, uint8_t cause)
288{
289 struct msgb *msg;
290 struct gprs_ns_hdr *nsh;
291 uint16_t nsvci = osmo_htons(nsvc->nsvci);
292 uint16_t nsei = osmo_htons(nsvc->nse->nsei);
293
Daniel Willmann751977b2020-12-02 18:59:44 +0100294 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200295 log_set_context(LOG_CTX_GB_NSVC, nsvc);
296
297 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS RESET");
298
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100299 msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200300 if (!msg)
301 return -ENOMEM;
302
Alexander Couzens6a161492020-07-12 13:45:50 +0200303 msg->l2h = msgb_put(msg, sizeof(*nsh));
304 nsh = (struct gprs_ns_hdr *) msg->l2h;
305 nsh->pdu_type = NS_PDUT_RESET;
306
307 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
308 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci);
309 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei);
310
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100311 LOG_NS_SIGNAL(nsvc, "Tx", nsh->pdu_type, LOGL_INFO, " cause=%s\n", gprs_ns2_cause_str(cause));
Harald Welte5a5bf722021-01-30 21:49:28 +0100312 return ns_vc_tx(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200313}
314
Harald Welte5bef2cc2020-09-18 22:33:24 +0200315/*! Transmit a NS-RESET-ACK on a given NS-VC.
316 * \param[in] nsvc NS-VC used for transmission
317 * \returns 0 in case of success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200318int ns2_tx_reset_ack(struct gprs_ns2_vc *nsvc)
319{
320 struct msgb *msg;
321 struct gprs_ns_hdr *nsh;
322 uint16_t nsvci, nsei;
323
Harald Welte5bef2cc2020-09-18 22:33:24 +0200324 /* Section 9.2.6 */
Daniel Willmann751977b2020-12-02 18:59:44 +0100325 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200326 log_set_context(LOG_CTX_GB_NSVC, nsvc);
327
328 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS RESET ACK");
329
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100330 msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200331 if (!msg)
332 return -ENOMEM;
333
334 nsvci = osmo_htons(nsvc->nsvci);
335 nsei = osmo_htons(nsvc->nse->nsei);
336
337 msg->l2h = msgb_put(msg, sizeof(*nsh));
338 nsh = (struct gprs_ns_hdr *) msg->l2h;
339
340 nsh->pdu_type = NS_PDUT_RESET_ACK;
341
Alexander Couzens6a161492020-07-12 13:45:50 +0200342 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
343 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
344
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100345 LOG_NS_TX_SIGNAL(nsvc, nsh->pdu_type);
Harald Welte5a5bf722021-01-30 21:49:28 +0100346 return ns_vc_tx(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200347}
348
Harald Welte5bef2cc2020-09-18 22:33:24 +0200349/*! Transmit a NS-UNBLOCK on a given NS-VC.
Alexander Couzens6a161492020-07-12 13:45:50 +0200350 * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted
Harald Welte5bef2cc2020-09-18 22:33:24 +0200351 * \returns 0 in case of success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200352int ns2_tx_unblock(struct gprs_ns2_vc *nsvc)
353{
Daniel Willmann751977b2020-12-02 18:59:44 +0100354 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200355 log_set_context(LOG_CTX_GB_NSVC, nsvc);
356
357 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS UNBLOCK");
358
Alexander Couzens6a161492020-07-12 13:45:50 +0200359 return ns2_tx_simple(nsvc, NS_PDUT_UNBLOCK);
360}
361
362
Harald Welte5bef2cc2020-09-18 22:33:24 +0200363/*! Transmit a NS-UNBLOCK-ACK on a given NS-VC.
Alexander Couzens6a161492020-07-12 13:45:50 +0200364 * \param[in] nsvc NS-VC on which the NS-UNBLOCK-ACK is to be transmitted
Harald Welte5bef2cc2020-09-18 22:33:24 +0200365 * \returns 0 in case of success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200366int ns2_tx_unblock_ack(struct gprs_ns2_vc *nsvc)
367{
Daniel Willmann751977b2020-12-02 18:59:44 +0100368 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200369 log_set_context(LOG_CTX_GB_NSVC, nsvc);
370
371 ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS UNBLOCK ACK");
372
Alexander Couzens6a161492020-07-12 13:45:50 +0200373 return ns2_tx_simple(nsvc, NS_PDUT_UNBLOCK_ACK);
374}
375
Harald Welte5bef2cc2020-09-18 22:33:24 +0200376/*! Transmit a NS-ALIVE on a given NS-VC.
Alexander Couzens6a161492020-07-12 13:45:50 +0200377 * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted
Harald Welte5bef2cc2020-09-18 22:33:24 +0200378 * \returns 0 in case of success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200379int ns2_tx_alive(struct gprs_ns2_vc *nsvc)
380{
Daniel Willmann751977b2020-12-02 18:59:44 +0100381 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200382 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200383
384 return ns2_tx_simple(nsvc, NS_PDUT_ALIVE);
385}
386
Harald Welte5bef2cc2020-09-18 22:33:24 +0200387/*! Transmit a NS-ALIVE-ACK on a given NS-VC.
Alexander Couzens6a161492020-07-12 13:45:50 +0200388 * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted
Harald Welte5bef2cc2020-09-18 22:33:24 +0200389 * \returns 0 in case of success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200390int ns2_tx_alive_ack(struct gprs_ns2_vc *nsvc)
391{
Daniel Willmann751977b2020-12-02 18:59:44 +0100392 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200393 log_set_context(LOG_CTX_GB_NSVC, nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200394
395 return ns2_tx_simple(nsvc, NS_PDUT_ALIVE_ACK);
396}
397
Harald Welte5bef2cc2020-09-18 22:33:24 +0200398/*! Transmit NS-UNITDATA on a given NS-VC.
399 * \param[in] nsvc NS-VC on which the NS-UNITDATA is to be transmitted
400 * \param[in] bvci BVCI to encode in NS-UNITDATA header
401 * \param[in] sducontrol SDU control octet of NS header
402 * \param[in] msg message buffer containing payload
403 * \returns 0 in case of success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200404int ns2_tx_unit_data(struct gprs_ns2_vc *nsvc,
405 uint16_t bvci, uint8_t sducontrol,
406 struct msgb *msg)
407{
408 struct gprs_ns_hdr *nsh;
409
Daniel Willmann751977b2020-12-02 18:59:44 +0100410 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200411 log_set_context(LOG_CTX_GB_NSVC, nsvc);
412
413 msg->l2h = msgb_push(msg, sizeof(*nsh) + 3);
414 nsh = (struct gprs_ns_hdr *) msg->l2h;
415 if (!nsh) {
Harald Weltef2949742021-01-20 14:54:14 +0100416 LOGNSVC(nsvc, LOGL_ERROR, "Not enough headroom for NS header\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200417 msgb_free(msg);
418 return -EIO;
419 }
420
421 nsh->pdu_type = NS_PDUT_UNITDATA;
422 nsh->data[0] = sducontrol;
423 nsh->data[1] = bvci >> 8;
424 nsh->data[2] = bvci & 0xff;
425
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100426 LOG_NS_DATA(nsvc, "Tx", nsh->pdu_type, LOGL_INFO, "\n");
Harald Welte5a5bf722021-01-30 21:49:28 +0100427 return ns_vc_tx(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200428}
429
Harald Welte5bef2cc2020-09-18 22:33:24 +0200430/*! Transmit a NS-STATUS on a given NS-VC.
Alexander Couzens6a161492020-07-12 13:45:50 +0200431 * \param[in] nsvc NS-VC to be used for transmission
432 * \param[in] cause Numeric NS cause value
433 * \param[in] bvci BVCI to be reset within NSVC
Harald Welte5bef2cc2020-09-18 22:33:24 +0200434 * \param[in] orig_msg message causing the STATUS
435 * \returns 0 in case of success */
Alexander Couzens6a161492020-07-12 13:45:50 +0200436int ns2_tx_status(struct gprs_ns2_vc *nsvc, uint8_t cause,
437 uint16_t bvci, struct msgb *orig_msg)
438{
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100439 struct msgb *msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200440 struct gprs_ns_hdr *nsh;
441 uint16_t nsvci = osmo_htons(nsvc->nsvci);
Alexander Couzenscf1fa632021-02-15 04:51:14 +0100442 unsigned int orig_len, max_orig_len;
Alexander Couzens6a161492020-07-12 13:45:50 +0200443
Daniel Willmann751977b2020-12-02 18:59:44 +0100444 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200445 log_set_context(LOG_CTX_GB_NSVC, nsvc);
446
447 bvci = osmo_htons(bvci);
448
449 if (!msg)
450 return -ENOMEM;
451
Alexander Couzens6a161492020-07-12 13:45:50 +0200452 msg->l2h = msgb_put(msg, sizeof(*nsh));
453 nsh = (struct gprs_ns_hdr *) msg->l2h;
454 nsh->pdu_type = NS_PDUT_STATUS;
455
456 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause);
457
Alexander Couzens6a161492020-07-12 13:45:50 +0200458 switch (cause) {
Alexander Couzensce646c22021-02-15 04:55:08 +0100459 case NS_CAUSE_NSVC_BLOCKED:
460 case NS_CAUSE_NSVC_UNKNOWN:
461 /* Section 9.2.7.1: Static conditions for NS-VCI */
462 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci);
463 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200464 case NS_CAUSE_SEM_INCORR_PDU:
465 case NS_CAUSE_PDU_INCOMP_PSTATE:
466 case NS_CAUSE_PROTO_ERR_UNSPEC:
467 case NS_CAUSE_INVAL_ESSENT_IE:
468 case NS_CAUSE_MISSING_ESSENT_IE:
Alexander Couzensce646c22021-02-15 04:55:08 +0100469 /* Section 9.2.7.2: Static conditions for NS PDU */
Alexander Couzenscf1fa632021-02-15 04:51:14 +0100470 /* ensure the PDU doesn't exceed the MTU */
471 orig_len = msgb_l2len(orig_msg);
472 max_orig_len = msgb_length(msg) + TVLV_GROSS_LEN(orig_len);
473 if (max_orig_len > nsvc->bind->mtu)
474 orig_len -= max_orig_len - nsvc->bind->mtu;
475 msgb_tvlv_put(msg, NS_IE_PDU, orig_len, orig_msg->l2h);
Alexander Couzens6a161492020-07-12 13:45:50 +0200476 break;
Alexander Couzensce646c22021-02-15 04:55:08 +0100477 case NS_CAUSE_BVCI_UNKNOWN:
478 /* Section 9.2.7.3: Static conditions for BVCI */
479 msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci);
480 break;
481
Alexander Couzens6a161492020-07-12 13:45:50 +0200482 default:
483 break;
484 }
485
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100486 LOG_NS_SIGNAL(nsvc, "Tx", nsh->pdu_type, LOGL_INFO, " cause=%s\n", gprs_ns2_cause_str(cause));
Harald Welte5a5bf722021-01-30 21:49:28 +0100487 return ns_vc_tx(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200488}
489
Alexander Couzens27a55922021-02-27 01:08:35 +0100490/*! Encode + Transmit a SNS-ADD/SNS-CHANGE-WEIGHT as per Section 9.3.2/9.3.3.
491 * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG
492 * \param[in] pdu The PDU type to send out
493 * \param[in] trans_id The transaction id
494 * \param[in] ip4_elems Array of IPv4 Elements
495 * \param[in] num_ip4_elems number of ip4_elems
496 * \param[in] ip6_elems Array of IPv6 Elements
497 * \param[in] num_ip6_elems number of ip6_elems
498 * \returns 0 on success; negative in case of error */
499static int ns2_tx_sns_procedure(struct gprs_ns2_vc *nsvc,
500 enum ns_pdu_type pdu,
501 uint8_t trans_id,
502 const struct gprs_ns_ie_ip4_elem *ip4_elems,
503 unsigned int num_ip4_elems,
504 const struct gprs_ns_ie_ip6_elem *ip6_elems,
505 unsigned int num_ip6_elems)
506{
507 struct msgb *msg;
508 struct gprs_ns_hdr *nsh;
509 uint16_t nsei;
510
511 if (!nsvc)
512 return -EINVAL;
513
514 if (!ip4_elems && !ip6_elems)
515 return -EINVAL;
516
517 msg = ns2_msgb_alloc();
518
519 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
520 log_set_context(LOG_CTX_GB_NSVC, nsvc);
521 if (!msg)
522 return -ENOMEM;
523
524 if (!nsvc->nse->bss_sns_fi) {
525 LOGNSVC(nsvc, LOGL_ERROR, "Cannot transmit SNS on NSVC without SNS active\n");
526 msgb_free(msg);
527 return -EIO;
528 }
529
530 nsei = osmo_htons(nsvc->nse->nsei);
531
532 msg->l2h = msgb_put(msg, sizeof(*nsh));
533 nsh = (struct gprs_ns_hdr *) msg->l2h;
534 nsh->pdu_type = pdu;
535 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
536 msgb_v_put(msg, trans_id);
537
538 /* List of IP4 Elements 10.3.2c */
539 if (ip4_elems) {
540 msgb_tvlv_put(msg, NS_IE_IPv4_LIST, num_ip4_elems*sizeof(struct gprs_ns_ie_ip4_elem),
541 (const uint8_t *)ip4_elems);
542 } else if (ip6_elems) {
543 /* List of IP6 elements 10.3.2d */
544 msgb_tvlv_put(msg, NS_IE_IPv6_LIST, num_ip6_elems*sizeof(struct gprs_ns_ie_ip6_elem),
545 (const uint8_t *)ip6_elems);
546 }
547
548 return ns_vc_tx(nsvc, msg);
549}
550
551/*! Encode + Transmit a SNS-ADD as per Section 9.3.2.
552 * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG
553 * \param[in] trans_id The transaction id
554 * \param[in] ip4_elems Array of IPv4 Elements
555 * \param[in] num_ip4_elems number of ip4_elems
556 * \param[in] ip6_elems Array of IPv6 Elements
557 * \param[in] num_ip6_elems number of ip6_elems
558 * \returns 0 on success; negative in case of error */
559int ns2_tx_sns_add(struct gprs_ns2_vc *nsvc,
560 uint8_t trans_id,
561 const struct gprs_ns_ie_ip4_elem *ip4_elems,
562 unsigned int num_ip4_elems,
563 const struct gprs_ns_ie_ip6_elem *ip6_elems,
564 unsigned int num_ip6_elems)
565{
566 return ns2_tx_sns_procedure(nsvc, SNS_PDUT_ADD, trans_id, ip4_elems, num_ip4_elems, ip6_elems, num_ip6_elems);
567}
568
569/*! Encode + Transmit a SNS-CHANGE-WEIGHT as per Section 9.3.3.
570 * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG
571 * \param[in] trans_id The transaction id
572 * \param[in] ip4_elems Array of IPv4 Elements
573 * \param[in] num_ip4_elems number of ip4_elems
574 * \param[in] ip6_elems Array of IPv6 Elements
575 * \param[in] num_ip6_elems number of ip6_elems
576 * \returns 0 on success; negative in case of error */
577int ns2_tx_sns_change_weight(struct gprs_ns2_vc *nsvc,
578 uint8_t trans_id,
579 const struct gprs_ns_ie_ip4_elem *ip4_elems,
580 unsigned int num_ip4_elems,
581 const struct gprs_ns_ie_ip6_elem *ip6_elems,
582 unsigned int num_ip6_elems)
583{
584 return ns2_tx_sns_procedure(nsvc, SNS_PDUT_CHANGE_WEIGHT, trans_id, ip4_elems, num_ip4_elems, ip6_elems, num_ip6_elems);
585}
586
587/*! Encode + Transmit a SNS-DEL as per Section 9.3.6.
588 * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG
589 * \param[in] trans_id The transaction id
590 * \param[in] ip4_elems Array of IPv4 Elements
591 * \param[in] num_ip4_elems number of ip4_elems
592 * \param[in] ip6_elems Array of IPv6 Elements
593 * \param[in] num_ip6_elems number of ip6_elems
594 * \returns 0 on success; negative in case of error */
595int ns2_tx_sns_del(struct gprs_ns2_vc *nsvc,
596 uint8_t trans_id,
597 const struct gprs_ns_ie_ip4_elem *ip4_elems,
598 unsigned int num_ip4_elems,
599 const struct gprs_ns_ie_ip6_elem *ip6_elems,
600 unsigned int num_ip6_elems)
601{
602 /* TODO: IP Address field */
603 return ns2_tx_sns_procedure(nsvc, SNS_PDUT_DELETE, trans_id, ip4_elems, num_ip4_elems, ip6_elems, num_ip6_elems);
604}
605
Alexander Couzens6a161492020-07-12 13:45:50 +0200606
607/*! Encode + Transmit a SNS-ACK as per Section 9.3.1.
608 * \param[in] nsvc NS-VC through which to transmit the ACK
609 * \param[in] trans_id Transaction ID which to acknowledge
610 * \param[in] cause Pointer to cause value (NULL if no cause to be sent)
611 * \param[in] ip4_elems Array of IPv4 Elements
612 * \param[in] num_ip4_elems number of ip4_elems
613 * \returns 0 on success; negative in case of error */
614int ns2_tx_sns_ack(struct gprs_ns2_vc *nsvc, uint8_t trans_id, uint8_t *cause,
615 const struct gprs_ns_ie_ip4_elem *ip4_elems,
616 unsigned int num_ip4_elems,
617 const struct gprs_ns_ie_ip6_elem *ip6_elems,
618 unsigned int num_ip6_elems)
619{
Daniel Willmann89a00f32021-01-17 14:20:28 +0100620 struct msgb *msg;
Alexander Couzens6a161492020-07-12 13:45:50 +0200621 struct gprs_ns_hdr *nsh;
622 uint16_t nsei;
623
624 if (!nsvc)
625 return -1;
626
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100627 msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200628
Daniel Willmann751977b2020-12-02 18:59:44 +0100629 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200630 log_set_context(LOG_CTX_GB_NSVC, nsvc);
631 if (!msg)
632 return -ENOMEM;
633
634 if (!nsvc->nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +0100635 LOGNSVC(nsvc, LOGL_ERROR, "Cannot transmit SNS on NSVC without SNS active\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200636 msgb_free(msg);
637 return -EIO;
638 }
639
Harald Welte22274df2021-03-04 07:59:16 +0100640
Alexander Couzens6a161492020-07-12 13:45:50 +0200641 nsei = osmo_htons(nsvc->nse->nsei);
642
643 msg->l2h = msgb_put(msg, sizeof(*nsh));
644 nsh = (struct gprs_ns_hdr *) msg->l2h;
645
646 nsh->pdu_type = SNS_PDUT_ACK;
647 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
648 msgb_v_put(msg, trans_id);
649 if (cause)
650 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause);
651 if (ip4_elems) {
652 /* List of IP4 Elements 10.3.2c */
653 msgb_tvlv_put(msg, NS_IE_IPv4_LIST,
654 num_ip4_elems*sizeof(struct gprs_ns_ie_ip4_elem),
655 (const uint8_t *)ip4_elems);
656 }
657 if (ip6_elems) {
658 /* List of IP6 elements 10.3.2d */
659 msgb_tvlv_put(msg, NS_IE_IPv6_LIST,
660 num_ip6_elems*sizeof(struct gprs_ns_ie_ip6_elem),
661 (const uint8_t *)ip6_elems);
662 }
663
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100664 LOG_NS_SIGNAL(nsvc, "Tx", nsh->pdu_type, LOGL_INFO,
665 " (trans_id=%u, cause=%s, num_ip4=%u, num_ip6=%u)\n",
666 trans_id, cause ? gprs_ns2_cause_str(*cause) : "NULL", num_ip4_elems, num_ip6_elems);
Harald Welte5a5bf722021-01-30 21:49:28 +0100667 return ns_vc_tx(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200668}
669
670/*! Encode + Transmit a SNS-CONFIG as per Section 9.3.4.
671 * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG
672 * \param[in] end_flag Whether or not this is the last SNS-CONFIG
673 * \param[in] ip4_elems Array of IPv4 Elements
674 * \param[in] num_ip4_elems number of ip4_elems
675 * \returns 0 on success; negative in case of error */
676int ns2_tx_sns_config(struct gprs_ns2_vc *nsvc, bool end_flag,
677 const struct gprs_ns_ie_ip4_elem *ip4_elems,
678 unsigned int num_ip4_elems,
679 const struct gprs_ns_ie_ip6_elem *ip6_elems,
680 unsigned int num_ip6_elems)
681{
682 struct msgb *msg;
683 struct gprs_ns_hdr *nsh;
684 uint16_t nsei;
685
686 if (!nsvc)
687 return -1;
688
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100689 msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200690
Daniel Willmann751977b2020-12-02 18:59:44 +0100691 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200692 log_set_context(LOG_CTX_GB_NSVC, nsvc);
693 if (!msg)
694 return -ENOMEM;
695
696 if (!nsvc->nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +0100697 LOGNSVC(nsvc, LOGL_ERROR, "Cannot transmit SNS on NSVC without SNS active\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200698 msgb_free(msg);
699 return -EIO;
700 }
701
702 nsei = osmo_htons(nsvc->nse->nsei);
703
704 msg->l2h = msgb_put(msg, sizeof(*nsh));
705 nsh = (struct gprs_ns_hdr *) msg->l2h;
706
707 nsh->pdu_type = SNS_PDUT_CONFIG;
708
709 msgb_v_put(msg, end_flag ? 0x01 : 0x00);
710 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
711
712 /* List of IP4 Elements 10.3.2c */
713 if (ip4_elems) {
714 msgb_tvlv_put(msg, NS_IE_IPv4_LIST, num_ip4_elems*sizeof(struct gprs_ns_ie_ip4_elem),
715 (const uint8_t *)ip4_elems);
716 } else if (ip6_elems) {
717 /* List of IP6 elements 10.3.2d */
718 msgb_tvlv_put(msg, NS_IE_IPv6_LIST, num_ip6_elems*sizeof(struct gprs_ns_ie_ip6_elem),
719 (const uint8_t *)ip6_elems);
720 }
721
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100722 LOG_NS_SIGNAL(nsvc, "Tx", nsh->pdu_type, LOGL_INFO,
723 " (end_flag=%u, num_ip4=%u, num_ip6=%u)\n",
724 end_flag, num_ip4_elems, num_ip6_elems);
Harald Welte5a5bf722021-01-30 21:49:28 +0100725 return ns_vc_tx(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200726}
727
728/*! Encode + Transmit a SNS-CONFIG-ACK as per Section 9.3.5.
729 * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG-ACK
730 * \param[in] cause Pointer to cause value (NULL if no cause to be sent)
731 * \returns 0 on success; negative in case of error */
732int ns2_tx_sns_config_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause)
733{
734 struct msgb *msg;
735 struct gprs_ns_hdr *nsh;
736 uint16_t nsei;
737
738 if (!nsvc)
739 return -1;
740
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100741 msg = ns2_msgb_alloc();
Daniel Willmann751977b2020-12-02 18:59:44 +0100742 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200743 log_set_context(LOG_CTX_GB_NSVC, nsvc);
744 if (!msg)
745 return -ENOMEM;
746
747 if (!nsvc->nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +0100748 LOGNSVC(nsvc, LOGL_ERROR, "Cannot transmit SNS on NSVC without SNS active\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200749 msgb_free(msg);
750 return -EIO;
751 }
752
753 nsei = osmo_htons(nsvc->nse->nsei);
754
755 msg->l2h = msgb_put(msg, sizeof(*nsh));
756 nsh = (struct gprs_ns_hdr *) msg->l2h;
757
758 nsh->pdu_type = SNS_PDUT_CONFIG_ACK;
759
760 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
761 if (cause)
762 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause);
763
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100764 LOGNSVC(nsvc, LOGL_INFO, "Tx SNS-CONFIG-ACK (cause=%s)\n",
765 cause ? gprs_ns2_cause_str(*cause) : "NULL");
766 LOG_NS_TX_SIGNAL(nsvc, nsh->pdu_type);
Harald Welte5a5bf722021-01-30 21:49:28 +0100767 return ns_vc_tx(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200768}
769
770
771/*! Encode + transmit a SNS-SIZE as per Section 9.3.7.
772 * \param[in] nsvc NS-VC through which to transmit the SNS-SIZE
773 * \param[in] reset_flag Whether or not to add a RESET flag
774 * \param[in] max_nr_nsvc Maximum number of NS-VCs
775 * \param[in] ip4_ep_nr Number of IPv4 endpoints (< 0 will omit the TLV)
776 * \param[in] ip6_ep_nr Number of IPv6 endpoints (< 0 will omit the TLV)
777 * \returns 0 on success; negative in case of error */
778int ns2_tx_sns_size(struct gprs_ns2_vc *nsvc, bool reset_flag, uint16_t max_nr_nsvc,
779 int ip4_ep_nr, int ip6_ep_nr)
780{
Daniel Willmann89a00f32021-01-17 14:20:28 +0100781 struct msgb *msg;
Alexander Couzens6a161492020-07-12 13:45:50 +0200782 struct gprs_ns_hdr *nsh;
783 uint16_t nsei;
784
785 if (!nsvc)
786 return -1;
787
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100788 msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200789
Daniel Willmann751977b2020-12-02 18:59:44 +0100790 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200791 log_set_context(LOG_CTX_GB_NSVC, nsvc);
792 if (!msg)
793 return -ENOMEM;
794
795 if (!nsvc->nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +0100796 LOGNSVC(nsvc, LOGL_ERROR, "Cannot transmit SNS on NSVC without SNS active\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200797 msgb_free(msg);
798 return -EIO;
799 }
800
801 nsei = osmo_htons(nsvc->nse->nsei);
802
803 msg->l2h = msgb_put(msg, sizeof(*nsh));
804 nsh = (struct gprs_ns_hdr *) msg->l2h;
805
806 nsh->pdu_type = SNS_PDUT_SIZE;
807
808 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
809 msgb_tv_put(msg, NS_IE_RESET_FLAG, reset_flag ? 0x01 : 0x00);
810 msgb_tv16_put(msg, NS_IE_MAX_NR_NSVC, max_nr_nsvc);
811 if (ip4_ep_nr >= 0)
812 msgb_tv16_put(msg, NS_IE_IPv4_EP_NR, ip4_ep_nr);
813 if (ip6_ep_nr >= 0)
814 msgb_tv16_put(msg, NS_IE_IPv6_EP_NR, ip6_ep_nr);
815
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100816 LOG_NS_SIGNAL(nsvc, "Tx", nsh->pdu_type, LOGL_INFO,
Alexander Couzens6ba77a32021-07-06 11:34:04 +0200817 " (reset=%u, max_nr_nsvc=%u, num_ip4=%d, num_ip6=%d)\n",
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100818 reset_flag, max_nr_nsvc, ip4_ep_nr, ip6_ep_nr);
Harald Welte5a5bf722021-01-30 21:49:28 +0100819 return ns_vc_tx(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200820}
821
822/*! Encode + Transmit a SNS-SIZE-ACK as per Section 9.3.8.
823 * \param[in] nsvc NS-VC through which to transmit the SNS-SIZE-ACK
824 * \param[in] cause Pointer to cause value (NULL if no cause to be sent)
825 * \returns 0 on success; negative in case of error */
826int ns2_tx_sns_size_ack(struct gprs_ns2_vc *nsvc, uint8_t *cause)
827{
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100828 struct msgb *msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200829 struct gprs_ns_hdr *nsh;
830 uint16_t nsei;
831
Daniel Willmann751977b2020-12-02 18:59:44 +0100832 log_set_context(LOG_CTX_GB_NSE, nsvc->nse);
Alexander Couzens6a161492020-07-12 13:45:50 +0200833 log_set_context(LOG_CTX_GB_NSVC, nsvc);
834 if (!msg)
835 return -ENOMEM;
836
837 if (!nsvc->nse->bss_sns_fi) {
Harald Weltef2949742021-01-20 14:54:14 +0100838 LOGNSVC(nsvc, LOGL_ERROR, "Cannot transmit SNS on NSVC without SNS active\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200839 msgb_free(msg);
840 return -EIO;
841 }
842
843 nsei = osmo_htons(nsvc->nse->nsei);
844
845 msg->l2h = msgb_put(msg, sizeof(*nsh));
846 nsh = (struct gprs_ns_hdr *) msg->l2h;
847
848 nsh->pdu_type = SNS_PDUT_SIZE_ACK;
849
850 msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei);
851 if (cause)
852 msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause);
853
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100854 LOG_NS_SIGNAL(nsvc, "Tx", nsh->pdu_type, LOGL_INFO, " cause=%s\n",
855 cause ? gprs_ns2_cause_str(*cause) : "NULL");
Harald Welte5a5bf722021-01-30 21:49:28 +0100856 return ns_vc_tx(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200857}