blob: 561cdb30195ea3131e0dfaba27db0eb81dcb78f0 [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 Welte33cb71a2011-05-21 18:54:32 +02004 * (C) 2010-2011 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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020092/*! create an arbitrary type GSMTAP message
Sylvain Munautabf66e72011-09-26 13:23:19 +020093 * \param[in] type The GSMTAP_TYPE_xxx constant of the message to create
Harald Welte47379ca2011-08-17 16:35:24 +020094 * \param[in] arfcn GSM ARFCN (Channel Number)
95 * \param[in] ts GSM time slot
96 * \param[in] chan_type Channel Type
97 * \param[in] ss Sub-slot
98 * \param[in] fn GSM Frame Number
99 * \param[in] signal_dbm Signal Strength (dBm)
100 * \param[in] snr Signal/Noise Ratio (SNR)
101 * \param[in] data Pointer to data buffer
102 * \param[in] len Length of \ref data
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200103 * \return dynamically allocated message buffer containing data
Harald Welte47379ca2011-08-17 16:35:24 +0200104 *
105 * This function will allocate a new msgb and fill it with a GSMTAP
106 * header containing the information
107 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200108struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type,
Harald Weltee34a9402010-06-29 22:31:21 +0200109 uint8_t ss, uint32_t fn, int8_t signal_dbm,
110 uint8_t snr, const uint8_t *data, unsigned int len)
Harald Weltee779c362010-06-29 20:51:13 +0200111{
112 struct msgb *msg;
113 struct gsmtap_hdr *gh;
114 uint8_t *dst;
115
Harald Weltee779c362010-06-29 20:51:13 +0200116 msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
117 if (!msg)
Harald Weltee34a9402010-06-29 22:31:21 +0200118 return NULL;
Harald Weltee779c362010-06-29 20:51:13 +0200119
120 gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
121
122 gh->version = GSMTAP_VERSION;
123 gh->hdr_len = sizeof(*gh)/4;
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200124 gh->type = type;
Harald Weltee779c362010-06-29 20:51:13 +0200125 gh->timeslot = ts;
126 gh->sub_slot = ss;
Harald Welte95871da2017-05-15 12:11:36 +0200127 gh->arfcn = osmo_htons(arfcn);
Harald Weltee779c362010-06-29 20:51:13 +0200128 gh->snr_db = snr;
129 gh->signal_dbm = signal_dbm;
Harald Welte95871da2017-05-15 12:11:36 +0200130 gh->frame_number = osmo_htonl(fn);
Harald Weltee779c362010-06-29 20:51:13 +0200131 gh->sub_type = chan_type;
132 gh->antenna_nr = 0;
133
134 dst = msgb_put(msg, len);
135 memcpy(dst, data, len);
136
Harald Weltee34a9402010-06-29 22:31:21 +0200137 return msg;
138}
139
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200140/*! create L1/L2 data and put it into GSMTAP
Sylvain Munautabf66e72011-09-26 13:23:19 +0200141 * \param[in] arfcn GSM ARFCN (Channel Number)
142 * \param[in] ts GSM time slot
143 * \param[in] chan_type Channel Type
144 * \param[in] ss Sub-slot
145 * \param[in] fn GSM Frame Number
146 * \param[in] signal_dbm Signal Strength (dBm)
147 * \param[in] snr Signal/Noise Ratio (SNR)
148 * \param[in] data Pointer to data buffer
149 * \param[in] len Length of \ref data
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200150 * \return message buffer or NULL in case of error
Sylvain Munautabf66e72011-09-26 13:23:19 +0200151 *
152 * This function will allocate a new msgb and fill it with a GSMTAP
153 * header containing the information
154 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200155struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
156 uint8_t ss, uint32_t fn, int8_t signal_dbm,
157 uint8_t snr, const uint8_t *data, unsigned int len)
158{
159 return gsmtap_makemsg_ex(GSMTAP_TYPE_UM, arfcn, ts, chan_type,
160 ss, fn, signal_dbm, snr, data, len);
161}
162
Harald Weltee4764422011-05-22 12:25:57 +0200163#ifdef HAVE_SYS_SOCKET_H
164
165#include <sys/socket.h>
166#include <netinet/in.h>
167
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200168/*! Create a new (sending) GSMTAP source socket
Harald Welte47379ca2011-08-17 16:35:24 +0200169 * \param[in] host host name or IP address in string format
170 * \param[in] port UDP port number in host byte order
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200171 * \return file descriptor of the new socket
Harald Welte47379ca2011-08-17 16:35:24 +0200172 *
173 * Opens a GSMTAP source (sending) socket, conncet it to host/port and
174 * return resulting fd. If \a host is NULL, the destination address
175 * will be localhost. If \a port is 0, the default \ref
176 * GSMTAP_UDP_PORT will be used.
177 * */
Harald Welte33cb71a2011-05-21 18:54:32 +0200178int gsmtap_source_init_fd(const char *host, uint16_t port)
179{
180 if (port == 0)
181 port = GSMTAP_UDP_PORT;
182 if (host == NULL)
183 host = "localhost";
184
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200185 return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port,
186 OSMO_SOCK_F_CONNECT);
Harald Welte33cb71a2011-05-21 18:54:32 +0200187}
188
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200189/*! Add a local sink to an existing GSMTAP source and return fd
Harald Weltede6e4982012-12-06 21:25:27 +0100190 * \param[in] gsmtap_fd file descriptor of the gsmtap socket
191 * \returns file descriptor of locally bound receive socket
192 *
193 * In case the GSMTAP socket is connected to a local destination
194 * IP/port, this function creates a corresponding receiving socket
195 * bound to that destination IP + port.
196 *
197 * In case the gsmtap socket is not connected to a local IP/port, or
198 * creation of the receiving socket fails, a negative error code is
199 * returned.
200 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200201int gsmtap_source_add_sink_fd(int gsmtap_fd)
202{
203 struct sockaddr_storage ss;
204 socklen_t ss_len = sizeof(ss);
205 int rc;
206
207 rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len);
208 if (rc < 0)
209 return rc;
210
211 if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) {
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200212 rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM,
213 IPPROTO_UDP, OSMO_SOCK_F_BIND);
Harald Welte33cb71a2011-05-21 18:54:32 +0200214 if (rc >= 0)
215 return rc;
216 }
217
218 return -ENODEV;
219}
220
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200221/*! Send a \ref msgb through a GSMTAP source
Harald Welte47379ca2011-08-17 16:35:24 +0200222 * \param[in] gti GSMTAP instance
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100223 * \param[in] msg message buffer
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200224 * \return 0 in case of success; negative in case of error
Harald Welte47379ca2011-08-17 16:35:24 +0200225 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200226int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg)
227{
Harald Welte13692a62011-05-22 20:06:11 +0200228 if (!gti)
229 return -ENODEV;
230
Harald Welte33cb71a2011-05-21 18:54:32 +0200231 if (gti->ofd_wq_mode)
232 return osmo_wqueue_enqueue(&gti->wq, msg);
233 else {
234 /* try immediate send and return error if any */
235 int rc;
236
237 rc = write(gsmtap_inst_fd(gti), msg->data, msg->len);
238 if (rc <= 0) {
239 return rc;
240 } else if (rc >= msg->len) {
241 msgb_free(msg);
242 return 0;
243 } else {
244 /* short write */
245 return -EIO;
246 }
247 }
248}
249
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200250/*! send an arbitrary type through GSMTAP.
Sylvain Munautabf66e72011-09-26 13:23:19 +0200251 * See \ref gsmtap_makemsg_ex for arguments
252 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200253int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts,
Harald Welte33cb71a2011-05-21 18:54:32 +0200254 uint8_t chan_type, uint8_t ss, uint32_t fn,
255 int8_t signal_dbm, uint8_t snr, const uint8_t *data,
256 unsigned int len)
Harald Weltee34a9402010-06-29 22:31:21 +0200257{
258 struct msgb *msg;
259
Harald Welte13692a62011-05-22 20:06:11 +0200260 if (!gti)
261 return -ENODEV;
262
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200263 msg = gsmtap_makemsg_ex(type, arfcn, ts, chan_type, ss, fn, signal_dbm,
Harald Weltee34a9402010-06-29 22:31:21 +0200264 snr, data, len);
265 if (!msg)
266 return -ENOMEM;
267
Harald Welte33cb71a2011-05-21 18:54:32 +0200268 return gsmtap_sendmsg(gti, msg);
Harald Weltee779c362010-06-29 20:51:13 +0200269}
270
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200271/*! send a message from L1/L2 through GSMTAP.
Sylvain Munautabf66e72011-09-26 13:23:19 +0200272 * See \ref gsmtap_makemsg for arguments
273 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200274int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
275 uint8_t chan_type, uint8_t ss, uint32_t fn,
276 int8_t signal_dbm, uint8_t snr, const uint8_t *data,
277 unsigned int len)
278{
279 return gsmtap_send_ex(gti, GSMTAP_TYPE_UM, arfcn, ts, chan_type, ss, fn,
280 signal_dbm, snr, data, len);
281}
282
Harald Weltee779c362010-06-29 20:51:13 +0200283/* Callback from select layer if we can write to the socket */
Harald Welte33cb71a2011-05-21 18:54:32 +0200284static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg)
Harald Weltee779c362010-06-29 20:51:13 +0200285{
Harald Weltee779c362010-06-29 20:51:13 +0200286 int rc;
287
Harald Welte33cb71a2011-05-21 18:54:32 +0200288 rc = write(ofd->fd, msg->data, msg->len);
Harald Weltee779c362010-06-29 20:51:13 +0200289 if (rc < 0) {
Harald Weltee779c362010-06-29 20:51:13 +0200290 return rc;
291 }
292 if (rc != msg->len) {
Harald Weltee779c362010-06-29 20:51:13 +0200293 return -EIO;
294 }
295
Harald Weltee779c362010-06-29 20:51:13 +0200296 return 0;
297}
298
Harald Welted58ba462011-04-27 10:57:49 +0200299/* Callback from select layer if we can read from the sink socket */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200300static int gsmtap_sink_fd_cb(struct osmo_fd *fd, unsigned int flags)
Harald Welted58ba462011-04-27 10:57:49 +0200301{
302 int rc;
303 uint8_t buf[4096];
304
305 if (!(flags & BSC_FD_READ))
306 return 0;
307
308 rc = read(fd->fd, buf, sizeof(buf));
309 if (rc < 0) {
Harald Welted58ba462011-04-27 10:57:49 +0200310 return rc;
311 }
312 /* simply discard any data arriving on the socket */
313
314 return 0;
315}
316
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200317/*! Add a local sink to an existing GSMTAP source and return fd
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200318 * \param[in] gsmtap_fd file descriptor of the gsmtap socket
319 * \returns file descriptor of locally bound receive socket
320 *
321 * In case the GSMTAP socket is connected to a local destination
322 * IP/port, this function creates a corresponding receiving socket
323 * bound to that destination IP + port.
324 *
325 * In case the gsmtap socket is not connected to a local IP/port, or
326 * creation of the receiving socket fails, a negative error code is
327 * returned.
328 *
329 * The file descriptor of the receiving socket is automatically added
330 * to the libosmocore select() handling.
331 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200332int gsmtap_source_add_sink(struct gsmtap_inst *gti)
Harald Welted58ba462011-04-27 10:57:49 +0200333{
Harald Welte9d862c82016-11-26 00:10:07 +0100334 int fd, rc;
Harald Welted58ba462011-04-27 10:57:49 +0200335
Harald Welte33cb71a2011-05-21 18:54:32 +0200336 fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti));
337 if (fd < 0)
338 return fd;
Harald Welted58ba462011-04-27 10:57:49 +0200339
Harald Welte33cb71a2011-05-21 18:54:32 +0200340 if (gti->ofd_wq_mode) {
341 struct osmo_fd *sink_ofd;
342
343 sink_ofd = &gti->sink_ofd;
344 sink_ofd->fd = fd;
345 sink_ofd->when = BSC_FD_READ;
346 sink_ofd->cb = gsmtap_sink_fd_cb;
347
Harald Welte9d862c82016-11-26 00:10:07 +0100348 rc = osmo_fd_register(sink_ofd);
349 if (rc < 0) {
350 close(fd);
351 return rc;
352 }
Harald Welted58ba462011-04-27 10:57:49 +0200353 }
354
Harald Welte33cb71a2011-05-21 18:54:32 +0200355 return fd;
356}
Harald Welted58ba462011-04-27 10:57:49 +0200357
Harald Welte47379ca2011-08-17 16:35:24 +0200358
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200359/*! Open GSMTAP source socket, connect and register osmo_fd
Harald Welte47379ca2011-08-17 16:35:24 +0200360 * \param[in] host host name or IP address in string format
361 * \param[in] port UDP port number in host byte order
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100362 * \param[in] ofd_wq_mode Register \ref osmo_wqueue (1) or not (0)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200363 * \return callee-allocated \ref gsmtap_inst
Harald Welte47379ca2011-08-17 16:35:24 +0200364 *
365 * Open GSMTAP source (sending) socket, connect it to host/port,
366 * allocate 'struct gsmtap_inst' and optionally osmo_fd/osmo_wqueue
367 * registration. This means it is like \ref gsmtap_init2 but integrated
368 * with libosmocore \ref select */
Harald Welte33cb71a2011-05-21 18:54:32 +0200369struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port,
370 int ofd_wq_mode)
371{
372 struct gsmtap_inst *gti;
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_init_fd(host, port);
376 if (fd < 0)
377 return NULL;
378
379 gti = talloc_zero(NULL, struct gsmtap_inst);
380 gti->ofd_wq_mode = ofd_wq_mode;
381 gti->wq.bfd.fd = fd;
382 gti->sink_ofd.fd = -1;
383
384 if (ofd_wq_mode) {
385 osmo_wqueue_init(&gti->wq, 64);
386 gti->wq.write_cb = &gsmtap_wq_w_cb;
387
Harald Welte9d862c82016-11-26 00:10:07 +0100388 rc = osmo_fd_register(&gti->wq.bfd);
389 if (rc < 0) {
390 close(fd);
391 return NULL;
392 }
Harald Welte33cb71a2011-05-21 18:54:32 +0200393 }
394
395 return gti;
Harald Welted58ba462011-04-27 10:57:49 +0200396}
397
Harald Weltee4764422011-05-22 12:25:57 +0200398#endif /* HAVE_SYS_SOCKET_H */
Harald Weltede6e4982012-12-06 21:25:27 +0100399
400/*! @} */