Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \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 Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 11 | * 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 Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 13 | * (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 Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 18 | * GNU General Public License for more details. |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 19 | * |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 20 | * You should have received a copy of the GNU General Public License |
Harald Welte | e4cbb3f | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <errno.h> |
| 26 | #include <stdint.h> |
| 27 | |
Pablo Neira Ayuso | ff66323 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 28 | #include <osmocom/core/msgb.h> |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 29 | #include <osmocom/core/byteswap.h> |
Pablo Neira Ayuso | ff66323 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 30 | #include <osmocom/gsm/tlv.h> |
| 31 | #include <osmocom/core/talloc.h> |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 32 | #include <osmocom/gprs/gprs_bssgp.h> |
| 33 | #include <osmocom/gprs/gprs_ns.h> |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 34 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 35 | #include "common_vty.h" |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 36 | |
| 37 | struct gprs_ns_inst *bssgp_nsi; |
| 38 | |
| 39 | /* BSSGP Protocol specific, not implementation specific */ |
| 40 | /* FIXME: This needs to go into libosmocore after finished */ |
| 41 | |
| 42 | /* Chapter 11.3.9 / Table 11.10: Cause coding */ |
| 43 | static const struct value_string bssgp_cause_strings[] = { |
Max | 4aaa8ab | 2017-06-29 19:27:24 +0200 | [diff] [blame] | 44 | { BSSGP_CAUSE_PROC_OVERLOAD, "Processor overload" }, |
| 45 | { BSSGP_CAUSE_EQUIP_FAIL, "Equipment Failure" }, |
| 46 | { BSSGP_CAUSE_TRASIT_NET_FAIL, "Transit netowkr service failure" }, |
| 47 | { BSSGP_CAUSE_CAPA_GREATER_0KPBS, "Transmission capacity modified" }, |
| 48 | { BSSGP_CAUSE_UNKNOWN_MS, "Unknown MS" }, |
| 49 | { BSSGP_CAUSE_UNKNOWN_BVCI, "Unknown BVCI" }, |
| 50 | { BSSGP_CAUSE_CELL_TRAF_CONG, "Cell traffic congestion" }, |
| 51 | { BSSGP_CAUSE_SGSN_CONG, "SGSN congestion" }, |
| 52 | { BSSGP_CAUSE_OML_INTERV, "O&M intervention" }, |
| 53 | { BSSGP_CAUSE_BVCI_BLOCKED, "BVCI blocked" }, |
| 54 | { BSSGP_CAUSE_PFC_CREATE_FAIL, "PFC create failure" }, |
| 55 | { BSSGP_CAUSE_PFC_PREEMPTED, "PFC preempted" }, |
| 56 | { BSSGP_CAUSE_ABQP_NOT_SUPP, "ABQP no more supported" }, |
| 57 | { BSSGP_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 58 | { BSSGP_CAUSE_INV_MAND_INF, "Invalid mandatory information" }, |
| 59 | { BSSGP_CAUSE_MISSING_MAND_IE, "Missing mandatory IE" }, |
| 60 | { BSSGP_CAUSE_MISSING_COND_IE, "Missing conditional IE" }, |
| 61 | { BSSGP_CAUSE_UNEXP_COND_IE, "Unexpected conditional IE" }, |
| 62 | { BSSGP_CAUSE_COND_IE_ERR, "Conditional IE error" }, |
| 63 | { BSSGP_CAUSE_PDU_INCOMP_STATE, "PDU incompatible with protocol state" }, |
| 64 | { BSSGP_CAUSE_PROTO_ERR_UNSPEC, "Protocol error - unspecified" }, |
| 65 | { BSSGP_CAUSE_PDU_INCOMP_FEAT, "PDU not compatible with feature set" }, |
| 66 | { BSSGP_CAUSE_REQ_INFO_NOT_AVAIL, "Requested Information not available" }, |
| 67 | { BSSGP_CAUSE_UNKN_DST , "Unknown Destination address" }, |
| 68 | { BSSGP_CAUSE_UNKN_RIM_AI, "Unknown RIM Application Identity or RIM application disabled" }, |
| 69 | { BSSGP_CAUSE_INVAL_CONT_UI, "Invalid Container Unit Information" }, |
| 70 | { BSSGP_CAUSE_PFC_QUEUE, "PFC queuing" }, |
| 71 | { BSSGP_CAUSE_PFC_CREATED, "PFC created successfully" }, |
| 72 | { BSSGP_CAUSE_T12_EXPIRY, "T12 expiry" }, |
| 73 | { BSSGP_CAUSE_MS_UNDER_PS_HO, "MS under PS Handover treatment" }, |
| 74 | { BSSGP_CAUSE_UL_QUALITY, "Uplink quality" }, |
| 75 | { BSSGP_CAUSE_UL_STRENGTH, "Uplink strength" }, |
| 76 | { BSSGP_CAUSE_DL_QUALITY, "Downlink quality" }, |
| 77 | { BSSGP_CAUSE_DL_STRENGTH, "Downlink strength" }, |
| 78 | { BSSGP_CAUSE_DISTANCE, "Distance" }, |
| 79 | { BSSGP_CAUSE_BETTER_CELL, "Better cell" }, |
| 80 | { BSSGP_CAUSE_TRAFFIC, "Traffic" }, |
| 81 | { BSSGP_CAUSE_MS_RADIO_LOSS, "Radio contact lost with MS" }, |
| 82 | { BSSGP_CAUSE_MS_BACK_OLD_CHAN, "MS back on old channel" }, |
| 83 | { BSSGP_CAUSE_T13_EXPIRY, "T13 expiry" }, |
| 84 | { BSSGP_CAUSE_T14_EXPIRY, "T14 expiry" }, |
| 85 | { BSSGP_CAUSE_NOT_ALL_PFC, "Not all requested PFCs created" }, |
| 86 | { BSSGP_CAUSE_CS, "CS cause" }, |
| 87 | { BSSGP_CAUSE_REQ_ALG_NOT_SUPP, "Requested ciphering and/or integrity protection algorithms not supported" }, |
| 88 | { BSSGP_CAUSE_RELOC_FAIL, "Relocation failure in target system" }, |
| 89 | { BSSGP_CAUSE_DIR_RETRY , "Directed Retry" }, |
| 90 | { BSSGP_CAUSE_TIME_CRIT_RELOC , "Time critical relocation" }, |
| 91 | { BSSGP_CAUSE_PS_HO_TARG_NA , "PS Handover Target not allowed" }, |
| 92 | { BSSGP_CAUSE_PS_HO_TARG_NOT_SUPP, "PS Handover not Supported in Target BSS or Target System" }, |
| 93 | { BSSGP_CAUSE_PUESBINE, "Incoming relocation not supported due to PUESBINE feature" }, |
| 94 | { BSSGP_CAUSE_DTM_HO_NO_CS_RES, "DTM Handover - No CS resource" }, |
| 95 | { BSSGP_CAUSE_DTM_HO_PS_ALLOC_FAIL, "DTM Handover - PS Allocation failure" }, |
| 96 | { BSSGP_CAUSE_DTM_HO_T24_EXPIRY, "DTM Handover - T24 expiry" }, |
| 97 | { BSSGP_CAUSE_DTM_HO_INVAL_CS_IND, "DTM Handover - Invalid CS Indication IE" }, |
| 98 | { BSSGP_CAUSE_DTM_HO_T23_EXPIRY, "DTM Handover - T23 expiry" }, |
| 99 | { BSSGP_CAUSE_DTM_HO_MSC_ERR, "DTM Handover - MSC Error" }, |
| 100 | { BSSGP_CAUSE_INVAL_CSG_CELL, "Invalid CSG cell" }, |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 101 | { 0, NULL }, |
| 102 | }; |
| 103 | |
Max | c0d9a6c | 2016-03-09 12:29:23 +0100 | [diff] [blame] | 104 | static const struct value_string bssgp_pdu_strings[] = { |
| 105 | { BSSGP_PDUT_DL_UNITDATA, "DL-UNITDATA" }, |
| 106 | { BSSGP_PDUT_UL_UNITDATA, "UL-UNITDATA" }, |
| 107 | { BSSGP_PDUT_RA_CAPABILITY, "RA-CAPABILITY" }, |
| 108 | { BSSGP_PDUT_PTM_UNITDATA, "PTM-UNITDATA" }, |
| 109 | { BSSGP_PDUT_PAGING_PS, "PAGING-PS" }, |
| 110 | { BSSGP_PDUT_PAGING_CS, "PAGING-CS" }, |
| 111 | { BSSGP_PDUT_RA_CAPA_UDPATE, "RA-CAPABILITY-UPDATE" }, |
| 112 | { BSSGP_PDUT_RA_CAPA_UPDATE_ACK, "RA-CAPABILITY-UPDATE-ACK" }, |
| 113 | { BSSGP_PDUT_RADIO_STATUS, "RADIO-STATUS" }, |
| 114 | { BSSGP_PDUT_SUSPEND, "SUSPEND" }, |
| 115 | { BSSGP_PDUT_SUSPEND_ACK, "SUSPEND-ACK" }, |
| 116 | { BSSGP_PDUT_SUSPEND_NACK, "SUSPEND-NACK" }, |
| 117 | { BSSGP_PDUT_RESUME, "RESUME" }, |
| 118 | { BSSGP_PDUT_RESUME_ACK, "RESUME-ACK" }, |
| 119 | { BSSGP_PDUT_RESUME_NACK, "RESUME-NACK" }, |
| 120 | { BSSGP_PDUT_BVC_BLOCK, "BVC-BLOCK" }, |
| 121 | { BSSGP_PDUT_BVC_BLOCK_ACK, "BVC-BLOCK-ACK" }, |
| 122 | { BSSGP_PDUT_BVC_RESET, "BVC-RESET" }, |
| 123 | { BSSGP_PDUT_BVC_RESET_ACK, "BVC-RESET-ACK" }, |
| 124 | { BSSGP_PDUT_BVC_UNBLOCK, "BVC-UNBLOCK" }, |
| 125 | { BSSGP_PDUT_BVC_UNBLOCK_ACK, "BVC-UNBLOCK-ACK" }, |
| 126 | { BSSGP_PDUT_FLOW_CONTROL_BVC, "FLOW-CONTROL-BVC" }, |
| 127 | { BSSGP_PDUT_FLOW_CONTROL_BVC_ACK, "FLOW-CONTROL-BVC-ACK" }, |
| 128 | { BSSGP_PDUT_FLOW_CONTROL_MS, "FLOW-CONTROL-MS" }, |
| 129 | { BSSGP_PDUT_FLOW_CONTROL_MS_ACK, "FLOW-CONTROL-MS-ACK" }, |
| 130 | { BSSGP_PDUT_FLUSH_LL, "FLUSH-LL" }, |
| 131 | { BSSGP_PDUT_FLUSH_LL_ACK, "FLUSH-LL-ACK" }, |
| 132 | { BSSGP_PDUT_LLC_DISCARD, "LLC DISCARDED" }, |
| 133 | { BSSGP_PDUT_SGSN_INVOKE_TRACE, "SGSN-INVOKE-TRACE" }, |
| 134 | { BSSGP_PDUT_STATUS, "STATUS" }, |
| 135 | { BSSGP_PDUT_DOWNLOAD_BSS_PFC, "DOWNLOAD-BSS-PFC" }, |
| 136 | { BSSGP_PDUT_CREATE_BSS_PFC, "CREATE-BSS-PFC" }, |
| 137 | { BSSGP_PDUT_CREATE_BSS_PFC_ACK, "CREATE-BSS-PFC-ACK" }, |
| 138 | { BSSGP_PDUT_CREATE_BSS_PFC_NACK, "CREATE-BSS-PFC-NACK" }, |
| 139 | { BSSGP_PDUT_MODIFY_BSS_PFC, "MODIFY-BSS-PFC" }, |
| 140 | { BSSGP_PDUT_MODIFY_BSS_PFC_ACK, "MODIFY-BSS-PFC-ACK" }, |
| 141 | { BSSGP_PDUT_DELETE_BSS_PFC, "DELETE-BSS-PFC" }, |
| 142 | { BSSGP_PDUT_DELETE_BSS_PFC_ACK, "DELETE-BSS-PFC-ACK" }, |
| 143 | { 0, NULL }, |
| 144 | }; |
| 145 | |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 146 | const char *bssgp_cause_str(enum gprs_bssgp_cause cause) |
| 147 | { |
| 148 | return get_value_string(bssgp_cause_strings, cause); |
| 149 | } |
| 150 | |
Max | c0d9a6c | 2016-03-09 12:29:23 +0100 | [diff] [blame] | 151 | const char *bssgp_pdu_str(enum bssgp_pdu_type pdu) |
| 152 | { |
| 153 | return get_value_string(bssgp_pdu_strings, pdu); |
| 154 | } |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 155 | |
| 156 | struct msgb *bssgp_msgb_alloc(void) |
| 157 | { |
Jacob Erlbeck | a39e2de | 2014-09-23 13:28:24 +0200 | [diff] [blame] | 158 | struct msgb *msg = msgb_alloc_headroom(4096, 128, "BSSGP"); |
Jacob Erlbeck | a84db61 | 2015-04-09 14:22:22 +0200 | [diff] [blame] | 159 | |
| 160 | /* TODO: Add handling of msg == NULL to this function and to all callers */ |
| 161 | OSMO_ASSERT(msg != NULL); |
| 162 | |
Jacob Erlbeck | a39e2de | 2014-09-23 13:28:24 +0200 | [diff] [blame] | 163 | msgb_bssgph(msg) = msg->data; |
| 164 | return msg; |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Jacob Erlbeck | f78ec5c | 2015-11-17 09:53:23 +0100 | [diff] [blame] | 167 | struct msgb *bssgp_msgb_copy(const struct msgb *msg, const char *name) |
| 168 | { |
| 169 | struct libgb_msgb_cb *old_cb, *new_cb; |
| 170 | struct msgb *new_msg; |
| 171 | |
| 172 | new_msg = msgb_copy(msg, name); |
| 173 | if (!new_msg) |
| 174 | return NULL; |
| 175 | |
| 176 | /* copy GB specific data */ |
| 177 | old_cb = LIBGB_MSGB_CB(msg); |
| 178 | new_cb = LIBGB_MSGB_CB(new_msg); |
| 179 | |
| 180 | if (old_cb->bssgph) |
| 181 | new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data); |
| 182 | if (old_cb->llch) |
| 183 | new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data); |
| 184 | |
| 185 | /* bssgp_cell_id is a pointer into the old msgb, so we need to make |
| 186 | * it a pointer into the new msgb */ |
| 187 | if (old_cb->bssgp_cell_id) |
| 188 | new_cb->bssgp_cell_id = new_msg->_data + |
| 189 | (old_cb->bssgp_cell_id - msg->_data); |
| 190 | new_cb->nsei = old_cb->nsei; |
| 191 | new_cb->bvci = old_cb->bvci; |
| 192 | new_cb->tlli = old_cb->tlli; |
| 193 | |
| 194 | return new_msg; |
| 195 | } |
| 196 | |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 197 | /* Transmit a simple response such as BLOCK/UNBLOCK/RESET ACK/NACK */ |
| 198 | int bssgp_tx_simple_bvci(uint8_t pdu_type, uint16_t nsei, |
| 199 | uint16_t bvci, uint16_t ns_bvci) |
| 200 | { |
| 201 | struct msgb *msg = bssgp_msgb_alloc(); |
| 202 | struct bssgp_normal_hdr *bgph = |
| 203 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 204 | uint16_t _bvci; |
| 205 | |
| 206 | msgb_nsei(msg) = nsei; |
| 207 | msgb_bvci(msg) = ns_bvci; |
| 208 | |
| 209 | bgph->pdu_type = pdu_type; |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 210 | _bvci = osmo_htons(bvci); |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 211 | msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci); |
| 212 | |
| 213 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 214 | } |
| 215 | |
| 216 | /* Chapter 10.4.14: Status */ |
| 217 | int bssgp_tx_status(uint8_t cause, uint16_t *bvci, struct msgb *orig_msg) |
| 218 | { |
| 219 | struct msgb *msg = bssgp_msgb_alloc(); |
| 220 | struct bssgp_normal_hdr *bgph = |
| 221 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 222 | |
Jacob Erlbeck | a716577 | 2014-09-23 13:28:21 +0200 | [diff] [blame] | 223 | /* GSM 08.18, 10.4.14.1: The BVCI must be included if (and only if) the |
| 224 | cause is either "BVCI blocked" or "BVCI unknown" */ |
| 225 | if (cause == BSSGP_CAUSE_UNKNOWN_BVCI || cause == BSSGP_CAUSE_BVCI_BLOCKED) { |
| 226 | if (bvci == NULL) |
| 227 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP Tx STATUS, cause=%s: " |
| 228 | "missing conditional BVCI\n", |
| 229 | bssgp_cause_str(cause)); |
| 230 | } else { |
| 231 | if (bvci != NULL) |
| 232 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP Tx STATUS, cause=%s: " |
| 233 | "unexpected conditional BVCI\n", |
| 234 | bssgp_cause_str(cause)); |
| 235 | } |
| 236 | |
Holger Hans Peter Freyther | 8e2e78e | 2010-06-08 16:05:20 +0800 | [diff] [blame] | 237 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Tx STATUS, cause=%s\n", |
Harald Welte | cfb545a | 2010-06-03 21:30:57 +0200 | [diff] [blame] | 238 | bvci ? *bvci : 0, bssgp_cause_str(cause)); |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 239 | msgb_nsei(msg) = msgb_nsei(orig_msg); |
| 240 | msgb_bvci(msg) = 0; |
| 241 | |
| 242 | bgph->pdu_type = BSSGP_PDUT_STATUS; |
| 243 | msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause); |
| 244 | if (bvci) { |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 245 | uint16_t _bvci = osmo_htons(*bvci); |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 246 | msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci); |
| 247 | } |
Harald Welte | d83d1b6 | 2011-07-16 13:45:10 +0200 | [diff] [blame] | 248 | msgb_tvlv_put(msg, BSSGP_IE_PDU_IN_ERROR, |
| 249 | msgb_bssgp_len(orig_msg), msgb_bssgph(orig_msg)); |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 250 | |
| 251 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 252 | } |