Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 1 | /* OpenBSC Abis input driver for ip.access */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2010 by Holger Hans Peter Freyther |
| 5 | * (C) 2010 by On-Waves |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | fd5ad17 | 2014-10-26 20:47:42 +0100 | [diff] [blame] | 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | fd5ad17 | 2014-10-26 20:47:42 +0100 | [diff] [blame] | 17 | * GNU General Public License for more details. |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 18 | * |
Harald Welte | fd5ad17 | 2014-10-26 20:47:42 +0100 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <unistd.h> |
| 25 | #include <stdint.h> |
| 26 | #include <errno.h> |
| 27 | #include <stdlib.h> |
| 28 | |
| 29 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | ee6652b | 2015-11-09 16:21:19 +0000 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <sys/socket.h> |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 32 | |
| 33 | #include <osmocom/core/msgb.h> |
| 34 | #include <osmocom/core/talloc.h> |
| 35 | #include <osmocom/core/logging.h> |
| 36 | #include <osmocom/core/macaddr.h> |
| 37 | #include <osmocom/core/select.h> |
| 38 | |
| 39 | #include <osmocom/gsm/tlv.h> |
| 40 | #include <osmocom/gsm/protocol/ipaccess.h> |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 41 | #include <osmocom/gsm/ipa.h> |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 42 | |
| 43 | #define IPA_ALLOC_SIZE 1200 |
| 44 | |
| 45 | /* |
| 46 | * Common propietary IPA messages: |
| 47 | * - PONG: in reply to PING. |
| 48 | * - ID_REQUEST: first messages once OML has been established. |
| 49 | * - ID_ACK: in reply to ID_ACK. |
| 50 | */ |
| 51 | static const uint8_t ipa_pong_msg[] = { |
| 52 | 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG |
| 53 | }; |
| 54 | |
| 55 | static const uint8_t ipa_id_ack_msg[] = { |
| 56 | 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK |
| 57 | }; |
| 58 | |
| 59 | static const uint8_t ipa_id_req_msg[] = { |
| 60 | 0, 17, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_GET, |
| 61 | 0x01, IPAC_IDTAG_UNIT, |
| 62 | 0x01, IPAC_IDTAG_MACADDR, |
| 63 | 0x01, IPAC_IDTAG_LOCATION1, |
| 64 | 0x01, IPAC_IDTAG_LOCATION2, |
| 65 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 66 | 0x01, IPAC_IDTAG_SWVERSION, |
| 67 | 0x01, IPAC_IDTAG_UNITNAME, |
| 68 | 0x01, IPAC_IDTAG_SERNR, |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | static const char *idtag_names[] = { |
| 73 | [IPAC_IDTAG_SERNR] = "Serial_Number", |
| 74 | [IPAC_IDTAG_UNITNAME] = "Unit_Name", |
| 75 | [IPAC_IDTAG_LOCATION1] = "Location_1", |
| 76 | [IPAC_IDTAG_LOCATION2] = "Location_2", |
| 77 | [IPAC_IDTAG_EQUIPVERS] = "Equipment_Version", |
| 78 | [IPAC_IDTAG_SWVERSION] = "Software_Version", |
| 79 | [IPAC_IDTAG_IPADDR] = "IP_Address", |
| 80 | [IPAC_IDTAG_MACADDR] = "MAC_Address", |
| 81 | [IPAC_IDTAG_UNIT] = "Unit_ID", |
| 82 | }; |
| 83 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 84 | const char *ipa_ccm_idtag_name(uint8_t tag) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 85 | { |
| 86 | if (tag >= ARRAY_SIZE(idtag_names)) |
| 87 | return "unknown"; |
| 88 | |
| 89 | return idtag_names[tag]; |
| 90 | } |
| 91 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 92 | 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] | 93 | { |
Holger Hans Peter Freyther | f558ed4 | 2015-06-02 15:52:06 +0200 | [diff] [blame] | 94 | return ipa_ccm_idtag_parse_off(dec, buf, len, 0); |
| 95 | } |
| 96 | |
| 97 | int ipa_ccm_idtag_parse_off(struct tlv_parsed *dec, unsigned char *buf, int len, const int len_offset) |
| 98 | { |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 99 | uint8_t t_len; |
| 100 | uint8_t t_tag; |
| 101 | uint8_t *cur = buf; |
| 102 | |
| 103 | memset(dec, 0, sizeof(*dec)); |
| 104 | |
| 105 | while (len >= 2) { |
| 106 | len -= 2; |
| 107 | t_len = *cur++; |
| 108 | t_tag = *cur++; |
| 109 | |
Holger Hans Peter Freyther | f558ed4 | 2015-06-02 15:52:06 +0200 | [diff] [blame] | 110 | if (t_len < len_offset) { |
| 111 | LOGP(DLMI, LOGL_ERROR, "minimal offset not included: %d\n", t_len); |
| 112 | return -EINVAL; |
| 113 | } |
| 114 | |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 115 | if (t_len > len + 1) { |
| 116 | LOGP(DLMI, LOGL_ERROR, "The tag does not fit: %d\n", t_len); |
| 117 | return -EINVAL; |
| 118 | } |
| 119 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 120 | DEBUGPC(DLMI, "%s='%s' ", ipa_ccm_idtag_name(t_tag), cur); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 121 | |
Holger Hans Peter Freyther | f558ed4 | 2015-06-02 15:52:06 +0200 | [diff] [blame] | 122 | dec->lv[t_tag].len = t_len - len_offset; |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 123 | dec->lv[t_tag].val = cur; |
| 124 | |
Holger Hans Peter Freyther | f558ed4 | 2015-06-02 15:52:06 +0200 | [diff] [blame] | 125 | cur += t_len - len_offset; |
| 126 | len -= t_len - len_offset; |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 127 | } |
| 128 | return 0; |
| 129 | } |
| 130 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 131 | int ipa_parse_unitid(const char *str, struct ipaccess_unit *unit_data) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 132 | { |
| 133 | unsigned long ul; |
| 134 | char *endptr; |
| 135 | const char *nptr; |
| 136 | |
| 137 | nptr = str; |
| 138 | ul = strtoul(nptr, &endptr, 10); |
| 139 | if (endptr <= nptr) |
| 140 | return -EINVAL; |
| 141 | unit_data->site_id = ul & 0xffff; |
| 142 | |
| 143 | if (*endptr++ != '/') |
| 144 | return -EINVAL; |
| 145 | |
| 146 | nptr = endptr; |
| 147 | ul = strtoul(nptr, &endptr, 10); |
| 148 | if (endptr <= nptr) |
| 149 | return -EINVAL; |
| 150 | unit_data->bts_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->trx_id = ul & 0xffff; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 164 | int ipa_ccm_tlv_to_unitdata(struct ipaccess_unit *ud, |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 165 | const struct tlv_parsed *tp) |
| 166 | { |
| 167 | int rc = 0; |
| 168 | |
| 169 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_SERNR, 1)) |
| 170 | ud->serno = talloc_strdup(ud, (char *) |
| 171 | TLVP_VAL(tp, IPAC_IDTAG_SERNR)); |
| 172 | |
| 173 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_UNITNAME, 1)) |
| 174 | ud->unit_name = talloc_strdup(ud, (char *) |
| 175 | TLVP_VAL(tp, IPAC_IDTAG_UNITNAME)); |
| 176 | |
| 177 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_LOCATION1, 1)) |
| 178 | ud->location1 = talloc_strdup(ud, (char *) |
| 179 | TLVP_VAL(tp, IPAC_IDTAG_LOCATION1)); |
| 180 | |
| 181 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_LOCATION2, 1)) |
| 182 | ud->location2 = talloc_strdup(ud, (char *) |
| 183 | TLVP_VAL(tp, IPAC_IDTAG_LOCATION2)); |
| 184 | |
| 185 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_EQUIPVERS, 1)) |
| 186 | ud->equipvers = talloc_strdup(ud, (char *) |
| 187 | TLVP_VAL(tp, IPAC_IDTAG_EQUIPVERS)); |
| 188 | |
| 189 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_SWVERSION, 1)) |
| 190 | ud->swversion = talloc_strdup(ud, (char *) |
| 191 | TLVP_VAL(tp, IPAC_IDTAG_SWVERSION)); |
| 192 | |
| 193 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_MACADDR, 17)) { |
| 194 | rc = osmo_macaddr_parse(ud->mac_addr, (char *) |
| 195 | TLVP_VAL(tp, IPAC_IDTAG_MACADDR)); |
| 196 | if (rc < 0) |
| 197 | goto out; |
| 198 | } |
| 199 | |
| 200 | if (TLVP_PRES_LEN(tp, IPAC_IDTAG_UNIT, 1)) |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 201 | rc = ipa_parse_unitid((char *) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 202 | TLVP_VAL(tp, IPAC_IDTAG_UNIT), ud); |
| 203 | |
| 204 | out: |
| 205 | return rc; |
| 206 | } |
| 207 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 208 | int ipa_send(int fd, const void *msg, size_t msglen) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 209 | { |
| 210 | int ret; |
| 211 | |
| 212 | ret = write(fd, msg, msglen); |
| 213 | if (ret < 0) |
Jacob Erlbeck | a6be224 | 2014-12-22 10:58:46 +0100 | [diff] [blame] | 214 | return -errno; |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 215 | if (ret < msglen) { |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 216 | LOGP(DLINP, LOGL_ERROR, "ipa_send: short write\n"); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 217 | return -EIO; |
| 218 | } |
| 219 | return ret; |
| 220 | } |
| 221 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 222 | int ipa_ccm_send_pong(int fd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 223 | { |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 224 | return ipa_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg)); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 227 | int ipa_ccm_send_id_ack(int fd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 228 | { |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 229 | 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] | 230 | } |
| 231 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 232 | int ipa_ccm_send_id_req(int fd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 233 | { |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 234 | 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] | 235 | } |
| 236 | |
| 237 | /* base handling of the ip.access protocol */ |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 238 | int ipa_ccm_rcvmsg_base(struct msgb *msg, struct osmo_fd *bfd) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 239 | { |
| 240 | uint8_t msg_type = *(msg->l2h); |
| 241 | int ret; |
| 242 | |
| 243 | switch (msg_type) { |
| 244 | case IPAC_MSGT_PING: |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 245 | ret = ipa_ccm_send_pong(bfd->fd); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 246 | if (ret < 0) { |
| 247 | LOGP(DLINP, LOGL_ERROR, "Cannot send PING " |
| 248 | "message. Reason: %s\n", strerror(errno)); |
| 249 | break; |
| 250 | } |
| 251 | ret = 1; |
| 252 | break; |
| 253 | case IPAC_MSGT_PONG: |
| 254 | DEBUGP(DLMI, "PONG!\n"); |
| 255 | ret = 1; |
| 256 | break; |
| 257 | case IPAC_MSGT_ID_ACK: |
| 258 | DEBUGP(DLMI, "ID_ACK? -> ACK!\n"); |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 259 | ret = ipa_ccm_send_id_ack(bfd->fd); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 260 | if (ret < 0) { |
| 261 | LOGP(DLINP, LOGL_ERROR, "Cannot send ID_ACK " |
| 262 | "message. Reason: %s\n", strerror(errno)); |
| 263 | break; |
| 264 | } |
| 265 | ret = 1; |
| 266 | break; |
| 267 | default: |
| 268 | /* This is not an IPA PING, PONG or ID_ACK message */ |
| 269 | ret = 0; |
| 270 | break; |
| 271 | } |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | /* base handling of the ip.access protocol */ |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 276 | 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] | 277 | { |
| 278 | uint8_t msg_type = *(msg->l2h); |
| 279 | int ret = 0; |
| 280 | |
| 281 | switch (msg_type) { |
| 282 | case IPAC_MSGT_PING: |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 283 | ret = ipa_ccm_send_pong(bfd->fd); |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 284 | if (ret < 0) { |
| 285 | LOGP(DLINP, LOGL_ERROR, "Cannot send PONG " |
| 286 | "message. Reason: %s\n", strerror(errno)); |
| 287 | } |
| 288 | break; |
| 289 | case IPAC_MSGT_PONG: |
| 290 | DEBUGP(DLMI, "PONG!\n"); |
| 291 | break; |
| 292 | case IPAC_MSGT_ID_ACK: |
| 293 | DEBUGP(DLMI, "ID_ACK\n"); |
| 294 | break; |
| 295 | } |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 300 | void ipa_prepend_header_ext(struct msgb *msg, int proto) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 301 | { |
| 302 | struct ipaccess_head_ext *hh_ext; |
| 303 | |
| 304 | /* prepend the osmo ip.access header extension */ |
| 305 | hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext)); |
| 306 | hh_ext->proto = proto; |
| 307 | } |
| 308 | |
Harald Welte | e391996 | 2014-08-20 22:28:23 +0200 | [diff] [blame] | 309 | void ipa_prepend_header(struct msgb *msg, int proto) |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 310 | { |
| 311 | struct ipaccess_head *hh; |
| 312 | |
| 313 | /* prepend the ip.access header */ |
| 314 | hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh)); |
| 315 | hh->len = htons(msg->len - sizeof(*hh)); |
| 316 | hh->proto = proto; |
| 317 | } |
| 318 | |
| 319 | int ipa_msg_recv(int fd, struct msgb **rmsg) |
| 320 | { |
| 321 | int rc = ipa_msg_recv_buffered(fd, rmsg, NULL); |
| 322 | if (rc < 0) { |
| 323 | errno = -rc; |
| 324 | rc = -1; |
| 325 | } |
| 326 | return rc; |
| 327 | } |
| 328 | |
| 329 | int ipa_msg_recv_buffered(int fd, struct msgb **rmsg, struct msgb **tmp_msg) |
| 330 | { |
| 331 | struct msgb *msg = tmp_msg ? *tmp_msg : NULL; |
| 332 | struct ipaccess_head *hh; |
| 333 | int len, ret; |
| 334 | int needed; |
| 335 | |
| 336 | if (msg == NULL) { |
| 337 | msg = ipa_msg_alloc(0); |
| 338 | if (msg == NULL) { |
| 339 | ret = -ENOMEM; |
| 340 | goto discard_msg; |
| 341 | } |
| 342 | msg->l1h = msg->tail; |
| 343 | } |
| 344 | |
| 345 | if (msg->l2h == NULL) { |
| 346 | /* first read our 3-byte header */ |
| 347 | needed = sizeof(*hh) - msg->len; |
| 348 | ret = recv(fd, msg->tail, needed, 0); |
| 349 | if (ret == 0) |
| 350 | goto discard_msg; |
| 351 | |
| 352 | if (ret < 0) { |
| 353 | if (errno == EAGAIN || errno == EINTR) |
| 354 | ret = 0; |
| 355 | else { |
| 356 | ret = -errno; |
| 357 | goto discard_msg; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | msgb_put(msg, ret); |
| 362 | |
| 363 | if (ret < needed) { |
| 364 | if (msg->len == 0) { |
| 365 | ret = -EAGAIN; |
| 366 | goto discard_msg; |
| 367 | } |
| 368 | |
| 369 | LOGP(DLINP, LOGL_INFO, |
Harald Welte | f196a02 | 2014-08-21 09:42:03 +0200 | [diff] [blame] | 370 | "Received part of IPA message header (%d/%zu)\n", |
Harald Welte | 28aa991 | 2014-08-20 22:06:04 +0200 | [diff] [blame] | 371 | msg->len, sizeof(*hh)); |
| 372 | if (!tmp_msg) { |
| 373 | ret = -EIO; |
| 374 | goto discard_msg; |
| 375 | } |
| 376 | *tmp_msg = msg; |
| 377 | return -EAGAIN; |
| 378 | } |
| 379 | |
| 380 | msg->l2h = msg->tail; |
| 381 | } |
| 382 | |
| 383 | hh = (struct ipaccess_head *) msg->data; |
| 384 | |
| 385 | /* then read the length as specified in header */ |
| 386 | len = ntohs(hh->len); |
| 387 | |
| 388 | if (len < 0 || IPA_ALLOC_SIZE < len + sizeof(*hh)) { |
| 389 | LOGP(DLINP, LOGL_ERROR, "bad message length of %d bytes, " |
| 390 | "received %d bytes\n", len, msg->len); |
| 391 | ret = -EIO; |
| 392 | goto discard_msg; |
| 393 | } |
| 394 | |
| 395 | needed = len - msgb_l2len(msg); |
| 396 | |
| 397 | if (needed > 0) { |
| 398 | ret = recv(fd, msg->tail, needed, 0); |
| 399 | |
| 400 | if (ret == 0) |
| 401 | goto discard_msg; |
| 402 | |
| 403 | if (ret < 0) { |
| 404 | if (errno == EAGAIN || errno == EINTR) |
| 405 | ret = 0; |
| 406 | else { |
| 407 | ret = -errno; |
| 408 | goto discard_msg; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | msgb_put(msg, ret); |
| 413 | |
| 414 | if (ret < needed) { |
| 415 | LOGP(DLINP, LOGL_INFO, |
| 416 | "Received part of IPA message L2 data (%d/%d)\n", |
| 417 | msgb_l2len(msg), len); |
| 418 | if (!tmp_msg) { |
| 419 | ret = -EIO; |
| 420 | goto discard_msg; |
| 421 | } |
| 422 | *tmp_msg = msg; |
| 423 | return -EAGAIN; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | ret = msgb_l2len(msg); |
| 428 | |
| 429 | if (ret == 0) { |
| 430 | LOGP(DLINP, LOGL_INFO, |
| 431 | "Discarding IPA message without payload\n"); |
| 432 | ret = -EAGAIN; |
| 433 | goto discard_msg; |
| 434 | } |
| 435 | |
| 436 | if (tmp_msg) |
| 437 | *tmp_msg = NULL; |
| 438 | *rmsg = msg; |
| 439 | return ret; |
| 440 | |
| 441 | discard_msg: |
| 442 | if (tmp_msg) |
| 443 | *tmp_msg = NULL; |
| 444 | msgb_free(msg); |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | struct msgb *ipa_msg_alloc(int headroom) |
| 449 | { |
| 450 | struct msgb *nmsg; |
| 451 | |
| 452 | headroom += sizeof(struct ipaccess_head); |
| 453 | |
| 454 | nmsg = msgb_alloc_headroom(1200 + headroom, headroom, "Abis/IP"); |
| 455 | if (!nmsg) |
| 456 | return NULL; |
| 457 | return nmsg; |
| 458 | } |