blob: a4ee92691a00de3156e5fee202299e82499979d8 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gprs_bssgp_util.c
2 * GPRS BSSGP protocol implementation as per 3GPP TS 08.18. */
3/*
4 * (C) 2009-2017 by Harald Welte <laforge@gnumonks.org>
Harald Welteaf086782010-05-11 10:01:17 +02005 *
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Welteaf086782010-05-11 10:01:17 +020010 * This program is free software; you can redistribute it and/or modify
Harald Welte7fa89c22014-10-26 20:33:09 +010011 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
Harald Welteaf086782010-05-11 10:01:17 +020013 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte7fa89c22014-10-26 20:33:09 +010018 * GNU General Public License for more details.
Harald Welteaf086782010-05-11 10:01:17 +020019 *
Harald Welte7fa89c22014-10-26 20:33:09 +010020 * You should have received a copy of the GNU General Public License
Harald Weltee4cbb3f2011-01-01 15:25:50 +010021 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welteaf086782010-05-11 10:01:17 +020022 *
23 */
24
25#include <errno.h>
26#include <stdint.h>
27
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010028#include <osmocom/core/msgb.h>
Harald Weltebfe62e52017-05-15 12:48:30 +020029#include <osmocom/core/byteswap.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +010030#include <osmocom/gsm/tlv.h>
31#include <osmocom/core/talloc.h>
Harald Welte73952e32012-06-16 14:59:56 +080032#include <osmocom/gprs/gprs_bssgp.h>
33#include <osmocom/gprs/gprs_ns.h>
Harald Welteaf086782010-05-11 10:01:17 +020034
Alexander Couzens85a8fd32020-07-18 15:57:07 +020035#include "gprs_bssgp_internal.h"
Harald Weltecca49632012-06-16 17:45:59 +080036#include "common_vty.h"
Harald Welteaf086782010-05-11 10:01:17 +020037
38struct gprs_ns_inst *bssgp_nsi;
39
40/* BSSGP Protocol specific, not implementation specific */
41/* FIXME: This needs to go into libosmocore after finished */
42
43/* Chapter 11.3.9 / Table 11.10: Cause coding */
44static const struct value_string bssgp_cause_strings[] = {
Max4aaa8ab2017-06-29 19:27:24 +020045 { BSSGP_CAUSE_PROC_OVERLOAD, "Processor overload" },
46 { BSSGP_CAUSE_EQUIP_FAIL, "Equipment Failure" },
Pau Espin Pedrol9b90ca82020-01-03 17:45:36 +010047 { BSSGP_CAUSE_TRASIT_NET_FAIL, "Transit network service failure" },
Max4aaa8ab2017-06-29 19:27:24 +020048 { BSSGP_CAUSE_CAPA_GREATER_0KPBS, "Transmission capacity modified" },
49 { BSSGP_CAUSE_UNKNOWN_MS, "Unknown MS" },
50 { BSSGP_CAUSE_UNKNOWN_BVCI, "Unknown BVCI" },
51 { BSSGP_CAUSE_CELL_TRAF_CONG, "Cell traffic congestion" },
52 { BSSGP_CAUSE_SGSN_CONG, "SGSN congestion" },
53 { BSSGP_CAUSE_OML_INTERV, "O&M intervention" },
54 { BSSGP_CAUSE_BVCI_BLOCKED, "BVCI blocked" },
55 { BSSGP_CAUSE_PFC_CREATE_FAIL, "PFC create failure" },
56 { BSSGP_CAUSE_PFC_PREEMPTED, "PFC preempted" },
57 { BSSGP_CAUSE_ABQP_NOT_SUPP, "ABQP no more supported" },
58 { BSSGP_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" },
59 { BSSGP_CAUSE_INV_MAND_INF, "Invalid mandatory information" },
60 { BSSGP_CAUSE_MISSING_MAND_IE, "Missing mandatory IE" },
61 { BSSGP_CAUSE_MISSING_COND_IE, "Missing conditional IE" },
62 { BSSGP_CAUSE_UNEXP_COND_IE, "Unexpected conditional IE" },
63 { BSSGP_CAUSE_COND_IE_ERR, "Conditional IE error" },
64 { BSSGP_CAUSE_PDU_INCOMP_STATE, "PDU incompatible with protocol state" },
65 { BSSGP_CAUSE_PROTO_ERR_UNSPEC, "Protocol error - unspecified" },
66 { BSSGP_CAUSE_PDU_INCOMP_FEAT, "PDU not compatible with feature set" },
67 { BSSGP_CAUSE_REQ_INFO_NOT_AVAIL, "Requested Information not available" },
68 { BSSGP_CAUSE_UNKN_DST , "Unknown Destination address" },
69 { BSSGP_CAUSE_UNKN_RIM_AI, "Unknown RIM Application Identity or RIM application disabled" },
70 { BSSGP_CAUSE_INVAL_CONT_UI, "Invalid Container Unit Information" },
71 { BSSGP_CAUSE_PFC_QUEUE, "PFC queuing" },
72 { BSSGP_CAUSE_PFC_CREATED, "PFC created successfully" },
73 { BSSGP_CAUSE_T12_EXPIRY, "T12 expiry" },
74 { BSSGP_CAUSE_MS_UNDER_PS_HO, "MS under PS Handover treatment" },
75 { BSSGP_CAUSE_UL_QUALITY, "Uplink quality" },
76 { BSSGP_CAUSE_UL_STRENGTH, "Uplink strength" },
77 { BSSGP_CAUSE_DL_QUALITY, "Downlink quality" },
78 { BSSGP_CAUSE_DL_STRENGTH, "Downlink strength" },
79 { BSSGP_CAUSE_DISTANCE, "Distance" },
80 { BSSGP_CAUSE_BETTER_CELL, "Better cell" },
81 { BSSGP_CAUSE_TRAFFIC, "Traffic" },
82 { BSSGP_CAUSE_MS_RADIO_LOSS, "Radio contact lost with MS" },
83 { BSSGP_CAUSE_MS_BACK_OLD_CHAN, "MS back on old channel" },
84 { BSSGP_CAUSE_T13_EXPIRY, "T13 expiry" },
85 { BSSGP_CAUSE_T14_EXPIRY, "T14 expiry" },
86 { BSSGP_CAUSE_NOT_ALL_PFC, "Not all requested PFCs created" },
87 { BSSGP_CAUSE_CS, "CS cause" },
88 { BSSGP_CAUSE_REQ_ALG_NOT_SUPP, "Requested ciphering and/or integrity protection algorithms not supported" },
89 { BSSGP_CAUSE_RELOC_FAIL, "Relocation failure in target system" },
90 { BSSGP_CAUSE_DIR_RETRY , "Directed Retry" },
91 { BSSGP_CAUSE_TIME_CRIT_RELOC , "Time critical relocation" },
92 { BSSGP_CAUSE_PS_HO_TARG_NA , "PS Handover Target not allowed" },
93 { BSSGP_CAUSE_PS_HO_TARG_NOT_SUPP, "PS Handover not Supported in Target BSS or Target System" },
94 { BSSGP_CAUSE_PUESBINE, "Incoming relocation not supported due to PUESBINE feature" },
95 { BSSGP_CAUSE_DTM_HO_NO_CS_RES, "DTM Handover - No CS resource" },
96 { BSSGP_CAUSE_DTM_HO_PS_ALLOC_FAIL, "DTM Handover - PS Allocation failure" },
97 { BSSGP_CAUSE_DTM_HO_T24_EXPIRY, "DTM Handover - T24 expiry" },
98 { BSSGP_CAUSE_DTM_HO_INVAL_CS_IND, "DTM Handover - Invalid CS Indication IE" },
99 { BSSGP_CAUSE_DTM_HO_T23_EXPIRY, "DTM Handover - T23 expiry" },
100 { BSSGP_CAUSE_DTM_HO_MSC_ERR, "DTM Handover - MSC Error" },
101 { BSSGP_CAUSE_INVAL_CSG_CELL, "Invalid CSG cell" },
Harald Welteaf086782010-05-11 10:01:17 +0200102 { 0, NULL },
103};
104
Maxc0d9a6c2016-03-09 12:29:23 +0100105static const struct value_string bssgp_pdu_strings[] = {
106 { BSSGP_PDUT_DL_UNITDATA, "DL-UNITDATA" },
107 { BSSGP_PDUT_UL_UNITDATA, "UL-UNITDATA" },
108 { BSSGP_PDUT_RA_CAPABILITY, "RA-CAPABILITY" },
109 { BSSGP_PDUT_PTM_UNITDATA, "PTM-UNITDATA" },
Harald Welte61e9de82020-12-04 12:54:40 +0100110 { BSSGP_PDUT_DL_MMBS_UNITDATA, "DL-MBMS-UNITDATA" },
111 { BSSGP_PDUT_UL_MMBS_UNITDATA, "UL-MBMS-UNITDATA" },
Maxc0d9a6c2016-03-09 12:29:23 +0100112 { BSSGP_PDUT_PAGING_PS, "PAGING-PS" },
113 { BSSGP_PDUT_PAGING_CS, "PAGING-CS" },
114 { BSSGP_PDUT_RA_CAPA_UDPATE, "RA-CAPABILITY-UPDATE" },
115 { BSSGP_PDUT_RA_CAPA_UPDATE_ACK, "RA-CAPABILITY-UPDATE-ACK" },
116 { BSSGP_PDUT_RADIO_STATUS, "RADIO-STATUS" },
117 { BSSGP_PDUT_SUSPEND, "SUSPEND" },
118 { BSSGP_PDUT_SUSPEND_ACK, "SUSPEND-ACK" },
119 { BSSGP_PDUT_SUSPEND_NACK, "SUSPEND-NACK" },
120 { BSSGP_PDUT_RESUME, "RESUME" },
121 { BSSGP_PDUT_RESUME_ACK, "RESUME-ACK" },
122 { BSSGP_PDUT_RESUME_NACK, "RESUME-NACK" },
Harald Welte61e9de82020-12-04 12:54:40 +0100123 { BSSGP_PDUT_DUMMY_PAGING_PS, "DUMMY-PAGING-PS" },
124 { BSSGP_PDUT_DUMMY_PAGING_PS_RESP, "DUMMY-PAGING-PS-RESP" },
125 { BSSGP_PDUT_MS_REGISTR_ENQ, "MS-REGISTRATION-ENQ" },
126 { BSSGP_PDUT_MS_REGISTR_ENQ_RESP, "MS-REGISTRATION-ENQ-RESP" },
Maxc0d9a6c2016-03-09 12:29:23 +0100127 { BSSGP_PDUT_BVC_BLOCK, "BVC-BLOCK" },
128 { BSSGP_PDUT_BVC_BLOCK_ACK, "BVC-BLOCK-ACK" },
129 { BSSGP_PDUT_BVC_RESET, "BVC-RESET" },
130 { BSSGP_PDUT_BVC_RESET_ACK, "BVC-RESET-ACK" },
131 { BSSGP_PDUT_BVC_UNBLOCK, "BVC-UNBLOCK" },
132 { BSSGP_PDUT_BVC_UNBLOCK_ACK, "BVC-UNBLOCK-ACK" },
133 { BSSGP_PDUT_FLOW_CONTROL_BVC, "FLOW-CONTROL-BVC" },
134 { BSSGP_PDUT_FLOW_CONTROL_BVC_ACK, "FLOW-CONTROL-BVC-ACK" },
135 { BSSGP_PDUT_FLOW_CONTROL_MS, "FLOW-CONTROL-MS" },
136 { BSSGP_PDUT_FLOW_CONTROL_MS_ACK, "FLOW-CONTROL-MS-ACK" },
137 { BSSGP_PDUT_FLUSH_LL, "FLUSH-LL" },
138 { BSSGP_PDUT_FLUSH_LL_ACK, "FLUSH-LL-ACK" },
139 { BSSGP_PDUT_LLC_DISCARD, "LLC DISCARDED" },
Harald Welte61e9de82020-12-04 12:54:40 +0100140 { BSSGP_PDUT_FLOW_CONTROL_PFC, "FLOW-CONTROL-PFC" },
141 { BSSGP_PDUT_FLOW_CONTROL_PFC_ACK, "FLOW-CONTROL-PFC-ACK" },
Maxc0d9a6c2016-03-09 12:29:23 +0100142 { BSSGP_PDUT_SGSN_INVOKE_TRACE, "SGSN-INVOKE-TRACE" },
143 { BSSGP_PDUT_STATUS, "STATUS" },
Harald Welte61e9de82020-12-04 12:54:40 +0100144 { BSSGP_PDUT_OVERLOAD, "OVERLOAD" },
Maxc0d9a6c2016-03-09 12:29:23 +0100145 { BSSGP_PDUT_DOWNLOAD_BSS_PFC, "DOWNLOAD-BSS-PFC" },
146 { BSSGP_PDUT_CREATE_BSS_PFC, "CREATE-BSS-PFC" },
147 { BSSGP_PDUT_CREATE_BSS_PFC_ACK, "CREATE-BSS-PFC-ACK" },
148 { BSSGP_PDUT_CREATE_BSS_PFC_NACK, "CREATE-BSS-PFC-NACK" },
149 { BSSGP_PDUT_MODIFY_BSS_PFC, "MODIFY-BSS-PFC" },
150 { BSSGP_PDUT_MODIFY_BSS_PFC_ACK, "MODIFY-BSS-PFC-ACK" },
151 { BSSGP_PDUT_DELETE_BSS_PFC, "DELETE-BSS-PFC" },
152 { BSSGP_PDUT_DELETE_BSS_PFC_ACK, "DELETE-BSS-PFC-ACK" },
Harald Welte61e9de82020-12-04 12:54:40 +0100153 { BSSGP_PDUT_DELETE_BSS_PFC_REQ, "DELETE-BSS-PFC-REQ" },
154 { BSSGP_PDUT_PS_HO_REQUIRED, "PS-HO-REQUIRED" },
155 { BSSGP_PDUT_PS_HO_REQUIRED_ACK, "PS-HO-REQUIRED-ACK" },
156 { BSSGP_PDUT_PS_HO_REQUIRED_NACK, "PS-HO-REQUIRED-NACK" },
157 { BSSGP_PDUT_PS_HO_REQUEST, "PS-HO-REQUEST" },
158 { BSSGP_PDUT_PS_HO_REQUEST_ACK, "PS-HO-REQUEST-ACK" },
159 { BSSGP_PDUT_PS_HO_REQUEST_NACK, "PS-HO-REQUEST-NACK" },
160 { BSSGP_PDUT_PS_HO_COMPLETE, "PS-HO-COMPLETE" },
161 { BSSGP_PDUT_PS_HO_CANCEL, "PS-HO-CANCEL" },
162 { BSSGP_PDUT_PS_HO_COMPLETE_ACK, "PS-HO-COMPLETE-ACK" },
Philipp Maierb73a3de2020-12-07 20:53:26 +0100163 { BSSGP_PDUT_PERFORM_LOC_REQ, "PERFORM-LOC-REQ" },
164 { BSSGP_PDUT_PERFORM_LOC_RESP, "PERFORM-LOC-RESP" },
165 { BSSGP_PDUT_PERFORM_LOC_ABORT, "PERFORM-LOC-ABORT" },
166 { BSSGP_PDUT_POSITION_COMMAND, "POSITION-COMMAND" },
167 { BSSGP_PDUT_POSITION_RESPONSE, "POSITION-RESPONSE" },
168 { BSSGP_PDUT_RAN_INFO, "RAN-INFO" },
169 { BSSGP_PDUT_RAN_INFO_REQ, "RAN-INFO-REQ" },
170 { BSSGP_PDUT_RAN_INFO_ACK, "RAN-INFO-ACK" },
171 { BSSGP_PDUT_RAN_INFO_ERROR, "RAN-INFO-ERROR" },
172 { BSSGP_PDUT_RAN_INFO_APP_ERROR, "RAN-INFO-APP-ERROR" },
173 { BSSGP_PDUT_MBMS_START_REQ, "MBMS-START-REQ" },
174 { BSSGP_PDUT_MBMS_START_RESP, "MBMS-START-RESP" },
175 { BSSGP_PDUT_MBMS_STOP_REQ, "MBMS-STOP-REQ" },
176 { BSSGP_PDUT_MBMS_STOP_RESP, "MBMS-STOP-RESP" },
177 { BSSGP_PDUT_MBMS_UPDATE_REQ, "MBMS-UPDATE-REQ" },
178 { BSSGP_PDUT_MBMS_UPDATE_RESP, "MBMS-UPDATE-RESP" },
Maxc0d9a6c2016-03-09 12:29:23 +0100179 { 0, NULL },
180};
181
Harald Welteaf086782010-05-11 10:01:17 +0200182const char *bssgp_cause_str(enum gprs_bssgp_cause cause)
183{
184 return get_value_string(bssgp_cause_strings, cause);
185}
186
Maxc0d9a6c2016-03-09 12:29:23 +0100187const char *bssgp_pdu_str(enum bssgp_pdu_type pdu)
188{
189 return get_value_string(bssgp_pdu_strings, pdu);
190}
Harald Welteaf086782010-05-11 10:01:17 +0200191
192struct msgb *bssgp_msgb_alloc(void)
193{
Jacob Erlbecka39e2de2014-09-23 13:28:24 +0200194 struct msgb *msg = msgb_alloc_headroom(4096, 128, "BSSGP");
Jacob Erlbecka84db612015-04-09 14:22:22 +0200195
196 /* TODO: Add handling of msg == NULL to this function and to all callers */
197 OSMO_ASSERT(msg != NULL);
198
Jacob Erlbecka39e2de2014-09-23 13:28:24 +0200199 msgb_bssgph(msg) = msg->data;
200 return msg;
Harald Welteaf086782010-05-11 10:01:17 +0200201}
202
Jacob Erlbeckf78ec5c2015-11-17 09:53:23 +0100203struct msgb *bssgp_msgb_copy(const struct msgb *msg, const char *name)
204{
205 struct libgb_msgb_cb *old_cb, *new_cb;
206 struct msgb *new_msg;
207
208 new_msg = msgb_copy(msg, name);
209 if (!new_msg)
210 return NULL;
211
212 /* copy GB specific data */
213 old_cb = LIBGB_MSGB_CB(msg);
214 new_cb = LIBGB_MSGB_CB(new_msg);
215
216 if (old_cb->bssgph)
217 new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data);
218 if (old_cb->llch)
219 new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data);
220
221 /* bssgp_cell_id is a pointer into the old msgb, so we need to make
222 * it a pointer into the new msgb */
223 if (old_cb->bssgp_cell_id)
224 new_cb->bssgp_cell_id = new_msg->_data +
225 (old_cb->bssgp_cell_id - msg->_data);
226 new_cb->nsei = old_cb->nsei;
227 new_cb->bvci = old_cb->bvci;
228 new_cb->tlli = old_cb->tlli;
229
230 return new_msg;
231}
232
Harald Welteaf086782010-05-11 10:01:17 +0200233/* Transmit a simple response such as BLOCK/UNBLOCK/RESET ACK/NACK */
234int bssgp_tx_simple_bvci(uint8_t pdu_type, uint16_t nsei,
235 uint16_t bvci, uint16_t ns_bvci)
236{
237 struct msgb *msg = bssgp_msgb_alloc();
238 struct bssgp_normal_hdr *bgph =
239 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
240 uint16_t _bvci;
241
242 msgb_nsei(msg) = nsei;
243 msgb_bvci(msg) = ns_bvci;
244
245 bgph->pdu_type = pdu_type;
Harald Weltebfe62e52017-05-15 12:48:30 +0200246 _bvci = osmo_htons(bvci);
Harald Welteaf086782010-05-11 10:01:17 +0200247 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
248
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200249 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welteaf086782010-05-11 10:01:17 +0200250}
251
252/* Chapter 10.4.14: Status */
253int bssgp_tx_status(uint8_t cause, uint16_t *bvci, struct msgb *orig_msg)
254{
255 struct msgb *msg = bssgp_msgb_alloc();
256 struct bssgp_normal_hdr *bgph =
257 (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph));
258
Jacob Erlbecka7165772014-09-23 13:28:21 +0200259 /* GSM 08.18, 10.4.14.1: The BVCI must be included if (and only if) the
260 cause is either "BVCI blocked" or "BVCI unknown" */
261 if (cause == BSSGP_CAUSE_UNKNOWN_BVCI || cause == BSSGP_CAUSE_BVCI_BLOCKED) {
262 if (bvci == NULL)
263 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Tx STATUS, cause=%s: "
264 "missing conditional BVCI\n",
265 bssgp_cause_str(cause));
266 } else {
267 if (bvci != NULL)
268 LOGP(DBSSGP, LOGL_ERROR, "BSSGP Tx STATUS, cause=%s: "
269 "unexpected conditional BVCI\n",
270 bssgp_cause_str(cause));
271 }
272
Holger Hans Peter Freyther8e2e78e2010-06-08 16:05:20 +0800273 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Tx STATUS, cause=%s\n",
Harald Weltecfb545a2010-06-03 21:30:57 +0200274 bvci ? *bvci : 0, bssgp_cause_str(cause));
Harald Welteaf086782010-05-11 10:01:17 +0200275 msgb_nsei(msg) = msgb_nsei(orig_msg);
276 msgb_bvci(msg) = 0;
277
278 bgph->pdu_type = BSSGP_PDUT_STATUS;
279 msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause);
280 if (bvci) {
Harald Weltebfe62e52017-05-15 12:48:30 +0200281 uint16_t _bvci = osmo_htons(*bvci);
Harald Welteaf086782010-05-11 10:01:17 +0200282 msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci);
283 }
Harald Welted83d1b62011-07-16 13:45:10 +0200284 msgb_tvlv_put(msg, BSSGP_IE_PDU_IN_ERROR,
285 msgb_bssgp_len(orig_msg), msgb_bssgph(orig_msg));
Harald Welteaf086782010-05-11 10:01:17 +0200286
Alexander Couzens85a8fd32020-07-18 15:57:07 +0200287 return bssgp_ns_send(bssgp_ns_send_data, msg);
Harald Welteaf086782010-05-11 10:01:17 +0200288}