blob: 26720b6c05b1d0bb9bf187d1816e6cd805609c02 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gsmtap_util.c
2 * GSMTAP support code in libosmocore. */
Harald Weltee779c362010-06-29 20:51:13 +02003/*
Harald Welte93713a52017-07-12 23:43:40 +02004 * (C) 2010-2017 by Harald Welte <laforge@gnumonks.org>
Harald Weltee779c362010-06-29 20:51:13 +02005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include "../config.h"
25
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010026#include <osmocom/core/gsmtap_util.h>
27#include <osmocom/core/logging.h>
28#include <osmocom/core/gsmtap.h>
29#include <osmocom/core/msgb.h>
Harald Welte33cb71a2011-05-21 18:54:32 +020030#include <osmocom/core/talloc.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010031#include <osmocom/core/select.h>
Harald Welte33cb71a2011-05-21 18:54:32 +020032#include <osmocom/core/socket.h>
Harald Welte95871da2017-05-15 12:11:36 +020033#include <osmocom/core/byteswap.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010034#include <osmocom/gsm/protocol/gsm_04_08.h>
35#include <osmocom/gsm/rsl.h>
Harald Weltee779c362010-06-29 20:51:13 +020036
Harald Welte33cb71a2011-05-21 18:54:32 +020037#include <sys/types.h>
Harald Weltee4764422011-05-22 12:25:57 +020038
Harald Weltee779c362010-06-29 20:51:13 +020039#include <stdio.h>
40#include <unistd.h>
41#include <stdint.h>
42#include <string.h>
43#include <errno.h>
44
Harald Welte47379ca2011-08-17 16:35:24 +020045/*! \addtogroup gsmtap
46 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020047 * GSMTAP utility routines. Encapsulates GSM messages over UDP.
48 *
49 * \file gsmtap_util.c */
Harald Welte47379ca2011-08-17 16:35:24 +020050
51
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052/*! convert RSL channel number to GSMTAP channel type
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010053 * \param[in] rsl_chantype RSL channel type
Harald Welte47379ca2011-08-17 16:35:24 +020054 * \param[in] link_id RSL link identifier
55 * \returns GSMTAP channel type
56 */
Harald Weltee779c362010-06-29 20:51:13 +020057uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id)
58{
59 uint8_t ret = GSMTAP_CHANNEL_UNKNOWN;
60
61 switch (rsl_chantype) {
62 case RSL_CHAN_Bm_ACCHs:
63 ret = GSMTAP_CHANNEL_TCH_F;
64 break;
65 case RSL_CHAN_Lm_ACCHs:
66 ret = GSMTAP_CHANNEL_TCH_H;
67 break;
68 case RSL_CHAN_SDCCH4_ACCH:
69 ret = GSMTAP_CHANNEL_SDCCH4;
70 break;
71 case RSL_CHAN_SDCCH8_ACCH:
72 ret = GSMTAP_CHANNEL_SDCCH8;
73 break;
74 case RSL_CHAN_BCCH:
75 ret = GSMTAP_CHANNEL_BCCH;
76 break;
77 case RSL_CHAN_RACH:
78 ret = GSMTAP_CHANNEL_RACH;
79 break;
80 case RSL_CHAN_PCH_AGCH:
81 /* it could also be AGCH... */
82 ret = GSMTAP_CHANNEL_PCH;
83 break;
84 }
85
86 if (link_id & 0x40)
87 ret |= GSMTAP_CHANNEL_ACCH;
88
89 return ret;
90}
91
Harald Welte93713a52017-07-12 23:43:40 +020092/*! convert GSMTAP channel type to RSL channel number + Link ID
93 * \param[in] gsmtap_chantype GSMTAP channel type
94 * \param[out] rsl_chantype RSL channel mumber
95 * \param[out] link_id RSL link identifier
96 */
97void chantype_gsmtap2rsl(uint8_t gsmtap_chantype, uint8_t *rsl_chantype,
98 uint8_t *link_id)
99{
100 switch (gsmtap_chantype & ~GSMTAP_CHANNEL_ACCH & 0xff) {
101 case GSMTAP_CHANNEL_TCH_F: // TCH/F, FACCH/F
102 *rsl_chantype = RSL_CHAN_Bm_ACCHs;
103 break;
104 case GSMTAP_CHANNEL_TCH_H: // TCH/H, FACCH/H
105 *rsl_chantype = RSL_CHAN_Lm_ACCHs;
106 break;
107 case GSMTAP_CHANNEL_SDCCH4: // SDCCH/4
108 *rsl_chantype = RSL_CHAN_SDCCH4_ACCH;
109 break;
110 case GSMTAP_CHANNEL_SDCCH8: // SDCCH/8
111 *rsl_chantype = RSL_CHAN_SDCCH8_ACCH;
112 break;
113 case GSMTAP_CHANNEL_BCCH: // BCCH
114 *rsl_chantype = RSL_CHAN_BCCH;
115 break;
116 case GSMTAP_CHANNEL_RACH: // RACH
117 *rsl_chantype = RSL_CHAN_RACH;
118 break;
119 case GSMTAP_CHANNEL_PCH: // PCH
120 case GSMTAP_CHANNEL_AGCH: // AGCH
121 *rsl_chantype = RSL_CHAN_PCH_AGCH;
122 break;
123 case GSMTAP_CHANNEL_PDCH:
124 *rsl_chantype = GSMTAP_CHANNEL_PDCH;
125 break;
126 }
127
128 *link_id = gsmtap_chantype & GSMTAP_CHANNEL_ACCH ? 0x40 : 0x00;
129}
130
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200131/*! create an arbitrary type GSMTAP message
Sylvain Munautabf66e72011-09-26 13:23:19 +0200132 * \param[in] type The GSMTAP_TYPE_xxx constant of the message to create
Harald Welte47379ca2011-08-17 16:35:24 +0200133 * \param[in] arfcn GSM ARFCN (Channel Number)
134 * \param[in] ts GSM time slot
135 * \param[in] chan_type Channel Type
136 * \param[in] ss Sub-slot
137 * \param[in] fn GSM Frame Number
138 * \param[in] signal_dbm Signal Strength (dBm)
139 * \param[in] snr Signal/Noise Ratio (SNR)
140 * \param[in] data Pointer to data buffer
141 * \param[in] len Length of \ref data
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200142 * \return dynamically allocated message buffer containing data
Harald Welte47379ca2011-08-17 16:35:24 +0200143 *
144 * This function will allocate a new msgb and fill it with a GSMTAP
145 * header containing the information
146 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200147struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type,
Harald Weltee34a9402010-06-29 22:31:21 +0200148 uint8_t ss, uint32_t fn, int8_t signal_dbm,
149 uint8_t snr, const uint8_t *data, unsigned int len)
Harald Weltee779c362010-06-29 20:51:13 +0200150{
151 struct msgb *msg;
152 struct gsmtap_hdr *gh;
153 uint8_t *dst;
154
Harald Weltee779c362010-06-29 20:51:13 +0200155 msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
156 if (!msg)
Harald Weltee34a9402010-06-29 22:31:21 +0200157 return NULL;
Harald Weltee779c362010-06-29 20:51:13 +0200158
159 gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
160
161 gh->version = GSMTAP_VERSION;
162 gh->hdr_len = sizeof(*gh)/4;
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200163 gh->type = type;
Harald Weltee779c362010-06-29 20:51:13 +0200164 gh->timeslot = ts;
165 gh->sub_slot = ss;
Harald Welte95871da2017-05-15 12:11:36 +0200166 gh->arfcn = osmo_htons(arfcn);
Harald Weltee779c362010-06-29 20:51:13 +0200167 gh->snr_db = snr;
168 gh->signal_dbm = signal_dbm;
Harald Welte95871da2017-05-15 12:11:36 +0200169 gh->frame_number = osmo_htonl(fn);
Harald Weltee779c362010-06-29 20:51:13 +0200170 gh->sub_type = chan_type;
171 gh->antenna_nr = 0;
172
173 dst = msgb_put(msg, len);
174 memcpy(dst, data, len);
175
Harald Weltee34a9402010-06-29 22:31:21 +0200176 return msg;
177}
178
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200179/*! create L1/L2 data and put it into GSMTAP
Sylvain Munautabf66e72011-09-26 13:23:19 +0200180 * \param[in] arfcn GSM ARFCN (Channel Number)
181 * \param[in] ts GSM time slot
182 * \param[in] chan_type Channel Type
183 * \param[in] ss Sub-slot
184 * \param[in] fn GSM Frame Number
185 * \param[in] signal_dbm Signal Strength (dBm)
186 * \param[in] snr Signal/Noise Ratio (SNR)
187 * \param[in] data Pointer to data buffer
188 * \param[in] len Length of \ref data
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200189 * \return message buffer or NULL in case of error
Sylvain Munautabf66e72011-09-26 13:23:19 +0200190 *
191 * This function will allocate a new msgb and fill it with a GSMTAP
192 * header containing the information
193 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200194struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
195 uint8_t ss, uint32_t fn, int8_t signal_dbm,
196 uint8_t snr, const uint8_t *data, unsigned int len)
197{
198 return gsmtap_makemsg_ex(GSMTAP_TYPE_UM, arfcn, ts, chan_type,
199 ss, fn, signal_dbm, snr, data, len);
200}
201
Harald Weltee4764422011-05-22 12:25:57 +0200202#ifdef HAVE_SYS_SOCKET_H
203
204#include <sys/socket.h>
205#include <netinet/in.h>
206
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200207/*! Create a new (sending) GSMTAP source socket
Harald Welte47379ca2011-08-17 16:35:24 +0200208 * \param[in] host host name or IP address in string format
209 * \param[in] port UDP port number in host byte order
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200210 * \return file descriptor of the new socket
Harald Welte47379ca2011-08-17 16:35:24 +0200211 *
212 * Opens a GSMTAP source (sending) socket, conncet it to host/port and
213 * return resulting fd. If \a host is NULL, the destination address
214 * will be localhost. If \a port is 0, the default \ref
215 * GSMTAP_UDP_PORT will be used.
216 * */
Harald Welte33cb71a2011-05-21 18:54:32 +0200217int gsmtap_source_init_fd(const char *host, uint16_t port)
218{
219 if (port == 0)
220 port = GSMTAP_UDP_PORT;
221 if (host == NULL)
222 host = "localhost";
223
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200224 return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port,
225 OSMO_SOCK_F_CONNECT);
Harald Welte33cb71a2011-05-21 18:54:32 +0200226}
227
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200228/*! Add a local sink to an existing GSMTAP source and return fd
Harald Weltede6e4982012-12-06 21:25:27 +0100229 * \param[in] gsmtap_fd file descriptor of the gsmtap socket
230 * \returns file descriptor of locally bound receive socket
231 *
232 * In case the GSMTAP socket is connected to a local destination
233 * IP/port, this function creates a corresponding receiving socket
234 * bound to that destination IP + port.
235 *
236 * In case the gsmtap socket is not connected to a local IP/port, or
237 * creation of the receiving socket fails, a negative error code is
238 * returned.
239 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200240int gsmtap_source_add_sink_fd(int gsmtap_fd)
241{
242 struct sockaddr_storage ss;
243 socklen_t ss_len = sizeof(ss);
244 int rc;
245
246 rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len);
247 if (rc < 0)
248 return rc;
249
250 if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) {
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200251 rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM,
252 IPPROTO_UDP, OSMO_SOCK_F_BIND);
Harald Welte33cb71a2011-05-21 18:54:32 +0200253 if (rc >= 0)
254 return rc;
255 }
256
257 return -ENODEV;
258}
259
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200260/*! Send a \ref msgb through a GSMTAP source
Harald Welte47379ca2011-08-17 16:35:24 +0200261 * \param[in] gti GSMTAP instance
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100262 * \param[in] msg message buffer
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200263 * \return 0 in case of success; negative in case of error
Harald Welte47379ca2011-08-17 16:35:24 +0200264 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200265int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg)
266{
Harald Welte13692a62011-05-22 20:06:11 +0200267 if (!gti)
268 return -ENODEV;
269
Harald Welte33cb71a2011-05-21 18:54:32 +0200270 if (gti->ofd_wq_mode)
271 return osmo_wqueue_enqueue(&gti->wq, msg);
272 else {
273 /* try immediate send and return error if any */
274 int rc;
275
276 rc = write(gsmtap_inst_fd(gti), msg->data, msg->len);
277 if (rc <= 0) {
278 return rc;
279 } else if (rc >= msg->len) {
280 msgb_free(msg);
281 return 0;
282 } else {
283 /* short write */
284 return -EIO;
285 }
286 }
287}
288
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200289/*! send an arbitrary type through GSMTAP.
Sylvain Munautabf66e72011-09-26 13:23:19 +0200290 * See \ref gsmtap_makemsg_ex for arguments
291 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200292int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts,
Harald Welte33cb71a2011-05-21 18:54:32 +0200293 uint8_t chan_type, uint8_t ss, uint32_t fn,
294 int8_t signal_dbm, uint8_t snr, const uint8_t *data,
295 unsigned int len)
Harald Weltee34a9402010-06-29 22:31:21 +0200296{
297 struct msgb *msg;
298
Harald Welte13692a62011-05-22 20:06:11 +0200299 if (!gti)
300 return -ENODEV;
301
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200302 msg = gsmtap_makemsg_ex(type, arfcn, ts, chan_type, ss, fn, signal_dbm,
Harald Weltee34a9402010-06-29 22:31:21 +0200303 snr, data, len);
304 if (!msg)
305 return -ENOMEM;
306
Harald Welte33cb71a2011-05-21 18:54:32 +0200307 return gsmtap_sendmsg(gti, msg);
Harald Weltee779c362010-06-29 20:51:13 +0200308}
309
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200310/*! send a message from L1/L2 through GSMTAP.
Sylvain Munautabf66e72011-09-26 13:23:19 +0200311 * See \ref gsmtap_makemsg for arguments
312 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200313int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
314 uint8_t chan_type, uint8_t ss, uint32_t fn,
315 int8_t signal_dbm, uint8_t snr, const uint8_t *data,
316 unsigned int len)
317{
318 return gsmtap_send_ex(gti, GSMTAP_TYPE_UM, arfcn, ts, chan_type, ss, fn,
319 signal_dbm, snr, data, len);
320}
321
Harald Weltee779c362010-06-29 20:51:13 +0200322/* Callback from select layer if we can write to the socket */
Harald Welte33cb71a2011-05-21 18:54:32 +0200323static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg)
Harald Weltee779c362010-06-29 20:51:13 +0200324{
Harald Weltee779c362010-06-29 20:51:13 +0200325 int rc;
326
Harald Welte33cb71a2011-05-21 18:54:32 +0200327 rc = write(ofd->fd, msg->data, msg->len);
Harald Weltee779c362010-06-29 20:51:13 +0200328 if (rc < 0) {
Harald Weltee779c362010-06-29 20:51:13 +0200329 return rc;
330 }
331 if (rc != msg->len) {
Harald Weltee779c362010-06-29 20:51:13 +0200332 return -EIO;
333 }
334
Harald Weltee779c362010-06-29 20:51:13 +0200335 return 0;
336}
337
Harald Welted58ba462011-04-27 10:57:49 +0200338/* Callback from select layer if we can read from the sink socket */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200339static int gsmtap_sink_fd_cb(struct osmo_fd *fd, unsigned int flags)
Harald Welted58ba462011-04-27 10:57:49 +0200340{
341 int rc;
342 uint8_t buf[4096];
343
344 if (!(flags & BSC_FD_READ))
345 return 0;
346
347 rc = read(fd->fd, buf, sizeof(buf));
348 if (rc < 0) {
Harald Welted58ba462011-04-27 10:57:49 +0200349 return rc;
350 }
351 /* simply discard any data arriving on the socket */
352
353 return 0;
354}
355
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200356/*! Add a local sink to an existing GSMTAP source and return fd
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200357 * \param[in] gsmtap_fd file descriptor of the gsmtap socket
358 * \returns file descriptor of locally bound receive socket
359 *
360 * In case the GSMTAP socket is connected to a local destination
361 * IP/port, this function creates a corresponding receiving socket
362 * bound to that destination IP + port.
363 *
364 * In case the gsmtap socket is not connected to a local IP/port, or
365 * creation of the receiving socket fails, a negative error code is
366 * returned.
367 *
368 * The file descriptor of the receiving socket is automatically added
369 * to the libosmocore select() handling.
370 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200371int gsmtap_source_add_sink(struct gsmtap_inst *gti)
Harald Welted58ba462011-04-27 10:57:49 +0200372{
Harald Welte9d862c82016-11-26 00:10:07 +0100373 int fd, rc;
Harald Welted58ba462011-04-27 10:57:49 +0200374
Harald Welte33cb71a2011-05-21 18:54:32 +0200375 fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti));
376 if (fd < 0)
377 return fd;
Harald Welted58ba462011-04-27 10:57:49 +0200378
Harald Welte33cb71a2011-05-21 18:54:32 +0200379 if (gti->ofd_wq_mode) {
380 struct osmo_fd *sink_ofd;
381
382 sink_ofd = &gti->sink_ofd;
383 sink_ofd->fd = fd;
384 sink_ofd->when = BSC_FD_READ;
385 sink_ofd->cb = gsmtap_sink_fd_cb;
386
Harald Welte9d862c82016-11-26 00:10:07 +0100387 rc = osmo_fd_register(sink_ofd);
388 if (rc < 0) {
389 close(fd);
390 return rc;
391 }
Harald Welted58ba462011-04-27 10:57:49 +0200392 }
393
Harald Welte33cb71a2011-05-21 18:54:32 +0200394 return fd;
395}
Harald Welted58ba462011-04-27 10:57:49 +0200396
Harald Welte47379ca2011-08-17 16:35:24 +0200397
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200398/*! Open GSMTAP source socket, connect and register osmo_fd
Harald Welte47379ca2011-08-17 16:35:24 +0200399 * \param[in] host host name or IP address in string format
400 * \param[in] port UDP port number in host byte order
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100401 * \param[in] ofd_wq_mode Register \ref osmo_wqueue (1) or not (0)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200402 * \return callee-allocated \ref gsmtap_inst
Harald Welte47379ca2011-08-17 16:35:24 +0200403 *
404 * Open GSMTAP source (sending) socket, connect it to host/port,
405 * allocate 'struct gsmtap_inst' and optionally osmo_fd/osmo_wqueue
406 * registration. This means it is like \ref gsmtap_init2 but integrated
407 * with libosmocore \ref select */
Harald Welte33cb71a2011-05-21 18:54:32 +0200408struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port,
409 int ofd_wq_mode)
410{
411 struct gsmtap_inst *gti;
Harald Welte9d862c82016-11-26 00:10:07 +0100412 int fd, rc;
Harald Welted58ba462011-04-27 10:57:49 +0200413
Harald Welte33cb71a2011-05-21 18:54:32 +0200414 fd = gsmtap_source_init_fd(host, port);
415 if (fd < 0)
416 return NULL;
417
418 gti = talloc_zero(NULL, struct gsmtap_inst);
419 gti->ofd_wq_mode = ofd_wq_mode;
420 gti->wq.bfd.fd = fd;
421 gti->sink_ofd.fd = -1;
422
423 if (ofd_wq_mode) {
424 osmo_wqueue_init(&gti->wq, 64);
425 gti->wq.write_cb = &gsmtap_wq_w_cb;
426
Harald Welte9d862c82016-11-26 00:10:07 +0100427 rc = osmo_fd_register(&gti->wq.bfd);
428 if (rc < 0) {
429 close(fd);
430 return NULL;
431 }
Harald Welte33cb71a2011-05-21 18:54:32 +0200432 }
433
434 return gti;
Harald Welted58ba462011-04-27 10:57:49 +0200435}
436
Harald Weltee4764422011-05-22 12:25:57 +0200437#endif /* HAVE_SYS_SOCKET_H */
Harald Weltede6e4982012-12-06 21:25:27 +0100438
439/*! @} */