blob: e03f61531efc57b29cc68efe5abfbd1c0815cea1 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file ipa.c
2 * OpenBSC Abis input driver for ip.access */
3/*
4 * (C) 2009-2017 by Harald Welte <laforge@gnumonks.org>
Harald Welte28aa9912014-08-20 22:06:04 +02005 * (C) 2010 by Holger Hans Peter Freyther
6 * (C) 2010 by On-Waves
7 *
8 * All Rights Reserved
9 *
Harald Weltee08da972017-11-13 01:00:26 +090010 * SPDX-License-Identifier: GPL-2.0+
11 *
Harald Welte28aa9912014-08-20 22:06:04 +020012 * This program is free software; you can redistribute it and/or modify
Harald Weltefd5ad172014-10-26 20:47:42 +010013 * 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 Welte28aa9912014-08-20 22:06:04 +020015 * (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 Weltefd5ad172014-10-26 20:47:42 +010020 * GNU General Public License for more details.
Harald Welte28aa9912014-08-20 22:06:04 +020021 *
Harald Weltefd5ad172014-10-26 20:47:42 +010022 * You should have received a copy of the GNU General Public License
Harald Welte28aa9912014-08-20 22:06:04 +020023 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 */
26
Harald Welte20725b92017-05-15 12:50:04 +020027#include "config.h"
28
Harald Welte28aa9912014-08-20 22:06:04 +020029#include <unistd.h>
30#include <stdint.h>
31#include <errno.h>
32#include <stdlib.h>
33
Holger Hans Peter Freytheree6652b2015-11-09 16:21:19 +000034#include <sys/types.h>
Harald Welte28aa9912014-08-20 22:06:04 +020035
Harald Welte95871da2017-05-15 12:11:36 +020036#include <osmocom/core/byteswap.h>
Harald Welte28aa9912014-08-20 22:06:04 +020037#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 Weltee3919962014-08-20 22:28:23 +020045#include <osmocom/gsm/ipa.h>
Harald Welte28aa9912014-08-20 22:06:04 +020046
Harald Welte96e2a002017-06-12 21:44:18 +020047/*! \addtogroup ipa
48 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020049 * IPA Multiplex utility routines
Harald Welte96e2a002017-06-12 21:44:18 +020050 */
51
Harald Welte28aa9912014-08-20 22:06:04 +020052#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 */
60static const uint8_t ipa_pong_msg[] = {
61 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG
62};
63
64static const uint8_t ipa_id_ack_msg[] = {
65 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
66};
67
68static 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
81static 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 Weltee3919962014-08-20 22:28:23 +020093const char *ipa_ccm_idtag_name(uint8_t tag)
Harald Welte28aa9912014-08-20 22:06:04 +020094{
95 if (tag >= ARRAY_SIZE(idtag_names))
96 return "unknown";
97
98 return idtag_names[tag];
99}
100
Harald Weltee3919962014-08-20 22:28:23 +0200101int ipa_ccm_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
Harald Welte28aa9912014-08-20 22:06:04 +0200102{
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +0200103 return ipa_ccm_idtag_parse_off(dec, buf, len, 0);
104}
105
106int ipa_ccm_idtag_parse_off(struct tlv_parsed *dec, unsigned char *buf, int len, const int len_offset)
107{
Harald Welte28aa9912014-08-20 22:06:04 +0200108 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 Freytherf558ed42015-06-02 15:52:06 +0200119 if (t_len < len_offset) {
Max9b4d0652016-11-15 19:21:23 +0100120 LOGP(DLMI, LOGL_ERROR, "minimal offset not included: %d < %d\n", t_len, len_offset);
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +0200121 return -EINVAL;
122 }
123
Harald Welte28aa9912014-08-20 22:06:04 +0200124 if (t_len > len + 1) {
Max9b4d0652016-11-15 19:21:23 +0100125 LOGP(DLMI, LOGL_ERROR, "The tag does not fit: %d > %d\n", t_len, len + 1);
Harald Welte28aa9912014-08-20 22:06:04 +0200126 return -EINVAL;
127 }
128
Harald Weltee3919962014-08-20 22:28:23 +0200129 DEBUGPC(DLMI, "%s='%s' ", ipa_ccm_idtag_name(t_tag), cur);
Harald Welte28aa9912014-08-20 22:06:04 +0200130
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +0200131 dec->lv[t_tag].len = t_len - len_offset;
Harald Welte28aa9912014-08-20 22:06:04 +0200132 dec->lv[t_tag].val = cur;
133
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +0200134 cur += t_len - len_offset;
135 len -= t_len - len_offset;
Harald Welte28aa9912014-08-20 22:06:04 +0200136 }
137 return 0;
138}
139
Harald Weltee3919962014-08-20 22:28:23 +0200140int ipa_parse_unitid(const char *str, struct ipaccess_unit *unit_data)
Harald Welte28aa9912014-08-20 22:06:04 +0200141{
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 Weltee3919962014-08-20 22:28:23 +0200173int ipa_ccm_tlv_to_unitdata(struct ipaccess_unit *ud,
Harald Welte28aa9912014-08-20 22:06:04 +0200174 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 Weltee3919962014-08-20 22:28:23 +0200210 rc = ipa_parse_unitid((char *)
Harald Welte28aa9912014-08-20 22:06:04 +0200211 TLVP_VAL(tp, IPAC_IDTAG_UNIT), ud);
212
213out:
214 return rc;
215}
216
Harald Welte7bc88bb2017-04-15 19:05:33 +0200217#define IPA_STRING_MAX 64
218
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200219/*! Generate IPA CCM ID RESP based on list of IEs
Harald Welte7bc88bb2017-04-15 19:05:33 +0200220 * \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 */
224struct 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)
254 strncpy(str, dev->location1, IPA_STRING_MAX);
255 break;
256 case IPAC_IDTAG_LOCATION2:
257 if (dev->location2)
258 strncpy(str, dev->location2, IPA_STRING_MAX);
259 break;
260 case IPAC_IDTAG_EQUIPVERS:
261 if (dev->equipvers)
262 strncpy(str, dev->equipvers, IPA_STRING_MAX);
263 break;
264 case IPAC_IDTAG_SWVERSION:
265 if (dev->swversion)
266 strncpy(str, dev->swversion, IPA_STRING_MAX);
267 break;
268 case IPAC_IDTAG_UNITNAME:
269 if (dev->unit_name) {
270 snprintf(str, sizeof(str), dev->unit_name, IPA_STRING_MAX);
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)
281 strncpy(str, dev->serno, IPA_STRING_MAX);
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 }
289 str[IPA_STRING_MAX-1] = '\0';
290
291 LOGP(DLINP, LOGL_INFO, " tag %d: %s\n", ies_req[i], str);
292 tag = msgb_put(msg, 3 + strlen(str) + 1);
293 tag[0] = 0x00;
294 tag[1] = 1 + strlen(str) + 1;
295 tag[2] = ies_req[1];
296 memcpy(tag + 3, str, strlen(str) + 1);
297 }
298 ipa_prepend_header(msg, IPAC_PROTO_IPACCESS);
299 return msg;
300}
301
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200302/*! Generate IPA CCM ID RESP based on requets payload
Harald Welte7bc88bb2017-04-15 19:05:33 +0200303 * \param[in] dev Descriptor describing identity data for response
304 * \param[in] data Payload of the IPA CCM ID GET request
305 * \param[in] len Length of \a data in octets
306 * \returns Message buffer with IPA CCM ID RESP */
307struct msgb *ipa_ccm_make_id_resp_from_req(const struct ipaccess_unit *dev,
308 const uint8_t *data, unsigned int len)
309{
310 uint8_t ies[len/2];
311 unsigned int num_ies = 0;
312 const uint8_t *cur = data;
313
Harald Welte8a4895c2017-04-27 10:25:10 +0200314 memset(ies, 0, sizeof(ies));
315
Harald Welte7bc88bb2017-04-15 19:05:33 +0200316 /* build a array of the IEIs */
317 while (len >= 2) {
318 uint8_t t_len, t_tag;
319 len -= 2;
320 t_len = *cur++;
321 t_tag = *cur++;
322
323 if (t_len > len + 1) {
Thorsten Alteholz5a9dbf82018-04-08 19:13:25 +0200324 LOGP(DLINP, LOGL_ERROR, "IPA CCM tag 0x%02x does not fit\n", t_tag);
Harald Welte7bc88bb2017-04-15 19:05:33 +0200325 break;
326 }
327
328 ies[num_ies++] = t_tag;
329
330 cur += t_len;
331 len -= t_len;
332 }
333 return ipa_ccm_make_id_resp(dev, ies, num_ies);
334}
335
Harald Weltee3919962014-08-20 22:28:23 +0200336int ipa_send(int fd, const void *msg, size_t msglen)
Harald Welte28aa9912014-08-20 22:06:04 +0200337{
338 int ret;
339
340 ret = write(fd, msg, msglen);
341 if (ret < 0)
Jacob Erlbecka6be2242014-12-22 10:58:46 +0100342 return -errno;
Harald Welte28aa9912014-08-20 22:06:04 +0200343 if (ret < msglen) {
Harald Weltee3919962014-08-20 22:28:23 +0200344 LOGP(DLINP, LOGL_ERROR, "ipa_send: short write\n");
Harald Welte28aa9912014-08-20 22:06:04 +0200345 return -EIO;
346 }
347 return ret;
348}
349
Harald Weltee3919962014-08-20 22:28:23 +0200350int ipa_ccm_send_pong(int fd)
Harald Welte28aa9912014-08-20 22:06:04 +0200351{
Harald Weltee3919962014-08-20 22:28:23 +0200352 return ipa_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg));
Harald Welte28aa9912014-08-20 22:06:04 +0200353}
354
Harald Weltee3919962014-08-20 22:28:23 +0200355int ipa_ccm_send_id_ack(int fd)
Harald Welte28aa9912014-08-20 22:06:04 +0200356{
Harald Weltee3919962014-08-20 22:28:23 +0200357 return ipa_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg));
Harald Welte28aa9912014-08-20 22:06:04 +0200358}
359
Harald Weltee3919962014-08-20 22:28:23 +0200360int ipa_ccm_send_id_req(int fd)
Harald Welte28aa9912014-08-20 22:06:04 +0200361{
Harald Weltee3919962014-08-20 22:28:23 +0200362 return ipa_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg));
Harald Welte28aa9912014-08-20 22:06:04 +0200363}
364
365/* base handling of the ip.access protocol */
Harald Weltee3919962014-08-20 22:28:23 +0200366int ipa_ccm_rcvmsg_base(struct msgb *msg, struct osmo_fd *bfd)
Harald Welte28aa9912014-08-20 22:06:04 +0200367{
368 uint8_t msg_type = *(msg->l2h);
369 int ret;
370
371 switch (msg_type) {
372 case IPAC_MSGT_PING:
Harald Weltee3919962014-08-20 22:28:23 +0200373 ret = ipa_ccm_send_pong(bfd->fd);
Harald Welte28aa9912014-08-20 22:06:04 +0200374 if (ret < 0) {
375 LOGP(DLINP, LOGL_ERROR, "Cannot send PING "
376 "message. Reason: %s\n", strerror(errno));
377 break;
378 }
379 ret = 1;
380 break;
381 case IPAC_MSGT_PONG:
382 DEBUGP(DLMI, "PONG!\n");
383 ret = 1;
384 break;
385 case IPAC_MSGT_ID_ACK:
386 DEBUGP(DLMI, "ID_ACK? -> ACK!\n");
Harald Weltee3919962014-08-20 22:28:23 +0200387 ret = ipa_ccm_send_id_ack(bfd->fd);
Harald Welte28aa9912014-08-20 22:06:04 +0200388 if (ret < 0) {
389 LOGP(DLINP, LOGL_ERROR, "Cannot send ID_ACK "
390 "message. Reason: %s\n", strerror(errno));
391 break;
392 }
393 ret = 1;
394 break;
395 default:
396 /* This is not an IPA PING, PONG or ID_ACK message */
397 ret = 0;
398 break;
399 }
400 return ret;
401}
402
403/* base handling of the ip.access protocol */
Harald Weltee3919962014-08-20 22:28:23 +0200404int ipa_ccm_rcvmsg_bts_base(struct msgb *msg, struct osmo_fd *bfd)
Harald Welte28aa9912014-08-20 22:06:04 +0200405{
406 uint8_t msg_type = *(msg->l2h);
407 int ret = 0;
408
409 switch (msg_type) {
410 case IPAC_MSGT_PING:
Harald Weltee3919962014-08-20 22:28:23 +0200411 ret = ipa_ccm_send_pong(bfd->fd);
Harald Welte28aa9912014-08-20 22:06:04 +0200412 if (ret < 0) {
413 LOGP(DLINP, LOGL_ERROR, "Cannot send PONG "
414 "message. Reason: %s\n", strerror(errno));
415 }
416 break;
417 case IPAC_MSGT_PONG:
418 DEBUGP(DLMI, "PONG!\n");
419 break;
420 case IPAC_MSGT_ID_ACK:
421 DEBUGP(DLMI, "ID_ACK\n");
422 break;
423 }
424 return ret;
425}
426
427
Harald Weltee3919962014-08-20 22:28:23 +0200428void ipa_prepend_header_ext(struct msgb *msg, int proto)
Harald Welte28aa9912014-08-20 22:06:04 +0200429{
430 struct ipaccess_head_ext *hh_ext;
431
432 /* prepend the osmo ip.access header extension */
433 hh_ext = (struct ipaccess_head_ext *) msgb_push(msg, sizeof(*hh_ext));
434 hh_ext->proto = proto;
435}
436
Harald Weltee3919962014-08-20 22:28:23 +0200437void ipa_prepend_header(struct msgb *msg, int proto)
Harald Welte28aa9912014-08-20 22:06:04 +0200438{
439 struct ipaccess_head *hh;
440
441 /* prepend the ip.access header */
442 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
Harald Welte95871da2017-05-15 12:11:36 +0200443 hh->len = osmo_htons(msg->len - sizeof(*hh));
Harald Welte28aa9912014-08-20 22:06:04 +0200444 hh->proto = proto;
445}
446
Harald Welte20725b92017-05-15 12:50:04 +0200447#ifdef HAVE_SYS_SOCKET_H
448#include <sys/socket.h>
449
Harald Welte28aa9912014-08-20 22:06:04 +0200450int ipa_msg_recv(int fd, struct msgb **rmsg)
451{
452 int rc = ipa_msg_recv_buffered(fd, rmsg, NULL);
453 if (rc < 0) {
454 errno = -rc;
455 rc = -1;
456 }
457 return rc;
458}
459
460int ipa_msg_recv_buffered(int fd, struct msgb **rmsg, struct msgb **tmp_msg)
461{
462 struct msgb *msg = tmp_msg ? *tmp_msg : NULL;
463 struct ipaccess_head *hh;
464 int len, ret;
465 int needed;
466
467 if (msg == NULL) {
468 msg = ipa_msg_alloc(0);
469 if (msg == NULL) {
470 ret = -ENOMEM;
471 goto discard_msg;
472 }
473 msg->l1h = msg->tail;
474 }
475
476 if (msg->l2h == NULL) {
477 /* first read our 3-byte header */
478 needed = sizeof(*hh) - msg->len;
479 ret = recv(fd, msg->tail, needed, 0);
480 if (ret == 0)
481 goto discard_msg;
482
483 if (ret < 0) {
484 if (errno == EAGAIN || errno == EINTR)
485 ret = 0;
486 else {
487 ret = -errno;
488 goto discard_msg;
489 }
490 }
491
492 msgb_put(msg, ret);
493
494 if (ret < needed) {
495 if (msg->len == 0) {
496 ret = -EAGAIN;
497 goto discard_msg;
498 }
499
500 LOGP(DLINP, LOGL_INFO,
Harald Weltef196a022014-08-21 09:42:03 +0200501 "Received part of IPA message header (%d/%zu)\n",
Harald Welte28aa9912014-08-20 22:06:04 +0200502 msg->len, sizeof(*hh));
503 if (!tmp_msg) {
504 ret = -EIO;
505 goto discard_msg;
506 }
507 *tmp_msg = msg;
508 return -EAGAIN;
509 }
510
511 msg->l2h = msg->tail;
512 }
513
514 hh = (struct ipaccess_head *) msg->data;
515
516 /* then read the length as specified in header */
Harald Welte95871da2017-05-15 12:11:36 +0200517 len = osmo_ntohs(hh->len);
Harald Welte28aa9912014-08-20 22:06:04 +0200518
519 if (len < 0 || IPA_ALLOC_SIZE < len + sizeof(*hh)) {
520 LOGP(DLINP, LOGL_ERROR, "bad message length of %d bytes, "
521 "received %d bytes\n", len, msg->len);
522 ret = -EIO;
523 goto discard_msg;
524 }
525
526 needed = len - msgb_l2len(msg);
527
528 if (needed > 0) {
529 ret = recv(fd, msg->tail, needed, 0);
530
531 if (ret == 0)
532 goto discard_msg;
533
534 if (ret < 0) {
535 if (errno == EAGAIN || errno == EINTR)
536 ret = 0;
537 else {
538 ret = -errno;
539 goto discard_msg;
540 }
541 }
542
543 msgb_put(msg, ret);
544
545 if (ret < needed) {
546 LOGP(DLINP, LOGL_INFO,
547 "Received part of IPA message L2 data (%d/%d)\n",
548 msgb_l2len(msg), len);
549 if (!tmp_msg) {
550 ret = -EIO;
551 goto discard_msg;
552 }
553 *tmp_msg = msg;
554 return -EAGAIN;
555 }
556 }
557
558 ret = msgb_l2len(msg);
559
560 if (ret == 0) {
561 LOGP(DLINP, LOGL_INFO,
562 "Discarding IPA message without payload\n");
563 ret = -EAGAIN;
564 goto discard_msg;
565 }
566
567 if (tmp_msg)
568 *tmp_msg = NULL;
569 *rmsg = msg;
570 return ret;
571
572discard_msg:
573 if (tmp_msg)
574 *tmp_msg = NULL;
575 msgb_free(msg);
576 return ret;
577}
578
Harald Welte20725b92017-05-15 12:50:04 +0200579#endif /* SYS_SOCKET_H */
580
Harald Welte28aa9912014-08-20 22:06:04 +0200581struct msgb *ipa_msg_alloc(int headroom)
582{
583 struct msgb *nmsg;
584
585 headroom += sizeof(struct ipaccess_head);
586
Neels Hofmeyr889ab162017-09-07 20:41:12 +0200587 nmsg = msgb_alloc_headroom(1200 + headroom, headroom, "IPA Multiplex");
Harald Welte28aa9912014-08-20 22:06:04 +0200588 if (!nmsg)
589 return NULL;
590 return nmsg;
591}
Harald Welte96e2a002017-06-12 21:44:18 +0200592
593/*! @} */