Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 1 | /* GPRS BSSGP protocol implementation as per 3GPP TS 08.18 */ |
| 2 | |
Harald Welte | 605ac5d | 2012-06-16 16:09:52 +0800 | [diff] [blame] | 3 | /* (C) 2009-2012 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 15 | * GNU General Public License for more details. |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 16 | * |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License |
Harald Welte | e4cbb3f | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #include <netinet/in.h> |
| 26 | |
Pablo Neira Ayuso | ff66323 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 27 | #include <osmocom/core/msgb.h> |
| 28 | #include <osmocom/gsm/tlv.h> |
| 29 | #include <osmocom/core/talloc.h> |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 30 | #include <osmocom/gprs/gprs_bssgp.h> |
| 31 | #include <osmocom/gprs/gprs_ns.h> |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 32 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 33 | #include "common_vty.h" |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 34 | |
| 35 | struct gprs_ns_inst *bssgp_nsi; |
| 36 | |
| 37 | /* BSSGP Protocol specific, not implementation specific */ |
| 38 | /* FIXME: This needs to go into libosmocore after finished */ |
| 39 | |
| 40 | /* Chapter 11.3.9 / Table 11.10: Cause coding */ |
| 41 | static const struct value_string bssgp_cause_strings[] = { |
| 42 | { BSSGP_CAUSE_PROC_OVERLOAD, "Processor overload" }, |
| 43 | { BSSGP_CAUSE_EQUIP_FAIL, "Equipment Failure" }, |
| 44 | { BSSGP_CAUSE_TRASIT_NET_FAIL, "Transit netowkr service failure" }, |
| 45 | { BSSGP_CAUSE_CAPA_GREATER_0KPBS,"Transmission capacity modified" }, |
| 46 | { BSSGP_CAUSE_UNKNOWN_MS, "Unknown MS" }, |
| 47 | { BSSGP_CAUSE_UNKNOWN_BVCI, "Unknown BVCI" }, |
| 48 | { BSSGP_CAUSE_CELL_TRAF_CONG, "Cell traffic congestion" }, |
| 49 | { BSSGP_CAUSE_SGSN_CONG, "SGSN congestion" }, |
| 50 | { BSSGP_CAUSE_OML_INTERV, "O&M intervention" }, |
| 51 | { BSSGP_CAUSE_BVCI_BLOCKED, "BVCI blocked" }, |
| 52 | { BSSGP_CAUSE_PFC_CREATE_FAIL, "PFC create failure" }, |
| 53 | { BSSGP_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 54 | { BSSGP_CAUSE_INV_MAND_INF, "Invalid mandatory information" }, |
| 55 | { BSSGP_CAUSE_MISSING_MAND_IE, "Missing mandatory IE" }, |
| 56 | { BSSGP_CAUSE_MISSING_COND_IE, "Missing conditional IE" }, |
| 57 | { BSSGP_CAUSE_UNEXP_COND_IE, "Unexpected conditional IE" }, |
| 58 | { BSSGP_CAUSE_COND_IE_ERR, "Conditional IE error" }, |
| 59 | { BSSGP_CAUSE_PDU_INCOMP_STATE, "PDU incompatible with protocol state" }, |
| 60 | { BSSGP_CAUSE_PROTO_ERR_UNSPEC, "Protocol error - unspecified" }, |
| 61 | { BSSGP_CAUSE_PDU_INCOMP_FEAT, "PDU not compatible with feature set" }, |
| 62 | { 0, NULL }, |
| 63 | }; |
| 64 | |
Max | c0d9a6c | 2016-03-09 12:29:23 +0100 | [diff] [blame] | 65 | static const struct value_string bssgp_pdu_strings[] = { |
| 66 | { BSSGP_PDUT_DL_UNITDATA, "DL-UNITDATA" }, |
| 67 | { BSSGP_PDUT_UL_UNITDATA, "UL-UNITDATA" }, |
| 68 | { BSSGP_PDUT_RA_CAPABILITY, "RA-CAPABILITY" }, |
| 69 | { BSSGP_PDUT_PTM_UNITDATA, "PTM-UNITDATA" }, |
| 70 | { BSSGP_PDUT_PAGING_PS, "PAGING-PS" }, |
| 71 | { BSSGP_PDUT_PAGING_CS, "PAGING-CS" }, |
| 72 | { BSSGP_PDUT_RA_CAPA_UDPATE, "RA-CAPABILITY-UPDATE" }, |
| 73 | { BSSGP_PDUT_RA_CAPA_UPDATE_ACK, "RA-CAPABILITY-UPDATE-ACK" }, |
| 74 | { BSSGP_PDUT_RADIO_STATUS, "RADIO-STATUS" }, |
| 75 | { BSSGP_PDUT_SUSPEND, "SUSPEND" }, |
| 76 | { BSSGP_PDUT_SUSPEND_ACK, "SUSPEND-ACK" }, |
| 77 | { BSSGP_PDUT_SUSPEND_NACK, "SUSPEND-NACK" }, |
| 78 | { BSSGP_PDUT_RESUME, "RESUME" }, |
| 79 | { BSSGP_PDUT_RESUME_ACK, "RESUME-ACK" }, |
| 80 | { BSSGP_PDUT_RESUME_NACK, "RESUME-NACK" }, |
| 81 | { BSSGP_PDUT_BVC_BLOCK, "BVC-BLOCK" }, |
| 82 | { BSSGP_PDUT_BVC_BLOCK_ACK, "BVC-BLOCK-ACK" }, |
| 83 | { BSSGP_PDUT_BVC_RESET, "BVC-RESET" }, |
| 84 | { BSSGP_PDUT_BVC_RESET_ACK, "BVC-RESET-ACK" }, |
| 85 | { BSSGP_PDUT_BVC_UNBLOCK, "BVC-UNBLOCK" }, |
| 86 | { BSSGP_PDUT_BVC_UNBLOCK_ACK, "BVC-UNBLOCK-ACK" }, |
| 87 | { BSSGP_PDUT_FLOW_CONTROL_BVC, "FLOW-CONTROL-BVC" }, |
| 88 | { BSSGP_PDUT_FLOW_CONTROL_BVC_ACK, "FLOW-CONTROL-BVC-ACK" }, |
| 89 | { BSSGP_PDUT_FLOW_CONTROL_MS, "FLOW-CONTROL-MS" }, |
| 90 | { BSSGP_PDUT_FLOW_CONTROL_MS_ACK, "FLOW-CONTROL-MS-ACK" }, |
| 91 | { BSSGP_PDUT_FLUSH_LL, "FLUSH-LL" }, |
| 92 | { BSSGP_PDUT_FLUSH_LL_ACK, "FLUSH-LL-ACK" }, |
| 93 | { BSSGP_PDUT_LLC_DISCARD, "LLC DISCARDED" }, |
| 94 | { BSSGP_PDUT_SGSN_INVOKE_TRACE, "SGSN-INVOKE-TRACE" }, |
| 95 | { BSSGP_PDUT_STATUS, "STATUS" }, |
| 96 | { BSSGP_PDUT_DOWNLOAD_BSS_PFC, "DOWNLOAD-BSS-PFC" }, |
| 97 | { BSSGP_PDUT_CREATE_BSS_PFC, "CREATE-BSS-PFC" }, |
| 98 | { BSSGP_PDUT_CREATE_BSS_PFC_ACK, "CREATE-BSS-PFC-ACK" }, |
| 99 | { BSSGP_PDUT_CREATE_BSS_PFC_NACK, "CREATE-BSS-PFC-NACK" }, |
| 100 | { BSSGP_PDUT_MODIFY_BSS_PFC, "MODIFY-BSS-PFC" }, |
| 101 | { BSSGP_PDUT_MODIFY_BSS_PFC_ACK, "MODIFY-BSS-PFC-ACK" }, |
| 102 | { BSSGP_PDUT_DELETE_BSS_PFC, "DELETE-BSS-PFC" }, |
| 103 | { BSSGP_PDUT_DELETE_BSS_PFC_ACK, "DELETE-BSS-PFC-ACK" }, |
| 104 | { 0, NULL }, |
| 105 | }; |
| 106 | |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 107 | const char *bssgp_cause_str(enum gprs_bssgp_cause cause) |
| 108 | { |
| 109 | return get_value_string(bssgp_cause_strings, cause); |
| 110 | } |
| 111 | |
Max | c0d9a6c | 2016-03-09 12:29:23 +0100 | [diff] [blame] | 112 | const char *bssgp_pdu_str(enum bssgp_pdu_type pdu) |
| 113 | { |
| 114 | return get_value_string(bssgp_pdu_strings, pdu); |
| 115 | } |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 116 | |
| 117 | struct msgb *bssgp_msgb_alloc(void) |
| 118 | { |
Jacob Erlbeck | a39e2de | 2014-09-23 13:28:24 +0200 | [diff] [blame] | 119 | struct msgb *msg = msgb_alloc_headroom(4096, 128, "BSSGP"); |
Jacob Erlbeck | a84db61 | 2015-04-09 14:22:22 +0200 | [diff] [blame] | 120 | |
| 121 | /* TODO: Add handling of msg == NULL to this function and to all callers */ |
| 122 | OSMO_ASSERT(msg != NULL); |
| 123 | |
Jacob Erlbeck | a39e2de | 2014-09-23 13:28:24 +0200 | [diff] [blame] | 124 | msgb_bssgph(msg) = msg->data; |
| 125 | return msg; |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Jacob Erlbeck | f78ec5c | 2015-11-17 09:53:23 +0100 | [diff] [blame] | 128 | struct msgb *bssgp_msgb_copy(const struct msgb *msg, const char *name) |
| 129 | { |
| 130 | struct libgb_msgb_cb *old_cb, *new_cb; |
| 131 | struct msgb *new_msg; |
| 132 | |
| 133 | new_msg = msgb_copy(msg, name); |
| 134 | if (!new_msg) |
| 135 | return NULL; |
| 136 | |
| 137 | /* copy GB specific data */ |
| 138 | old_cb = LIBGB_MSGB_CB(msg); |
| 139 | new_cb = LIBGB_MSGB_CB(new_msg); |
| 140 | |
| 141 | if (old_cb->bssgph) |
| 142 | new_cb->bssgph = new_msg->_data + (old_cb->bssgph - msg->_data); |
| 143 | if (old_cb->llch) |
| 144 | new_cb->llch = new_msg->_data + (old_cb->llch - msg->_data); |
| 145 | |
| 146 | /* bssgp_cell_id is a pointer into the old msgb, so we need to make |
| 147 | * it a pointer into the new msgb */ |
| 148 | if (old_cb->bssgp_cell_id) |
| 149 | new_cb->bssgp_cell_id = new_msg->_data + |
| 150 | (old_cb->bssgp_cell_id - msg->_data); |
| 151 | new_cb->nsei = old_cb->nsei; |
| 152 | new_cb->bvci = old_cb->bvci; |
| 153 | new_cb->tlli = old_cb->tlli; |
| 154 | |
| 155 | return new_msg; |
| 156 | } |
| 157 | |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 158 | /* Transmit a simple response such as BLOCK/UNBLOCK/RESET ACK/NACK */ |
| 159 | int bssgp_tx_simple_bvci(uint8_t pdu_type, uint16_t nsei, |
| 160 | uint16_t bvci, uint16_t ns_bvci) |
| 161 | { |
| 162 | struct msgb *msg = bssgp_msgb_alloc(); |
| 163 | struct bssgp_normal_hdr *bgph = |
| 164 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 165 | uint16_t _bvci; |
| 166 | |
| 167 | msgb_nsei(msg) = nsei; |
| 168 | msgb_bvci(msg) = ns_bvci; |
| 169 | |
| 170 | bgph->pdu_type = pdu_type; |
| 171 | _bvci = htons(bvci); |
| 172 | msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci); |
| 173 | |
| 174 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 175 | } |
| 176 | |
| 177 | /* Chapter 10.4.14: Status */ |
| 178 | int bssgp_tx_status(uint8_t cause, uint16_t *bvci, struct msgb *orig_msg) |
| 179 | { |
| 180 | struct msgb *msg = bssgp_msgb_alloc(); |
| 181 | struct bssgp_normal_hdr *bgph = |
| 182 | (struct bssgp_normal_hdr *) msgb_put(msg, sizeof(*bgph)); |
| 183 | |
Jacob Erlbeck | a716577 | 2014-09-23 13:28:21 +0200 | [diff] [blame] | 184 | /* GSM 08.18, 10.4.14.1: The BVCI must be included if (and only if) the |
| 185 | cause is either "BVCI blocked" or "BVCI unknown" */ |
| 186 | if (cause == BSSGP_CAUSE_UNKNOWN_BVCI || cause == BSSGP_CAUSE_BVCI_BLOCKED) { |
| 187 | if (bvci == NULL) |
| 188 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP Tx STATUS, cause=%s: " |
| 189 | "missing conditional BVCI\n", |
| 190 | bssgp_cause_str(cause)); |
| 191 | } else { |
| 192 | if (bvci != NULL) |
| 193 | LOGP(DBSSGP, LOGL_ERROR, "BSSGP Tx STATUS, cause=%s: " |
| 194 | "unexpected conditional BVCI\n", |
| 195 | bssgp_cause_str(cause)); |
| 196 | } |
| 197 | |
Holger Hans Peter Freyther | 8e2e78e | 2010-06-08 16:05:20 +0800 | [diff] [blame] | 198 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u Tx STATUS, cause=%s\n", |
Harald Welte | cfb545a | 2010-06-03 21:30:57 +0200 | [diff] [blame] | 199 | bvci ? *bvci : 0, bssgp_cause_str(cause)); |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 200 | msgb_nsei(msg) = msgb_nsei(orig_msg); |
| 201 | msgb_bvci(msg) = 0; |
| 202 | |
| 203 | bgph->pdu_type = BSSGP_PDUT_STATUS; |
| 204 | msgb_tvlv_put(msg, BSSGP_IE_CAUSE, 1, &cause); |
| 205 | if (bvci) { |
| 206 | uint16_t _bvci = htons(*bvci); |
| 207 | msgb_tvlv_put(msg, BSSGP_IE_BVCI, 2, (uint8_t *) &_bvci); |
| 208 | } |
Harald Welte | d83d1b6 | 2011-07-16 13:45:10 +0200 | [diff] [blame] | 209 | msgb_tvlv_put(msg, BSSGP_IE_PDU_IN_ERROR, |
| 210 | msgb_bssgp_len(orig_msg), msgb_bssgph(orig_msg)); |
Harald Welte | af08678 | 2010-05-11 10:01:17 +0200 | [diff] [blame] | 211 | |
| 212 | return gprs_ns_sendmsg(bssgp_nsi, msg); |
| 213 | } |