blob: f44c328471440de77d7b46a1a9c85ba828cf9114 [file] [log] [blame]
Harald Welte28aa9912014-08-20 22:06:04 +02001/* 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 Weltefd5ad172014-10-26 20:47:42 +010010 * 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 Welte28aa9912014-08-20 22:06:04 +020012 * (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 Weltefd5ad172014-10-26 20:47:42 +010017 * GNU General Public License for more details.
Harald Welte28aa9912014-08-20 22:06:04 +020018 *
Harald Weltefd5ad172014-10-26 20:47:42 +010019 * You should have received a copy of the GNU General Public License
Harald Welte28aa9912014-08-20 22:06:04 +020020 * 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 Freytheree6652b2015-11-09 16:21:19 +000030#include <sys/types.h>
31#include <sys/socket.h>
Harald Welte28aa9912014-08-20 22:06:04 +020032
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 Weltee3919962014-08-20 22:28:23 +020041#include <osmocom/gsm/ipa.h>
Harald Welte28aa9912014-08-20 22:06:04 +020042
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 */
51static const uint8_t ipa_pong_msg[] = {
52 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG
53};
54
55static const uint8_t ipa_id_ack_msg[] = {
56 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK
57};
58
59static 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
72static 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 Weltee3919962014-08-20 22:28:23 +020084const char *ipa_ccm_idtag_name(uint8_t tag)
Harald Welte28aa9912014-08-20 22:06:04 +020085{
86 if (tag >= ARRAY_SIZE(idtag_names))
87 return "unknown";
88
89 return idtag_names[tag];
90}
91
Harald Weltee3919962014-08-20 22:28:23 +020092int ipa_ccm_idtag_parse(struct tlv_parsed *dec, unsigned char *buf, int len)
Harald Welte28aa9912014-08-20 22:06:04 +020093{
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +020094 return ipa_ccm_idtag_parse_off(dec, buf, len, 0);
95}
96
97int ipa_ccm_idtag_parse_off(struct tlv_parsed *dec, unsigned char *buf, int len, const int len_offset)
98{
Harald Welte28aa9912014-08-20 22:06:04 +020099 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 Freytherf558ed42015-06-02 15:52:06 +0200110 if (t_len < len_offset) {
Max9b4d0652016-11-15 19:21:23 +0100111 LOGP(DLMI, LOGL_ERROR, "minimal offset not included: %d < %d\n", t_len, len_offset);
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +0200112 return -EINVAL;
113 }
114
Harald Welte28aa9912014-08-20 22:06:04 +0200115 if (t_len > len + 1) {
Max9b4d0652016-11-15 19:21:23 +0100116 LOGP(DLMI, LOGL_ERROR, "The tag does not fit: %d > %d\n", t_len, len + 1);
Harald Welte28aa9912014-08-20 22:06:04 +0200117 return -EINVAL;
118 }
119
Harald Weltee3919962014-08-20 22:28:23 +0200120 DEBUGPC(DLMI, "%s='%s' ", ipa_ccm_idtag_name(t_tag), cur);
Harald Welte28aa9912014-08-20 22:06:04 +0200121
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +0200122 dec->lv[t_tag].len = t_len - len_offset;
Harald Welte28aa9912014-08-20 22:06:04 +0200123 dec->lv[t_tag].val = cur;
124
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +0200125 cur += t_len - len_offset;
126 len -= t_len - len_offset;
Harald Welte28aa9912014-08-20 22:06:04 +0200127 }
128 return 0;
129}
130
Harald Weltee3919962014-08-20 22:28:23 +0200131int ipa_parse_unitid(const char *str, struct ipaccess_unit *unit_data)
Harald Welte28aa9912014-08-20 22:06:04 +0200132{
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 Weltee3919962014-08-20 22:28:23 +0200164int ipa_ccm_tlv_to_unitdata(struct ipaccess_unit *ud,
Harald Welte28aa9912014-08-20 22:06:04 +0200165 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 Weltee3919962014-08-20 22:28:23 +0200201 rc = ipa_parse_unitid((char *)
Harald Welte28aa9912014-08-20 22:06:04 +0200202 TLVP_VAL(tp, IPAC_IDTAG_UNIT), ud);
203
204out:
205 return rc;
206}
207
Harald Weltee3919962014-08-20 22:28:23 +0200208int ipa_send(int fd, const void *msg, size_t msglen)
Harald Welte28aa9912014-08-20 22:06:04 +0200209{
210 int ret;
211
212 ret = write(fd, msg, msglen);
213 if (ret < 0)
Jacob Erlbecka6be2242014-12-22 10:58:46 +0100214 return -errno;
Harald Welte28aa9912014-08-20 22:06:04 +0200215 if (ret < msglen) {
Harald Weltee3919962014-08-20 22:28:23 +0200216 LOGP(DLINP, LOGL_ERROR, "ipa_send: short write\n");
Harald Welte28aa9912014-08-20 22:06:04 +0200217 return -EIO;
218 }
219 return ret;
220}
221
Harald Weltee3919962014-08-20 22:28:23 +0200222int ipa_ccm_send_pong(int fd)
Harald Welte28aa9912014-08-20 22:06:04 +0200223{
Harald Weltee3919962014-08-20 22:28:23 +0200224 return ipa_send(fd, ipa_pong_msg, sizeof(ipa_pong_msg));
Harald Welte28aa9912014-08-20 22:06:04 +0200225}
226
Harald Weltee3919962014-08-20 22:28:23 +0200227int ipa_ccm_send_id_ack(int fd)
Harald Welte28aa9912014-08-20 22:06:04 +0200228{
Harald Weltee3919962014-08-20 22:28:23 +0200229 return ipa_send(fd, ipa_id_ack_msg, sizeof(ipa_id_ack_msg));
Harald Welte28aa9912014-08-20 22:06:04 +0200230}
231
Harald Weltee3919962014-08-20 22:28:23 +0200232int ipa_ccm_send_id_req(int fd)
Harald Welte28aa9912014-08-20 22:06:04 +0200233{
Harald Weltee3919962014-08-20 22:28:23 +0200234 return ipa_send(fd, ipa_id_req_msg, sizeof(ipa_id_req_msg));
Harald Welte28aa9912014-08-20 22:06:04 +0200235}
236
237/* base handling of the ip.access protocol */
Harald Weltee3919962014-08-20 22:28:23 +0200238int ipa_ccm_rcvmsg_base(struct msgb *msg, struct osmo_fd *bfd)
Harald Welte28aa9912014-08-20 22:06:04 +0200239{
240 uint8_t msg_type = *(msg->l2h);
241 int ret;
242
243 switch (msg_type) {
244 case IPAC_MSGT_PING:
Harald Weltee3919962014-08-20 22:28:23 +0200245 ret = ipa_ccm_send_pong(bfd->fd);
Harald Welte28aa9912014-08-20 22:06:04 +0200246 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 Weltee3919962014-08-20 22:28:23 +0200259 ret = ipa_ccm_send_id_ack(bfd->fd);
Harald Welte28aa9912014-08-20 22:06:04 +0200260 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 Weltee3919962014-08-20 22:28:23 +0200276int ipa_ccm_rcvmsg_bts_base(struct msgb *msg, struct osmo_fd *bfd)
Harald Welte28aa9912014-08-20 22:06:04 +0200277{
278 uint8_t msg_type = *(msg->l2h);
279 int ret = 0;
280
281 switch (msg_type) {
282 case IPAC_MSGT_PING:
Harald Weltee3919962014-08-20 22:28:23 +0200283 ret = ipa_ccm_send_pong(bfd->fd);
Harald Welte28aa9912014-08-20 22:06:04 +0200284 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 Weltee3919962014-08-20 22:28:23 +0200300void ipa_prepend_header_ext(struct msgb *msg, int proto)
Harald Welte28aa9912014-08-20 22:06:04 +0200301{
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 Weltee3919962014-08-20 22:28:23 +0200309void ipa_prepend_header(struct msgb *msg, int proto)
Harald Welte28aa9912014-08-20 22:06:04 +0200310{
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
319int 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
329int 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 Weltef196a022014-08-21 09:42:03 +0200370 "Received part of IPA message header (%d/%zu)\n",
Harald Welte28aa9912014-08-20 22:06:04 +0200371 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
441discard_msg:
442 if (tmp_msg)
443 *tmp_msg = NULL;
444 msgb_free(msg);
445 return ret;
446}
447
448struct 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}