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 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 101 | 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] | 102 | { |
Holger Hans Peter Freyther | f558ed4 | 2015-06-02 15:52:06 +0200 | [diff] [blame] | 103 | return ipa_ccm_idtag_parse_off(dec, buf, len, 0); |
| 104 | } |
| 105 | |
| 106 | int ipa_ccm_idtag_parse_off(struct tlv_parsed *dec, unsigned char *buf, int len, const int len_offset) |
| 107 | { |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 108 | uint8_t t_len; |
| 109 | uint8_t t_tag; |
| 110 | uint8_t *cur = buf; |
| 111 | |
| 112 | memset(dec, 0, sizeof(*dec)); |
| 113 | |
| 114 | while (len >= 2) { |
| 115 | len -= 2; |
| 116 | t_len = *cur++; |
| 117 | t_tag = *cur++; |
| 118 | |
Holger Hans Peter Freyther | f558ed4 | 2015-06-02 15:52:06 +0200 | [diff] [blame] | 119 | if (t_len < len_offset) { |
Max | 9b4d065 | 2016-11-15 19:21:23 +0100 | [diff] [blame] | 120 | LOGP(DLMI, LOGL_ERROR, "minimal offset not included: %d < %d\n", t_len, len_offset); |
Holger Hans Peter Freyther | f558ed4 | 2015-06-02 15:52:06 +0200 | [diff] [blame] | 121 | return -EINVAL; |
| 122 | } |
| 123 | |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 124 | if (t_len > len + 1) { |
Max | 9b4d065 | 2016-11-15 19:21:23 +0100 | [diff] [blame] | 125 | 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] | 126 | return -EINVAL; |
| 127 | } |
| 128 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 129 | DEBUGPC(DLMI, "%s='%s' ", ipa_ccm_idtag_name(t_tag), cur); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 130 | |
Holger Hans Peter Freyther | f558ed4 | 2015-06-02 15:52:06 +0200 | [diff] [blame] | 131 | dec->lv[t_tag].len = t_len - len_offset; |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 132 | dec->lv[t_tag].val = cur; |
| 133 | |
Holger Hans Peter Freyther | f558ed4 | 2015-06-02 15:52:06 +0200 | [diff] [blame] | 134 | cur += t_len - len_offset; |
| 135 | len -= t_len - len_offset; |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 140 | int ipa_parse_unitid(const char *str, struct ipaccess_unit *unit_data) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 141 | { |
| 142 | unsigned long ul; |
| 143 | char *endptr; |
| 144 | const char *nptr; |
| 145 | |
| 146 | nptr = str; |
| 147 | ul = strtoul(nptr, &endptr, 10); |
| 148 | if (endptr <= nptr) |
| 149 | return -EINVAL; |
| 150 | unit_data->site_id = ul & 0xffff; |
| 151 | |
| 152 | if (*endptr++ != '/') |
| 153 | return -EINVAL; |
| 154 | |
| 155 | nptr = endptr; |
| 156 | ul = strtoul(nptr, &endptr, 10); |
| 157 | if (endptr <= nptr) |
| 158 | return -EINVAL; |
| 159 | unit_data->bts_id = ul & 0xffff; |
| 160 | |
| 161 | if (*endptr++ != '/') |
| 162 | return -EINVAL; |
| 163 | |
| 164 | nptr = endptr; |
| 165 | ul = strtoul(nptr, &endptr, 10); |
| 166 | if (endptr <= nptr) |
| 167 | return -EINVAL; |
| 168 | unit_data->trx_id = ul & 0xffff; |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 173 | int ipa_ccm_tlv_to_unitdata(struct ipaccess_unit *ud, |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 174 | const struct tlv_parsed *tp) |
| 175 | { |
| 176 | int rc = 0; |
| 177 | |
| 178 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_SERNR, 1)) |
| 179 | ud->serno = talloc_strdup(ud, (char *) |
| 180 | TLVP_VAL(tp, IPAC_IDTAG_SERNR)); |
| 181 | |
| 182 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_UNITNAME, 1)) |
| 183 | ud->unit_name = talloc_strdup(ud, (char *) |
| 184 | TLVP_VAL(tp, IPAC_IDTAG_UNITNAME)); |
| 185 | |
| 186 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_LOCATION1, 1)) |
| 187 | ud->location1 = talloc_strdup(ud, (char *) |
| 188 | TLVP_VAL(tp, IPAC_IDTAG_LOCATION1)); |
| 189 | |
| 190 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_LOCATION2, 1)) |
| 191 | ud->location2 = talloc_strdup(ud, (char *) |
| 192 | TLVP_VAL(tp, IPAC_IDTAG_LOCATION2)); |
| 193 | |
| 194 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_EQUIPVERS, 1)) |
| 195 | ud->equipvers = talloc_strdup(ud, (char *) |
| 196 | TLVP_VAL(tp, IPAC_IDTAG_EQUIPVERS)); |
| 197 | |
| 198 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_SWVERSION, 1)) |
| 199 | ud->swversion = talloc_strdup(ud, (char *) |
| 200 | TLVP_VAL(tp, IPAC_IDTAG_SWVERSION)); |
| 201 | |
| 202 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_MACADDR, 17)) { |
| 203 | rc = osmo_macaddr_parse(ud->mac_addr, (char *) |
| 204 | TLVP_VAL(tp, IPAC_IDTAG_MACADDR)); |
| 205 | if (rc < 0) |
| 206 | goto out; |
| 207 | } |
| 208 | |
| 209 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_UNIT, 1)) |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 210 | rc = ipa_parse_unitid((char *) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 211 | TLVP_VAL(tp, IPAC_IDTAG_UNIT), ud); |
| 212 | |
| 213 | out: |
| 214 | return rc; |
| 215 | } |
| 216 | |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 217 | #define IPA_STRING_MAX 64 |
| 218 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 219 | /*! Generate IPA CCM ID RESP based on list of IEs |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 220 | * \param[in] dev Descriptor describing identity data for response |
| 221 | * \param[in] ies_req List of IEIs to include in response |
| 222 | * \param[in] num_ies_req Number of IEIs in \a ies_req |
| 223 | * \returns Message buffer with IPA CCM ID RESP */ |
| 224 | struct msgb *ipa_ccm_make_id_resp(const struct ipaccess_unit *dev, |
| 225 | const uint8_t *ies_req, unsigned int num_ies_req) |
| 226 | { |
| 227 | struct msgb *msg = ipa_msg_alloc(16); |
| 228 | char str[IPA_STRING_MAX]; |
| 229 | unsigned int i; |
| 230 | |
| 231 | if (!msg) |
| 232 | return NULL; |
| 233 | |
| 234 | *msgb_put(msg, 1) = IPAC_MSGT_ID_RESP; |
| 235 | |
| 236 | for (i = 0; i < num_ies_req; i++) { |
| 237 | uint8_t *tag; |
| 238 | |
| 239 | str[0] = '\0'; |
| 240 | switch (ies_req[i]) { |
| 241 | case IPAC_IDTAG_UNIT: |
| 242 | snprintf(str, sizeof(str), "%u/%u/%u", |
| 243 | dev->site_id, dev->bts_id, dev->trx_id); |
| 244 | break; |
| 245 | case IPAC_IDTAG_MACADDR: |
| 246 | snprintf(str, sizeof(str), |
| 247 | "%02x:%02x:%02x:%02x:%02x:%02x", |
| 248 | dev->mac_addr[0], dev->mac_addr[1], |
| 249 | dev->mac_addr[2], dev->mac_addr[3], |
| 250 | dev->mac_addr[4], dev->mac_addr[5]); |
| 251 | break; |
| 252 | case IPAC_IDTAG_LOCATION1: |
| 253 | if (dev->location1) |
Neels Hofmeyr | ebe4ef7 | 2018-07-26 17:12:07 +0200 | [diff] [blame] | 254 | osmo_strlcpy(str, dev->location1, sizeof(str)); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 255 | break; |
| 256 | case IPAC_IDTAG_LOCATION2: |
| 257 | if (dev->location2) |
Neels Hofmeyr | ebe4ef7 | 2018-07-26 17:12:07 +0200 | [diff] [blame] | 258 | osmo_strlcpy(str, dev->location2, sizeof(str)); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 259 | break; |
| 260 | case IPAC_IDTAG_EQUIPVERS: |
| 261 | if (dev->equipvers) |
Neels Hofmeyr | ebe4ef7 | 2018-07-26 17:12:07 +0200 | [diff] [blame] | 262 | osmo_strlcpy(str, dev->equipvers, sizeof(str)); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 263 | break; |
| 264 | case IPAC_IDTAG_SWVERSION: |
| 265 | if (dev->swversion) |
Neels Hofmeyr | ebe4ef7 | 2018-07-26 17:12:07 +0200 | [diff] [blame] | 266 | osmo_strlcpy(str, dev->swversion, sizeof(str)); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 267 | break; |
| 268 | case IPAC_IDTAG_UNITNAME: |
| 269 | if (dev->unit_name) { |
Neels Hofmeyr | ebe4ef7 | 2018-07-26 17:12:07 +0200 | [diff] [blame] | 270 | snprintf(str, sizeof(str), "%s", dev->unit_name); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 271 | } else { |
| 272 | snprintf(str, sizeof(str), |
| 273 | "%02x-%02x-%02x-%02x-%02x-%02x", |
| 274 | dev->mac_addr[0], dev->mac_addr[1], |
| 275 | dev->mac_addr[2], dev->mac_addr[3], |
| 276 | dev->mac_addr[4], dev->mac_addr[5]); |
| 277 | } |
| 278 | break; |
| 279 | case IPAC_IDTAG_SERNR: |
| 280 | if (dev->serno) |
Neels Hofmeyr | ebe4ef7 | 2018-07-26 17:12:07 +0200 | [diff] [blame] | 281 | osmo_strlcpy(str, dev->serno, sizeof(str)); |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 282 | break; |
| 283 | default: |
| 284 | LOGP(DLINP, LOGL_NOTICE, |
| 285 | "Unknown ipaccess tag 0x%02x\n", ies_req[i]); |
| 286 | msgb_free(msg); |
| 287 | return NULL; |
| 288 | } |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 289 | |
| 290 | LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", ies_req[i], str); |
| 291 | tag = msgb_put(msg, 3 + strlen(str) + 1); |
| 292 | tag[0] = 0x00; |
| 293 | tag[1] = 1 + strlen(str) + 1; |
| 294 | tag[2] = ies_req[1]; |
| 295 | memcpy(tag + 3, str, strlen(str) + 1); |
| 296 | } |
| 297 | ipa_prepend_header(msg, IPAC_PROTO_IPACCESS); |
| 298 | return msg; |
| 299 | } |
| 300 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 301 | /*! Generate IPA CCM ID RESP based on requets payload |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 302 | * \param[in] dev Descriptor describing identity data for response |
| 303 | * \param[in] data Payload of the IPA CCM ID GET request |
| 304 | * \param[in] len Length of \a data in octets |
| 305 | * \returns Message buffer with IPA CCM ID RESP */ |
| 306 | struct msgb *ipa_ccm_make_id_resp_from_req(const struct ipaccess_unit *dev, |
| 307 | const uint8_t *data, unsigned int len) |
| 308 | { |
| 309 | uint8_t ies[len/2]; |
| 310 | unsigned int num_ies = 0; |
| 311 | const uint8_t *cur = data; |
| 312 | |
Harald Welte | 8a4895c | 2017-04-27 10:25:10 +0200 | [diff] [blame] | 313 | memset(ies, 0, sizeof(ies)); |
| 314 | |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 315 | /* build a array of the IEIs */ |
| 316 | while (len >= 2) { |
| 317 | uint8_t t_len, t_tag; |
| 318 | len -= 2; |
| 319 | t_len = *cur++; |
| 320 | t_tag = *cur++; |
| 321 | |
| 322 | if (t_len > len + 1) { |
Thorsten Alteholz | 5a9dbf8 | 2018-04-08 19:13:25 +0200 | [diff] [blame] | 323 | 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] | 324 | break; |
| 325 | } |
| 326 | |
| 327 | ies[num_ies++] = t_tag; |
| 328 | |
| 329 | cur += t_len; |
Harald Welte | 0b2c0ec | 2018-04-16 22:53:48 +0200 | [diff] [blame] | 330 | /* prevent any unsigned integer underflow due to somebody sending us |
| 331 | * messages with wrong length values */ |
| 332 | if (len <= t_len) |
| 333 | len -= t_len; |
| 334 | else |
| 335 | len = 0; |
Harald Welte | 7bc88bb | 2017-04-15 19:05:33 +0200 | [diff] [blame] | 336 | } |
| 337 | return ipa_ccm_make_id_resp(dev, ies, num_ies); |
| 338 | } |
| 339 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 340 | int ipa_send(int fd, const void *msg, size_t msglen) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 341 | { |
| 342 | int ret; |
| 343 | |
| 344 | ret = write(fd, msg, msglen); |
| 345 | if (ret < 0) |
Jacob Erlbeck | a6be224 | 2014-12-22 10:58:46 +0100 | [diff] [blame] | 346 | return -errno; |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 347 | if (ret < msglen) { |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 348 | LOGP(DLINP, LOGL_ERROR, "ipa_send: short write\n"); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 349 | return -EIO; |
| 350 | } |
| 351 | return ret; |
| 352 | } |
| 353 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 354 | int ipa_ccm_send_pong(int fd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 355 | { |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 356 | return ipa_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg)); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 359 | int ipa_ccm_send_id_ack(int fd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 360 | { |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 361 | 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] | 362 | } |
| 363 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 364 | int ipa_ccm_send_id_req(int fd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 365 | { |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 366 | 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] | 367 | } |
| 368 | |
| 369 | /* base handling of the ip.access protocol */ |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 370 | int ipa_ccm_rcvmsg_base(struct msgb *msg, struct osmo_fd *bfd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 371 | { |
| 372 | uint8_t msg_type = *(msg->l2h); |
| 373 | int ret; |
| 374 | |
| 375 | switch (msg_type) { |
| 376 | case IPAC_MSGT_PING: |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 377 | ret = ipa_ccm_send_pong(bfd->fd); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 378 | if (ret < 0) { |
| 379 | LOGP(DLINP, LOGL_ERROR, "Cannot send PING " |
| 380 | "message. Reason: %s\n", strerror(errno)); |
| 381 | break; |
| 382 | } |
| 383 | ret = 1; |
| 384 | break; |
| 385 | case IPAC_MSGT_PONG: |
| 386 | DEBUGP(DLMI, "PONG!\n"); |
| 387 | ret = 1; |
| 388 | break; |
| 389 | case IPAC_MSGT_ID_ACK: |
| 390 | DEBUGP(DLMI, "ID_ACK? -> ACK!\n"); |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 391 | ret = ipa_ccm_send_id_ack(bfd->fd); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 392 | if (ret < 0) { |
| 393 | LOGP(DLINP, LOGL_ERROR, "Cannot send ID_ACK " |
| 394 | "message. Reason: %s\n", strerror(errno)); |
| 395 | break; |
| 396 | } |
| 397 | ret = 1; |
| 398 | break; |
| 399 | default: |
| 400 | /* This is not an IPA PING, PONG or ID_ACK message */ |
| 401 | ret = 0; |
| 402 | break; |
| 403 | } |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | /* base handling of the ip.access protocol */ |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 408 | 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] | 409 | { |
| 410 | uint8_t msg_type = *(msg->l2h); |
| 411 | int ret = 0; |
| 412 | |
| 413 | switch (msg_type) { |
| 414 | case IPAC_MSGT_PING: |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 415 | ret = ipa_ccm_send_pong(bfd->fd); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 416 | if (ret < 0) { |
| 417 | LOGP(DLINP, LOGL_ERROR, "Cannot send PONG " |
| 418 | "message. Reason: %s\n", strerror(errno)); |
| 419 | } |
| 420 | break; |
| 421 | case IPAC_MSGT_PONG: |
| 422 | DEBUGP(DLMI, "PONG!\n"); |
| 423 | break; |
| 424 | case IPAC_MSGT_ID_ACK: |
| 425 | DEBUGP(DLMI, "ID_ACK\n"); |
| 426 | break; |
| 427 | } |
| 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 432 | void ipa_prepend_header_ext(struct msgb *msg, int proto) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 433 | { |
| 434 | struct ipaccess_head_ext *hh_ext; |
| 435 | |
| 436 | /* prepend the osmo ip.access header extension */ |
| 437 | hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext)); |
| 438 | hh_ext->proto = proto; |
| 439 | } |
| 440 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 441 | void ipa_prepend_header(struct msgb *msg, int proto) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 442 | { |
| 443 | struct ipaccess_head *hh; |
| 444 | |
| 445 | /* prepend the ip.access header */ |
| 446 | hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh)); |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 447 | hh->len = osmo_htons(msg->len - sizeof(*hh)); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 448 | hh->proto = proto; |
| 449 | } |
| 450 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 451 | #ifdef HAVE_SYS_SOCKET_H |
| 452 | #include <sys/socket.h> |
| 453 | |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 454 | int ipa_msg_recv(int fd, struct msgb **rmsg) |
| 455 | { |
| 456 | int rc = ipa_msg_recv_buffered(fd, rmsg, NULL); |
| 457 | if (rc < 0) { |
| 458 | errno = -rc; |
| 459 | rc = -1; |
| 460 | } |
| 461 | return rc; |
| 462 | } |
| 463 | |
| 464 | int ipa_msg_recv_buffered(int fd, struct msgb **rmsg, struct msgb **tmp_msg) |
| 465 | { |
| 466 | struct msgb *msg = tmp_msg ? *tmp_msg : NULL; |
| 467 | struct ipaccess_head *hh; |
| 468 | int len, ret; |
| 469 | int needed; |
| 470 | |
| 471 | if (msg == NULL) { |
| 472 | msg = ipa_msg_alloc(0); |
| 473 | if (msg == NULL) { |
| 474 | ret = -ENOMEM; |
| 475 | goto discard_msg; |
| 476 | } |
| 477 | msg->l1h = msg->tail; |
| 478 | } |
| 479 | |
| 480 | if (msg->l2h == NULL) { |
| 481 | /* first read our 3-byte header */ |
| 482 | needed = sizeof(*hh) - msg->len; |
| 483 | ret = recv(fd, msg->tail, needed, 0); |
| 484 | if (ret == 0) |
| 485 | goto discard_msg; |
| 486 | |
| 487 | if (ret < 0) { |
| 488 | if (errno == EAGAIN || errno == EINTR) |
| 489 | ret = 0; |
| 490 | else { |
| 491 | ret = -errno; |
| 492 | goto discard_msg; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | msgb_put(msg, ret); |
| 497 | |
| 498 | if (ret < needed) { |
| 499 | if (msg->len == 0) { |
| 500 | ret = -EAGAIN; |
| 501 | goto discard_msg; |
| 502 | } |
| 503 | |
| 504 | LOGP(DLINP, LOGL_INFO, |
Harald Welte | f196a02 | 2014-08-21 09:42:03 +0200 | [diff] [blame] | 505 | "Received part of IPA message header (%d/%zu)\n", |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 506 | msg->len, sizeof(*hh)); |
| 507 | if (!tmp_msg) { |
| 508 | ret = -EIO; |
| 509 | goto discard_msg; |
| 510 | } |
| 511 | *tmp_msg = msg; |
| 512 | return -EAGAIN; |
| 513 | } |
| 514 | |
| 515 | msg->l2h = msg->tail; |
| 516 | } |
| 517 | |
| 518 | hh = (struct ipaccess_head *) msg->data; |
| 519 | |
| 520 | /* then read the length as specified in header */ |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 521 | len = osmo_ntohs(hh->len); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 522 | |
| 523 | if (len < 0 || IPA_ALLOC_SIZE < len + sizeof(*hh)) { |
| 524 | LOGP(DLINP, LOGL_ERROR, "bad message length of %d bytes, " |
| 525 | "received %d bytes\n", len, msg->len); |
| 526 | ret = -EIO; |
| 527 | goto discard_msg; |
| 528 | } |
| 529 | |
| 530 | needed = len - msgb_l2len(msg); |
| 531 | |
| 532 | if (needed > 0) { |
| 533 | ret = recv(fd, msg->tail, needed, 0); |
| 534 | |
| 535 | if (ret == 0) |
| 536 | goto discard_msg; |
| 537 | |
| 538 | if (ret < 0) { |
| 539 | if (errno == EAGAIN || errno == EINTR) |
| 540 | ret = 0; |
| 541 | else { |
| 542 | ret = -errno; |
| 543 | goto discard_msg; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | msgb_put(msg, ret); |
| 548 | |
| 549 | if (ret < needed) { |
| 550 | LOGP(DLINP, LOGL_INFO, |
| 551 | "Received part of IPA message L2 data (%d/%d)\n", |
| 552 | msgb_l2len(msg), len); |
| 553 | if (!tmp_msg) { |
| 554 | ret = -EIO; |
| 555 | goto discard_msg; |
| 556 | } |
| 557 | *tmp_msg = msg; |
| 558 | return -EAGAIN; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | ret = msgb_l2len(msg); |
| 563 | |
| 564 | if (ret == 0) { |
| 565 | LOGP(DLINP, LOGL_INFO, |
| 566 | "Discarding IPA message without payload\n"); |
| 567 | ret = -EAGAIN; |
| 568 | goto discard_msg; |
| 569 | } |
| 570 | |
| 571 | if (tmp_msg) |
| 572 | *tmp_msg = NULL; |
| 573 | *rmsg = msg; |
| 574 | return ret; |
| 575 | |
| 576 | discard_msg: |
| 577 | if (tmp_msg) |
| 578 | *tmp_msg = NULL; |
| 579 | msgb_free(msg); |
| 580 | return ret; |
| 581 | } |
| 582 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 583 | #endif /* SYS_SOCKET_H */ |
| 584 | |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 585 | struct msgb *ipa_msg_alloc(int headroom) |
| 586 | { |
| 587 | struct msgb *nmsg; |
| 588 | |
| 589 | headroom += sizeof(struct ipaccess_head); |
| 590 | |
Neels Hofmeyr | 889ab16 | 2017-09-07 20:41:12 +0200 | [diff] [blame] | 591 | nmsg = msgb_alloc_headroom(1200 + headroom, headroom, "IPA Multiplex"); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 592 | if (!nmsg) |
| 593 | return NULL; |
| 594 | return nmsg; |
| 595 | } |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 596 | |
| 597 | /*! @} */ |