Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file ipa.c |
| 2 | * OpenBSC Abis input driver for ip.access */ |
| 3 | /* |
| 4 | * (C) 2009-2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 5 | * (C) 2010 by Holger Hans Peter Freyther |
| 6 | * (C) 2010 by On-Waves |
| 7 | * |
| 8 | * All Rights Reserved |
| 9 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
| 11 | * |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | fd5ad17 | 2014-10-26 20:47:42 +0100 | [diff] [blame] | 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | fd5ad17 | 2014-10-26 20:47:42 +0100 | [diff] [blame] | 20 | * GNU General Public License for more details. |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 21 | * |
Harald Welte | fd5ad17 | 2014-10-26 20:47:42 +0100 | [diff] [blame] | 22 | * You should have received a copy of the GNU General Public License |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 23 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | */ |
| 26 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 27 | #include "config.h" |
| 28 | |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <stdint.h> |
| 31 | #include <errno.h> |
| 32 | #include <stdlib.h> |
| 33 | |
Holger Hans Peter Freyther | ee6652b | 2015-11-09 16:21:19 +0000 | [diff] [blame] | 34 | #include <sys/types.h> |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 35 | |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 36 | #include <osmocom/core/byteswap.h> |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 37 | #include <osmocom/core/msgb.h> |
| 38 | #include <osmocom/core/talloc.h> |
| 39 | #include <osmocom/core/logging.h> |
| 40 | #include <osmocom/core/macaddr.h> |
| 41 | #include <osmocom/core/select.h> |
| 42 | |
| 43 | #include <osmocom/gsm/tlv.h> |
| 44 | #include <osmocom/gsm/protocol/ipaccess.h> |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 45 | #include <osmocom/gsm/ipa.h> |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 46 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 47 | /*! \addtogroup ipa |
| 48 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 49 | * IPA Multiplex utility routines |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 50 | */ |
| 51 | |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 52 | #define IPA_ALLOC_SIZE 1200 |
| 53 | |
| 54 | /* |
| 55 | * Common propietary IPA messages: |
| 56 | * - PONG: in reply to PING. |
| 57 | * - ID_REQUEST: first messages once OML has been established. |
| 58 | * - ID_ACK: in reply to ID_ACK. |
| 59 | */ |
| 60 | static const uint8_t ipa_pong_msg[] = { |
| 61 | 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG |
| 62 | }; |
| 63 | |
| 64 | static const uint8_t ipa_id_ack_msg[] = { |
| 65 | 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK |
| 66 | }; |
| 67 | |
| 68 | static const uint8_t ipa_id_req_msg[] = { |
| 69 | 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET, |
| 70 | 0x01, IPAC_IDTAG_UNIT, |
| 71 | 0x01, IPAC_IDTAG_MACADDR, |
| 72 | 0x01, IPAC_IDTAG_LOCATION1, |
| 73 | 0x01, IPAC_IDTAG_LOCATION2, |
| 74 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 75 | 0x01, IPAC_IDTAG_SWVERSION, |
| 76 | 0x01, IPAC_IDTAG_UNITNAME, |
| 77 | 0x01, IPAC_IDTAG_SERNR, |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | static const char *idtag_names[] = { |
| 82 | [IPAC_IDTAG_SERNR] = "Serial_Number", |
| 83 | [IPAC_IDTAG_UNITNAME] = "Unit_Name", |
| 84 | [IPAC_IDTAG_LOCATION1] = "Location_1", |
| 85 | [IPAC_IDTAG_LOCATION2] = "Location_2", |
| 86 | [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version", |
| 87 | [IPAC_IDTAG_SWVERSION] = "Software_Version", |
| 88 | [IPAC_IDTAG_IPADDR] = "IP_Address", |
| 89 | [IPAC_IDTAG_MACADDR] = "MAC_Address", |
| 90 | [IPAC_IDTAG_UNIT] = "Unit_ID", |
| 91 | }; |
| 92 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 93 | const char *ipa_ccm_idtag_name(uint8_t tag) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 94 | { |
| 95 | if (tag >= ARRAY_SIZE(idtag_names)) |
| 96 | return "unknown"; |
| 97 | |
| 98 | return idtag_names[tag]; |
| 99 | } |
| 100 | |
Pau Espin Pedrol | deeab47 | 2019-03-27 17:33:17 +0100 | [diff] [blame] | 101 | /*! Parse the payload part of an IPA CCM ID GET, return \ref tlv_parsed format. */ |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 102 | int ipa_ccm_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 103 | { |
Pau Espin Pedrol | deeab47 | 2019-03-27 17:33:17 +0100 | [diff] [blame] | 104 | return ipa_ccm_idtag_parse_off(dec, buf, len, 1); |
Harald Welte | 5a7740d | 2018-08-01 17:29:48 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Pau Espin Pedrol | deeab47 | 2019-03-27 17:33:17 +0100 | [diff] [blame] | 107 | /*! Parse the payload part of an IPA CCM ID GET, return \ref tlv_parsed format. |
| 108 | * WARNING: This function can only parse correctly IPA CCM ID GET/REQUEST |
| 109 | * messages, and only when len_offset is passed value of 1. |
| 110 | * \param[out] dec Caller-provided/allocated output structure for parsed payload |
| 111 | * \param[in] buf Buffer containing the payload (excluding 1 byte msg_type) of the message |
| 112 | * \param[in] len Length of \a buf in octets |
| 113 | * \param[in] len_offset Offset from end of len field to start of value (ommiting tag). Must be 1! |
| 114 | * \returns 0 on success; negative on error |
| 115 | */ |
Harald Welte | 5a7740d | 2018-08-01 17:29:48 +0200 | [diff] [blame] | 116 | int ipa_ccm_idtag_parse_off(struct tlv_parsed *dec, unsigned char *buf, int len, const int len_offset) |
| 117 | { |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 118 | uint8_t t_len; |
| 119 | uint8_t t_tag; |
| 120 | uint8_t *cur = buf; |
| 121 | |
| 122 | memset(dec, 0, sizeof(*dec)); |
| 123 | |
| 124 | while (len >= 2) { |
| 125 | len -= 2; |
| 126 | t_len = *cur++; |
| 127 | t_tag = *cur++; |
| 128 | |
Harald Welte | 5a7740d | 2018-08-01 17:29:48 +0200 | [diff] [blame] | 129 | if (t_len < len_offset) { |
| 130 | LOGP(DLMI, LOGL_ERROR, "minimal offset not included: %d < %d\n", t_len, len_offset); |
| 131 | return -EINVAL; |
| 132 | } |
| 133 | |
Harald Welte | 7869baf | 2018-07-31 20:25:48 +0200 | [diff] [blame] | 134 | if (t_len > len + 1) { |
| 135 | LOGP(DLMI, LOGL_ERROR, "The tag does not fit: %d > %d\n", t_len, len + 1); |
Holger Hans Peter Freyther | f558ed4 | 2015-06-02 15:52:06 +0200 | [diff] [blame] | 136 | return -EINVAL; |
| 137 | } |
| 138 | |
Harald Welte | 7869baf | 2018-07-31 20:25:48 +0200 | [diff] [blame] | 139 | DEBUGPC(DLMI, "%s='%s' ", ipa_ccm_idtag_name(t_tag), cur); |
| 140 | |
Oliver Smith | 27f7b0d | 2019-03-25 13:45:57 +0100 | [diff] [blame] | 141 | dec->lv[t_tag].len = t_len - len_offset; |
Harald Welte | 7869baf | 2018-07-31 20:25:48 +0200 | [diff] [blame] | 142 | dec->lv[t_tag].val = cur; |
| 143 | |
Harald Welte | 5a7740d | 2018-08-01 17:29:48 +0200 | [diff] [blame] | 144 | cur += t_len - len_offset; |
| 145 | len -= t_len - len_offset; |
Harald Welte | 7869baf | 2018-07-31 20:25:48 +0200 | [diff] [blame] | 146 | } |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /*! Parse the payload part of an IPA CCM ID GET, return \ref tlv_parsed format. |
| 151 | * The odd payload format of those messages is structured as follows: |
| 152 | * * 8bit length value (length of payload *and tag*) |
| 153 | * * 8bit tag value |
| 154 | * * optional, variable-length payload |
| 155 | * \param[out] dec Caller-provided/allocated output structure for parsed payload |
| 156 | * \param[in] buf Buffer containing the payload (excluding 1 byte msg_type) of the message |
| 157 | * \param[in] len Length of \a buf in octets |
| 158 | * \returns 0 on success; negative on error */ |
| 159 | int ipa_ccm_id_get_parse(struct tlv_parsed *dec, const uint8_t *buf, unsigned int len) |
| 160 | { |
| 161 | uint8_t t_len; |
| 162 | uint8_t t_tag; |
| 163 | const uint8_t *cur = buf; |
| 164 | |
| 165 | memset(dec, 0, sizeof(*dec)); |
| 166 | |
| 167 | while (len >= 2) { |
| 168 | len -= 2; |
| 169 | t_len = *cur++; |
| 170 | t_tag = *cur++; |
| 171 | |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 172 | if (t_len > len + 1) { |
Max | 9b4d065 | 2016-11-15 19:21:23 +0100 | [diff] [blame] | 173 | LOGP(DLMI, LOGL_ERROR, "The tag does not fit: %d > %d\n", t_len, len + 1); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 174 | return -EINVAL; |
| 175 | } |
| 176 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 177 | DEBUGPC(DLMI, "%s='%s' ", ipa_ccm_idtag_name(t_tag), cur); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 178 | |
Harald Welte | 7869baf | 2018-07-31 20:25:48 +0200 | [diff] [blame] | 179 | dec->lv[t_tag].len = t_len-1; |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 180 | dec->lv[t_tag].val = cur; |
| 181 | |
Harald Welte | 7869baf | 2018-07-31 20:25:48 +0200 | [diff] [blame] | 182 | cur += t_len-1; |
| 183 | len -= t_len-1; |
| 184 | } |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /*! Parse the payload part of an IPA CCM ID RESP, return \ref tlv_parsed format. |
| 189 | * The odd payload format of those messages is structured as follows: |
| 190 | * * 16bit length value (length of payload *and tag*) |
| 191 | * * 8bit tag value |
| 192 | * * optional, variable-length payload |
| 193 | * \param[out] dec Caller-provided/allocated output structure for parsed payload |
| 194 | * \param[in] buf Buffer containing the payload (excluding 1 byte msg_type) of the message |
| 195 | * \param[in] len Length of \a buf in octets |
| 196 | * \returns 0 on success; negative on error */ |
| 197 | int ipa_ccm_id_resp_parse(struct tlv_parsed *dec, const uint8_t *buf, unsigned int len) |
| 198 | { |
| 199 | uint8_t t_len; |
| 200 | uint8_t t_tag; |
| 201 | const uint8_t *cur = buf; |
| 202 | |
| 203 | memset(dec, 0, sizeof(*dec)); |
| 204 | |
| 205 | while (len >= 3) { |
| 206 | len -= 3; |
Pau Espin Pedrol | 3cb6851 | 2019-03-27 17:45:00 +0100 | [diff] [blame] | 207 | t_len = osmo_load16be(cur); |
| 208 | cur += 2; |
Harald Welte | 7869baf | 2018-07-31 20:25:48 +0200 | [diff] [blame] | 209 | t_tag = *cur++; |
| 210 | |
| 211 | if (t_len > len + 1) { |
| 212 | LOGP(DLMI, LOGL_ERROR, "The tag does not fit: %d > %d\n", t_len, len + 1); |
| 213 | return -EINVAL; |
| 214 | } |
| 215 | |
| 216 | DEBUGPC(DLMI, "%s='%s' ", ipa_ccm_idtag_name(t_tag), cur); |
| 217 | |
| 218 | dec->lv[t_tag].len = t_len-1; |
| 219 | dec->lv[t_tag].val = cur; |
| 220 | |
| 221 | cur += t_len-1; |
| 222 | len -= t_len-1; |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 223 | } |
| 224 | return 0; |
| 225 | } |
| 226 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 227 | int ipa_parse_unitid(const char *str, struct ipaccess_unit *unit_data) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 228 | { |
| 229 | unsigned long ul; |
| 230 | char *endptr; |
| 231 | const char *nptr; |
| 232 | |
| 233 | nptr = str; |
| 234 | ul = strtoul(nptr, &endptr, 10); |
| 235 | if (endptr <= nptr) |
| 236 | return -EINVAL; |
| 237 | unit_data->site_id = ul & 0xffff; |
| 238 | |
| 239 | if (*endptr++ != '/') |
| 240 | return -EINVAL; |
| 241 | |
| 242 | nptr = endptr; |
| 243 | ul = strtoul(nptr, &endptr, 10); |
| 244 | if (endptr <= nptr) |
| 245 | return -EINVAL; |
| 246 | unit_data->bts_id = ul & 0xffff; |
| 247 | |
| 248 | if (*endptr++ != '/') |
| 249 | return -EINVAL; |
| 250 | |
| 251 | nptr = endptr; |
| 252 | ul = strtoul(nptr, &endptr, 10); |
| 253 | if (endptr <= nptr) |
| 254 | return -EINVAL; |
| 255 | unit_data->trx_id = ul & 0xffff; |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 260 | int ipa_ccm_tlv_to_unitdata(struct ipaccess_unit *ud, |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 261 | const struct tlv_parsed *tp) |
| 262 | { |
| 263 | int rc = 0; |
| 264 | |
| 265 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_SERNR, 1)) |
| 266 | ud->serno = talloc_strdup(ud, (char *) |
| 267 | TLVP_VAL(tp, IPAC_IDTAG_SERNR)); |
| 268 | |
| 269 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_UNITNAME, 1)) |
| 270 | ud->unit_name = talloc_strdup(ud, (char *) |
| 271 | TLVP_VAL(tp, IPAC_IDTAG_UNITNAME)); |
| 272 | |
| 273 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_LOCATION1, 1)) |
| 274 | ud->location1 = talloc_strdup(ud, (char *) |
| 275 | TLVP_VAL(tp, IPAC_IDTAG_LOCATION1)); |
| 276 | |
| 277 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_LOCATION2, 1)) |
| 278 | ud->location2 = talloc_strdup(ud, (char *) |
| 279 | TLVP_VAL(tp, IPAC_IDTAG_LOCATION2)); |
| 280 | |
| 281 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_EQUIPVERS, 1)) |
| 282 | ud->equipvers = talloc_strdup(ud, (char *) |
| 283 | TLVP_VAL(tp, IPAC_IDTAG_EQUIPVERS)); |
| 284 | |
| 285 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_SWVERSION, 1)) |
| 286 | ud->swversion = talloc_strdup(ud, (char *) |
| 287 | TLVP_VAL(tp, IPAC_IDTAG_SWVERSION)); |
| 288 | |
| 289 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_MACADDR, 17)) { |
| 290 | rc = osmo_macaddr_parse(ud->mac_addr, (char *) |
| 291 | TLVP_VAL(tp, IPAC_IDTAG_MACADDR)); |
| 292 | if (rc < 0) |
| 293 | goto out; |
| 294 | } |
| 295 | |
| 296 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_UNIT, 1)) |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 297 | rc = ipa_parse_unitid((char *) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 298 | TLVP_VAL(tp, IPAC_IDTAG_UNIT), ud); |
| 299 | |
| 300 | out: |
| 301 | return rc; |
| 302 | } |
| 303 | |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 304 | #define IPA_STRING_MAX 64 |
| 305 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 306 | /*! Generate IPA CCM ID RESP based on list of IEs |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 307 | * \param[in] dev Descriptor describing identity data for response |
| 308 | * \param[in] ies_req List of IEIs to include in response |
| 309 | * \param[in] num_ies_req Number of IEIs in \a ies_req |
| 310 | * \returns Message buffer with IPA CCM ID RESP */ |
| 311 | struct msgb *ipa_ccm_make_id_resp(const struct ipaccess_unit *dev, |
| 312 | const uint8_t *ies_req, unsigned int num_ies_req) |
| 313 | { |
| 314 | struct msgb *msg = ipa_msg_alloc(16); |
| 315 | char str[IPA_STRING_MAX]; |
| 316 | unsigned int i; |
| 317 | |
| 318 | if (!msg) |
| 319 | return NULL; |
| 320 | |
| 321 | *msgb_put(msg, 1) = IPAC_MSGT_ID_RESP; |
| 322 | |
| 323 | for (i = 0; i < num_ies_req; i++) { |
| 324 | uint8_t *tag; |
| 325 | |
| 326 | str[0] = '\0'; |
| 327 | switch (ies_req[i]) { |
| 328 | case IPAC_IDTAG_UNIT: |
| 329 | snprintf(str, sizeof(str), "%u/%u/%u", |
| 330 | dev->site_id, dev->bts_id, dev->trx_id); |
| 331 | break; |
| 332 | case IPAC_IDTAG_MACADDR: |
| 333 | snprintf(str, sizeof(str), |
| 334 | "%02x:%02x:%02x:%02x:%02x:%02x", |
| 335 | dev->mac_addr[0], dev->mac_addr[1], |
| 336 | dev->mac_addr[2], dev->mac_addr[3], |
| 337 | dev->mac_addr[4], dev->mac_addr[5]); |
| 338 | break; |
| 339 | case IPAC_IDTAG_LOCATION1: |
| 340 | if (dev->location1) |
Neels Hofmeyr | ebe4ef7 | 2018-07-26 17:12:07 +0200 | [diff] [blame] | 341 | osmo_strlcpy(str, dev->location1, sizeof(str)); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 342 | break; |
| 343 | case IPAC_IDTAG_LOCATION2: |
| 344 | if (dev->location2) |
Neels Hofmeyr | ebe4ef7 | 2018-07-26 17:12:07 +0200 | [diff] [blame] | 345 | osmo_strlcpy(str, dev->location2, sizeof(str)); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 346 | break; |
| 347 | case IPAC_IDTAG_EQUIPVERS: |
| 348 | if (dev->equipvers) |
Neels Hofmeyr | ebe4ef7 | 2018-07-26 17:12:07 +0200 | [diff] [blame] | 349 | osmo_strlcpy(str, dev->equipvers, sizeof(str)); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 350 | break; |
| 351 | case IPAC_IDTAG_SWVERSION: |
| 352 | if (dev->swversion) |
Neels Hofmeyr | ebe4ef7 | 2018-07-26 17:12:07 +0200 | [diff] [blame] | 353 | osmo_strlcpy(str, dev->swversion, sizeof(str)); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 354 | break; |
| 355 | case IPAC_IDTAG_UNITNAME: |
| 356 | if (dev->unit_name) { |
Neels Hofmeyr | ebe4ef7 | 2018-07-26 17:12:07 +0200 | [diff] [blame] | 357 | snprintf(str, sizeof(str), "%s", dev->unit_name); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 358 | } else { |
| 359 | snprintf(str, sizeof(str), |
| 360 | "%02x-%02x-%02x-%02x-%02x-%02x", |
| 361 | dev->mac_addr[0], dev->mac_addr[1], |
| 362 | dev->mac_addr[2], dev->mac_addr[3], |
| 363 | dev->mac_addr[4], dev->mac_addr[5]); |
| 364 | } |
| 365 | break; |
| 366 | case IPAC_IDTAG_SERNR: |
| 367 | if (dev->serno) |
Neels Hofmeyr | ebe4ef7 | 2018-07-26 17:12:07 +0200 | [diff] [blame] | 368 | osmo_strlcpy(str, dev->serno, sizeof(str)); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 369 | break; |
| 370 | default: |
| 371 | LOGP(DLINP, LOGL_NOTICE, |
| 372 | "Unknown ipaccess tag 0x%02x\n", ies_req[i]); |
| 373 | msgb_free(msg); |
| 374 | return NULL; |
| 375 | } |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 376 | |
| 377 | LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", ies_req[i], str); |
| 378 | tag = msgb_put(msg, 3 + strlen(str) + 1); |
| 379 | tag[0] = 0x00; |
| 380 | tag[1] = 1 + strlen(str) + 1; |
| 381 | tag[2] = ies_req[1]; |
| 382 | memcpy(tag + 3, str, strlen(str) + 1); |
| 383 | } |
| 384 | ipa_prepend_header(msg, IPAC_PROTO_IPACCESS); |
| 385 | return msg; |
| 386 | } |
| 387 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 388 | /*! Generate IPA CCM ID RESP based on requets payload |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 389 | * \param[in] dev Descriptor describing identity data for response |
| 390 | * \param[in] data Payload of the IPA CCM ID GET request |
| 391 | * \param[in] len Length of \a data in octets |
| 392 | * \returns Message buffer with IPA CCM ID RESP */ |
| 393 | struct msgb *ipa_ccm_make_id_resp_from_req(const struct ipaccess_unit *dev, |
| 394 | const uint8_t *data, unsigned int len) |
| 395 | { |
| 396 | uint8_t ies[len/2]; |
| 397 | unsigned int num_ies = 0; |
| 398 | const uint8_t *cur = data; |
| 399 | |
Harald Welte | 8a4895c | 2017-04-27 10:25:10 +0200 | [diff] [blame] | 400 | memset(ies, 0, sizeof(ies)); |
| 401 | |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 402 | /* build a array of the IEIs */ |
| 403 | while (len >= 2) { |
| 404 | uint8_t t_len, t_tag; |
| 405 | len -= 2; |
| 406 | t_len = *cur++; |
| 407 | t_tag = *cur++; |
| 408 | |
| 409 | if (t_len > len + 1) { |
Thorsten Alteholz | 5a9dbf8 | 2018-04-08 19:13:25 +0200 | [diff] [blame] | 410 | LOGP(DLINP, LOGL_ERROR, "IPA CCM tag 0x%02x does not fit\n", t_tag); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | |
| 414 | ies[num_ies++] = t_tag; |
| 415 | |
| 416 | cur += t_len; |
Harald Welte | 0b2c0ec | 2018-04-16 22:53:48 +0200 | [diff] [blame] | 417 | /* prevent any unsigned integer underflow due to somebody sending us |
| 418 | * messages with wrong length values */ |
| 419 | if (len <= t_len) |
| 420 | len -= t_len; |
| 421 | else |
| 422 | len = 0; |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 423 | } |
| 424 | return ipa_ccm_make_id_resp(dev, ies, num_ies); |
| 425 | } |
| 426 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 427 | int ipa_send(int fd, const void *msg, size_t msglen) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 428 | { |
| 429 | int ret; |
| 430 | |
| 431 | ret = write(fd, msg, msglen); |
| 432 | if (ret < 0) |
Jacob Erlbeck | a6be224 | 2014-12-22 10:58:46 +0100 | [diff] [blame] | 433 | return -errno; |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 434 | if (ret < msglen) { |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 435 | LOGP(DLINP, LOGL_ERROR, "ipa_send: short write\n"); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 436 | return -EIO; |
| 437 | } |
| 438 | return ret; |
| 439 | } |
| 440 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 441 | int ipa_ccm_send_pong(int fd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 442 | { |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 443 | return ipa_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg)); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 446 | int ipa_ccm_send_id_ack(int fd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 447 | { |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 448 | return ipa_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg)); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 451 | int ipa_ccm_send_id_req(int fd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 452 | { |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 453 | return ipa_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg)); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* base handling of the ip.access protocol */ |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 457 | int ipa_ccm_rcvmsg_base(struct msgb *msg, struct osmo_fd *bfd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 458 | { |
| 459 | uint8_t msg_type = *(msg->l2h); |
| 460 | int ret; |
| 461 | |
| 462 | switch (msg_type) { |
| 463 | case IPAC_MSGT_PING: |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 464 | ret = ipa_ccm_send_pong(bfd->fd); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 465 | if (ret < 0) { |
| 466 | LOGP(DLINP, LOGL_ERROR, "Cannot send PING " |
| 467 | "message. Reason: %s\n", strerror(errno)); |
| 468 | break; |
| 469 | } |
| 470 | ret = 1; |
| 471 | break; |
| 472 | case IPAC_MSGT_PONG: |
| 473 | DEBUGP(DLMI, "PONG!\n"); |
| 474 | ret = 1; |
| 475 | break; |
| 476 | case IPAC_MSGT_ID_ACK: |
| 477 | DEBUGP(DLMI, "ID_ACK? -> ACK!\n"); |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 478 | ret = ipa_ccm_send_id_ack(bfd->fd); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 479 | if (ret < 0) { |
| 480 | LOGP(DLINP, LOGL_ERROR, "Cannot send ID_ACK " |
| 481 | "message. Reason: %s\n", strerror(errno)); |
| 482 | break; |
| 483 | } |
| 484 | ret = 1; |
| 485 | break; |
| 486 | default: |
| 487 | /* This is not an IPA PING, PONG or ID_ACK message */ |
| 488 | ret = 0; |
| 489 | break; |
| 490 | } |
| 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | /* base handling of the ip.access protocol */ |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 495 | int ipa_ccm_rcvmsg_bts_base(struct msgb *msg, struct osmo_fd *bfd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 496 | { |
| 497 | uint8_t msg_type = *(msg->l2h); |
| 498 | int ret = 0; |
| 499 | |
| 500 | switch (msg_type) { |
| 501 | case IPAC_MSGT_PING: |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 502 | ret = ipa_ccm_send_pong(bfd->fd); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 503 | if (ret < 0) { |
| 504 | LOGP(DLINP, LOGL_ERROR, "Cannot send PONG " |
| 505 | "message. Reason: %s\n", strerror(errno)); |
| 506 | } |
| 507 | break; |
| 508 | case IPAC_MSGT_PONG: |
| 509 | DEBUGP(DLMI, "PONG!\n"); |
| 510 | break; |
| 511 | case IPAC_MSGT_ID_ACK: |
| 512 | DEBUGP(DLMI, "ID_ACK\n"); |
| 513 | break; |
| 514 | } |
| 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 519 | void ipa_prepend_header_ext(struct msgb *msg, int proto) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 520 | { |
| 521 | struct ipaccess_head_ext *hh_ext; |
| 522 | |
| 523 | /* prepend the osmo ip.access header extension */ |
| 524 | hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext)); |
| 525 | hh_ext->proto = proto; |
| 526 | } |
| 527 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 528 | void ipa_prepend_header(struct msgb *msg, int proto) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 529 | { |
| 530 | struct ipaccess_head *hh; |
| 531 | |
| 532 | /* prepend the ip.access header */ |
| 533 | hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh)); |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 534 | hh->len = osmo_htons(msg->len - sizeof(*hh)); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 535 | hh->proto = proto; |
| 536 | } |
| 537 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 538 | #ifdef HAVE_SYS_SOCKET_H |
| 539 | #include <sys/socket.h> |
| 540 | |
Pau Espin Pedrol | 8a757d2 | 2018-08-22 14:12:01 +0200 | [diff] [blame] | 541 | /*! Read one ipa message from socket fd without caching not fully received |
| 542 | * messages. See \ref ipa_msg_recv_buffered for further information. |
| 543 | */ |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 544 | int ipa_msg_recv(int fd, struct msgb **rmsg) |
| 545 | { |
| 546 | int rc = ipa_msg_recv_buffered(fd, rmsg, NULL); |
| 547 | if (rc < 0) { |
| 548 | errno = -rc; |
| 549 | rc = -1; |
| 550 | } |
| 551 | return rc; |
| 552 | } |
| 553 | |
Pau Espin Pedrol | 8a757d2 | 2018-08-22 14:12:01 +0200 | [diff] [blame] | 554 | /*! Read one ipa message from socket fd or store part if still not fully received. |
| 555 | * \param[in] fd The fd for the socket to read from. |
| 556 | * \param[out] rmsg internally allocated msgb containing a fully received ipa message. |
| 557 | * \param[inout] tmp_msg internally allocated msgb caching data for not yet fully received message. |
| 558 | * |
| 559 | * As ipa can run on top of stream based protocols such as TCP, there's the |
| 560 | * possibility that such lower layers split ipa messages in several low level |
| 561 | * packets. If a low layer packet is received containing several ipa frames, |
| 562 | * this function will pull from the socket and return only the first one |
| 563 | * available in the stream. As the socket will remain with data, it will |
| 564 | * trigger again during next select() and then this function will fetch the |
| 565 | * next ipa message, and so on. |
| 566 | * |
| 567 | * \returns -EAGAIN and allocated tmp_msg if message was not yet fully |
| 568 | * received. Other negative values indicate an error and cached msgb will be |
| 569 | * freed. 0 if socket is found dead. Positive value indicating l2 msgb len and |
| 570 | * rmsg pointing to internally allocated msgb containing the ipa frame on |
| 571 | * scucess. |
| 572 | */ |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 573 | int ipa_msg_recv_buffered(int fd, struct msgb **rmsg, struct msgb **tmp_msg) |
| 574 | { |
| 575 | struct msgb *msg = tmp_msg ? *tmp_msg : NULL; |
| 576 | struct ipaccess_head *hh; |
| 577 | int len, ret; |
| 578 | int needed; |
| 579 | |
| 580 | if (msg == NULL) { |
| 581 | msg = ipa_msg_alloc(0); |
| 582 | if (msg == NULL) { |
| 583 | ret = -ENOMEM; |
| 584 | goto discard_msg; |
| 585 | } |
| 586 | msg->l1h = msg->tail; |
| 587 | } |
| 588 | |
| 589 | if (msg->l2h == NULL) { |
| 590 | /* first read our 3-byte header */ |
| 591 | needed = sizeof(*hh) - msg->len; |
| 592 | ret = recv(fd, msg->tail, needed, 0); |
| 593 | if (ret == 0) |
| 594 | goto discard_msg; |
| 595 | |
| 596 | if (ret < 0) { |
| 597 | if (errno == EAGAIN || errno == EINTR) |
| 598 | ret = 0; |
| 599 | else { |
| 600 | ret = -errno; |
| 601 | goto discard_msg; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | msgb_put(msg, ret); |
| 606 | |
| 607 | if (ret < needed) { |
| 608 | if (msg->len == 0) { |
| 609 | ret = -EAGAIN; |
| 610 | goto discard_msg; |
| 611 | } |
| 612 | |
| 613 | LOGP(DLINP, LOGL_INFO, |
Harald Welte | f196a02 | 2014-08-21 09:42:03 +0200 | [diff] [blame] | 614 | "Received part of IPA message header (%d/%zu)\n", |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 615 | msg->len, sizeof(*hh)); |
| 616 | if (!tmp_msg) { |
| 617 | ret = -EIO; |
| 618 | goto discard_msg; |
| 619 | } |
| 620 | *tmp_msg = msg; |
| 621 | return -EAGAIN; |
| 622 | } |
| 623 | |
| 624 | msg->l2h = msg->tail; |
| 625 | } |
| 626 | |
| 627 | hh = (struct ipaccess_head *) msg->data; |
| 628 | |
| 629 | /* then read the length as specified in header */ |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 630 | len = osmo_ntohs(hh->len); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 631 | |
| 632 | if (len < 0 || IPA_ALLOC_SIZE < len + sizeof(*hh)) { |
| 633 | LOGP(DLINP, LOGL_ERROR, "bad message length of %d bytes, " |
| 634 | "received %d bytes\n", len, msg->len); |
| 635 | ret = -EIO; |
| 636 | goto discard_msg; |
| 637 | } |
| 638 | |
| 639 | needed = len - msgb_l2len(msg); |
| 640 | |
| 641 | if (needed > 0) { |
| 642 | ret = recv(fd, msg->tail, needed, 0); |
| 643 | |
| 644 | if (ret == 0) |
| 645 | goto discard_msg; |
| 646 | |
| 647 | if (ret < 0) { |
| 648 | if (errno == EAGAIN || errno == EINTR) |
| 649 | ret = 0; |
| 650 | else { |
| 651 | ret = -errno; |
| 652 | goto discard_msg; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | msgb_put(msg, ret); |
| 657 | |
| 658 | if (ret < needed) { |
| 659 | LOGP(DLINP, LOGL_INFO, |
| 660 | "Received part of IPA message L2 data (%d/%d)\n", |
| 661 | msgb_l2len(msg), len); |
| 662 | if (!tmp_msg) { |
| 663 | ret = -EIO; |
| 664 | goto discard_msg; |
| 665 | } |
| 666 | *tmp_msg = msg; |
| 667 | return -EAGAIN; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | ret = msgb_l2len(msg); |
| 672 | |
| 673 | if (ret == 0) { |
| 674 | LOGP(DLINP, LOGL_INFO, |
| 675 | "Discarding IPA message without payload\n"); |
| 676 | ret = -EAGAIN; |
| 677 | goto discard_msg; |
| 678 | } |
| 679 | |
| 680 | if (tmp_msg) |
| 681 | *tmp_msg = NULL; |
| 682 | *rmsg = msg; |
| 683 | return ret; |
| 684 | |
| 685 | discard_msg: |
| 686 | if (tmp_msg) |
| 687 | *tmp_msg = NULL; |
| 688 | msgb_free(msg); |
| 689 | return ret; |
| 690 | } |
| 691 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 692 | #endif /* SYS_SOCKET_H */ |
| 693 | |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 694 | struct msgb *ipa_msg_alloc(int headroom) |
| 695 | { |
| 696 | struct msgb *nmsg; |
| 697 | |
| 698 | headroom += sizeof(struct ipaccess_head); |
| 699 | |
Neels Hofmeyr | 889ab16 | 2017-09-07 20:41:12 +0200 | [diff] [blame] | 700 | nmsg = msgb_alloc_headroom(1200 + headroom, headroom, "IPA Multiplex"); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 701 | if (!nmsg) |
| 702 | return NULL; |
| 703 | return nmsg; |
| 704 | } |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 705 | |
| 706 | /*! @} */ |