blob: 8e64a1eb6356f607bf2628706b17dbc260f75da0 [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
Pau Espin Pedroldeeab472019-03-27 17:33:17 +0100101/*! Parse the payload part of an IPA CCM ID GET, return \ref tlv_parsed format. */
Harald Weltee3919962014-08-20 22:28:23 +0200102int ipa_ccm_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
Harald Welte28aa9912014-08-20 22:06:04 +0200103{
Pau Espin Pedroldeeab472019-03-27 17:33:17 +0100104 return ipa_ccm_idtag_parse_off(dec, buf, len, 1);
Harald Welte5a7740d2018-08-01 17:29:48 +0200105}
106
Pau Espin Pedroldeeab472019-03-27 17:33:17 +0100107/*! 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 Welte5a7740d2018-08-01 17:29:48 +0200116int ipa_ccm_idtag_parse_off(struct tlv_parsed *dec, unsigned char *buf, int len, const int len_offset)
117{
Harald Welte28aa9912014-08-20 22:06:04 +0200118 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 Welte5a7740d2018-08-01 17:29:48 +0200129 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 Welte7869baf2018-07-31 20:25:48 +0200134 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 Freytherf558ed42015-06-02 15:52:06 +0200136 return -EINVAL;
137 }
138
Harald Welte7869baf2018-07-31 20:25:48 +0200139 DEBUGPC(DLMI, "%s='%s' ", ipa_ccm_idtag_name(t_tag), cur);
140
Oliver Smith27f7b0d2019-03-25 13:45:57 +0100141 dec->lv[t_tag].len = t_len - len_offset;
Harald Welte7869baf2018-07-31 20:25:48 +0200142 dec->lv[t_tag].val = cur;
143
Harald Welte5a7740d2018-08-01 17:29:48 +0200144 cur += t_len - len_offset;
145 len -= t_len - len_offset;
Harald Welte7869baf2018-07-31 20:25:48 +0200146 }
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 */
159int 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 Welte28aa9912014-08-20 22:06:04 +0200172 if (t_len > len + 1) {
Max9b4d0652016-11-15 19:21:23 +0100173 LOGP(DLMI, LOGL_ERROR, "The tag does not fit: %d > %d\n", t_len, len + 1);
Harald Welte28aa9912014-08-20 22:06:04 +0200174 return -EINVAL;
175 }
176
Harald Weltee3919962014-08-20 22:28:23 +0200177 DEBUGPC(DLMI, "%s='%s' ", ipa_ccm_idtag_name(t_tag), cur);
Harald Welte28aa9912014-08-20 22:06:04 +0200178
Harald Welte7869baf2018-07-31 20:25:48 +0200179 dec->lv[t_tag].len = t_len-1;
Harald Welte28aa9912014-08-20 22:06:04 +0200180 dec->lv[t_tag].val = cur;
181
Harald Welte7869baf2018-07-31 20:25:48 +0200182 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 */
197int 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;
207 t_len = *cur++ << 8;
208 t_len += *cur++;
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 Welte28aa9912014-08-20 22:06:04 +0200223 }
224 return 0;
225}
226
Harald Weltee3919962014-08-20 22:28:23 +0200227int ipa_parse_unitid(const char *str, struct ipaccess_unit *unit_data)
Harald Welte28aa9912014-08-20 22:06:04 +0200228{
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 Weltee3919962014-08-20 22:28:23 +0200260int ipa_ccm_tlv_to_unitdata(struct ipaccess_unit *ud,
Harald Welte28aa9912014-08-20 22:06:04 +0200261 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 Weltee3919962014-08-20 22:28:23 +0200297 rc = ipa_parse_unitid((char *)
Harald Welte28aa9912014-08-20 22:06:04 +0200298 TLVP_VAL(tp, IPAC_IDTAG_UNIT), ud);
299
300out:
301 return rc;
302}
303
Harald Welte7bc88bb2017-04-15 19:05:33 +0200304#define IPA_STRING_MAX 64
305
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200306/*! Generate IPA CCM ID RESP based on list of IEs
Harald Welte7bc88bb2017-04-15 19:05:33 +0200307 * \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 */
311struct 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 Hofmeyrebe4ef72018-07-26 17:12:07 +0200341 osmo_strlcpy(str, dev->location1, sizeof(str));
Harald Welte7bc88bb2017-04-15 19:05:33 +0200342 break;
343 case IPAC_IDTAG_LOCATION2:
344 if (dev->location2)
Neels Hofmeyrebe4ef72018-07-26 17:12:07 +0200345 osmo_strlcpy(str, dev->location2, sizeof(str));
Harald Welte7bc88bb2017-04-15 19:05:33 +0200346 break;
347 case IPAC_IDTAG_EQUIPVERS:
348 if (dev->equipvers)
Neels Hofmeyrebe4ef72018-07-26 17:12:07 +0200349 osmo_strlcpy(str, dev->equipvers, sizeof(str));
Harald Welte7bc88bb2017-04-15 19:05:33 +0200350 break;
351 case IPAC_IDTAG_SWVERSION:
352 if (dev->swversion)
Neels Hofmeyrebe4ef72018-07-26 17:12:07 +0200353 osmo_strlcpy(str, dev->swversion, sizeof(str));
Harald Welte7bc88bb2017-04-15 19:05:33 +0200354 break;
355 case IPAC_IDTAG_UNITNAME:
356 if (dev->unit_name) {
Neels Hofmeyrebe4ef72018-07-26 17:12:07 +0200357 snprintf(str, sizeof(str), "%s", dev->unit_name);
Harald Welte7bc88bb2017-04-15 19:05:33 +0200358 } 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 Hofmeyrebe4ef72018-07-26 17:12:07 +0200368 osmo_strlcpy(str, dev->serno, sizeof(str));
Harald Welte7bc88bb2017-04-15 19:05:33 +0200369 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 Welte7bc88bb2017-04-15 19:05:33 +0200376
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 Hofmeyr87e45502017-06-20 00:17:59 +0200388/*! Generate IPA CCM ID RESP based on requets payload
Harald Welte7bc88bb2017-04-15 19:05:33 +0200389 * \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 */
393struct 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 Welte8a4895c2017-04-27 10:25:10 +0200400 memset(ies, 0, sizeof(ies));
401
Harald Welte7bc88bb2017-04-15 19:05:33 +0200402 /* 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 Alteholz5a9dbf82018-04-08 19:13:25 +0200410 LOGP(DLINP, LOGL_ERROR, "IPA CCM tag 0x%02x does not fit\n", t_tag);
Harald Welte7bc88bb2017-04-15 19:05:33 +0200411 break;
412 }
413
414 ies[num_ies++] = t_tag;
415
416 cur += t_len;
Harald Welte0b2c0ec2018-04-16 22:53:48 +0200417 /* 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 Welte7bc88bb2017-04-15 19:05:33 +0200423 }
424 return ipa_ccm_make_id_resp(dev, ies, num_ies);
425}
426
Harald Weltee3919962014-08-20 22:28:23 +0200427int ipa_send(int fd, const void *msg, size_t msglen)
Harald Welte28aa9912014-08-20 22:06:04 +0200428{
429 int ret;
430
431 ret = write(fd, msg, msglen);
432 if (ret < 0)
Jacob Erlbecka6be2242014-12-22 10:58:46 +0100433 return -errno;
Harald Welte28aa9912014-08-20 22:06:04 +0200434 if (ret < msglen) {
Harald Weltee3919962014-08-20 22:28:23 +0200435 LOGP(DLINP, LOGL_ERROR, "ipa_send: short write\n");
Harald Welte28aa9912014-08-20 22:06:04 +0200436 return -EIO;
437 }
438 return ret;
439}
440
Harald Weltee3919962014-08-20 22:28:23 +0200441int ipa_ccm_send_pong(int fd)
Harald Welte28aa9912014-08-20 22:06:04 +0200442{
Harald Weltee3919962014-08-20 22:28:23 +0200443 return ipa_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg));
Harald Welte28aa9912014-08-20 22:06:04 +0200444}
445
Harald Weltee3919962014-08-20 22:28:23 +0200446int ipa_ccm_send_id_ack(int fd)
Harald Welte28aa9912014-08-20 22:06:04 +0200447{
Harald Weltee3919962014-08-20 22:28:23 +0200448 return ipa_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg));
Harald Welte28aa9912014-08-20 22:06:04 +0200449}
450
Harald Weltee3919962014-08-20 22:28:23 +0200451int ipa_ccm_send_id_req(int fd)
Harald Welte28aa9912014-08-20 22:06:04 +0200452{
Harald Weltee3919962014-08-20 22:28:23 +0200453 return ipa_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg));
Harald Welte28aa9912014-08-20 22:06:04 +0200454}
455
456/* base handling of the ip.access protocol */
Harald Weltee3919962014-08-20 22:28:23 +0200457int ipa_ccm_rcvmsg_base(struct msgb *msg, struct osmo_fd *bfd)
Harald Welte28aa9912014-08-20 22:06:04 +0200458{
459 uint8_t msg_type = *(msg->l2h);
460 int ret;
461
462 switch (msg_type) {
463 case IPAC_MSGT_PING:
Harald Weltee3919962014-08-20 22:28:23 +0200464 ret = ipa_ccm_send_pong(bfd->fd);
Harald Welte28aa9912014-08-20 22:06:04 +0200465 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 Weltee3919962014-08-20 22:28:23 +0200478 ret = ipa_ccm_send_id_ack(bfd->fd);
Harald Welte28aa9912014-08-20 22:06:04 +0200479 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 Weltee3919962014-08-20 22:28:23 +0200495int ipa_ccm_rcvmsg_bts_base(struct msgb *msg, struct osmo_fd *bfd)
Harald Welte28aa9912014-08-20 22:06:04 +0200496{
497 uint8_t msg_type = *(msg->l2h);
498 int ret = 0;
499
500 switch (msg_type) {
501 case IPAC_MSGT_PING:
Harald Weltee3919962014-08-20 22:28:23 +0200502 ret = ipa_ccm_send_pong(bfd->fd);
Harald Welte28aa9912014-08-20 22:06:04 +0200503 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 Weltee3919962014-08-20 22:28:23 +0200519void ipa_prepend_header_ext(struct msgb *msg, int proto)
Harald Welte28aa9912014-08-20 22:06:04 +0200520{
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 Weltee3919962014-08-20 22:28:23 +0200528void ipa_prepend_header(struct msgb *msg, int proto)
Harald Welte28aa9912014-08-20 22:06:04 +0200529{
530 struct ipaccess_head *hh;
531
532 /* prepend the ip.access header */
533 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh));
Harald Welte95871da2017-05-15 12:11:36 +0200534 hh->len = osmo_htons(msg->len - sizeof(*hh));
Harald Welte28aa9912014-08-20 22:06:04 +0200535 hh->proto = proto;
536}
537
Harald Welte20725b92017-05-15 12:50:04 +0200538#ifdef HAVE_SYS_SOCKET_H
539#include <sys/socket.h>
540
Pau Espin Pedrol8a757d22018-08-22 14:12:01 +0200541/*! 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 Welte28aa9912014-08-20 22:06:04 +0200544int 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 Pedrol8a757d22018-08-22 14:12:01 +0200554/*! 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 Welte28aa9912014-08-20 22:06:04 +0200573int 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 Weltef196a022014-08-21 09:42:03 +0200614 "Received part of IPA message header (%d/%zu)\n",
Harald Welte28aa9912014-08-20 22:06:04 +0200615 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 Welte95871da2017-05-15 12:11:36 +0200630 len = osmo_ntohs(hh->len);
Harald Welte28aa9912014-08-20 22:06:04 +0200631
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
685discard_msg:
686 if (tmp_msg)
687 *tmp_msg = NULL;
688 msgb_free(msg);
689 return ret;
690}
691
Harald Welte20725b92017-05-15 12:50:04 +0200692#endif /* SYS_SOCKET_H */
693
Harald Welte28aa9912014-08-20 22:06:04 +0200694struct msgb *ipa_msg_alloc(int headroom)
695{
696 struct msgb *nmsg;
697
698 headroom += sizeof(struct ipaccess_head);
699
Neels Hofmeyr889ab162017-09-07 20:41:12 +0200700 nmsg = msgb_alloc_headroom(1200 + headroom, headroom, "IPA Multiplex");
Harald Welte28aa9912014-08-20 22:06:04 +0200701 if (!nmsg)
702 return NULL;
703 return nmsg;
704}
Harald Welte96e2a002017-06-12 21:44:18 +0200705
706/*! @} */