blob: 74f3b48d83d0197faeffd43a36f54319218296c5 [file] [log] [blame]
Harald Welte33cb71a2011-05-21 18:54:32 +02001/* GSMTAP support code in libmsomcore */
Harald Weltee779c362010-06-29 20:51:13 +02002/*
Harald Welte33cb71a2011-05-21 18:54:32 +02003 * (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
Harald Weltee779c362010-06-29 20:51:13 +02004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include "../config.h"
24
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010025#include <osmocom/core/gsmtap_util.h>
26#include <osmocom/core/logging.h>
27#include <osmocom/core/gsmtap.h>
28#include <osmocom/core/msgb.h>
Harald Welte33cb71a2011-05-21 18:54:32 +020029#include <osmocom/core/talloc.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010030#include <osmocom/core/select.h>
Harald Welte33cb71a2011-05-21 18:54:32 +020031#include <osmocom/core/socket.h>
Harald Welte95871da2017-05-15 12:11:36 +020032#include <osmocom/core/byteswap.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010033#include <osmocom/gsm/protocol/gsm_04_08.h>
34#include <osmocom/gsm/rsl.h>
Harald Weltee779c362010-06-29 20:51:13 +020035
Harald Welte33cb71a2011-05-21 18:54:32 +020036#include <sys/types.h>
Harald Weltee4764422011-05-22 12:25:57 +020037
Harald Weltee779c362010-06-29 20:51:13 +020038#include <stdio.h>
39#include <unistd.h>
40#include <stdint.h>
41#include <string.h>
42#include <errno.h>
43
Harald Welte47379ca2011-08-17 16:35:24 +020044/*! \addtogroup gsmtap
45 * @{
46 */
47/*! \file gsmtap_util.c */
48
49
50/*! \brief convert RSL channel number to GSMTAP channel type
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010051 * \param[in] rsl_chantype RSL channel type
Harald Welte47379ca2011-08-17 16:35:24 +020052 * \param[in] link_id RSL link identifier
53 * \returns GSMTAP channel type
54 */
Harald Weltee779c362010-06-29 20:51:13 +020055uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id)
56{
57 uint8_t ret = GSMTAP_CHANNEL_UNKNOWN;
58
59 switch (rsl_chantype) {
60 case RSL_CHAN_Bm_ACCHs:
61 ret = GSMTAP_CHANNEL_TCH_F;
62 break;
63 case RSL_CHAN_Lm_ACCHs:
64 ret = GSMTAP_CHANNEL_TCH_H;
65 break;
66 case RSL_CHAN_SDCCH4_ACCH:
67 ret = GSMTAP_CHANNEL_SDCCH4;
68 break;
69 case RSL_CHAN_SDCCH8_ACCH:
70 ret = GSMTAP_CHANNEL_SDCCH8;
71 break;
72 case RSL_CHAN_BCCH:
73 ret = GSMTAP_CHANNEL_BCCH;
74 break;
75 case RSL_CHAN_RACH:
76 ret = GSMTAP_CHANNEL_RACH;
77 break;
78 case RSL_CHAN_PCH_AGCH:
79 /* it could also be AGCH... */
80 ret = GSMTAP_CHANNEL_PCH;
81 break;
82 }
83
84 if (link_id & 0x40)
85 ret |= GSMTAP_CHANNEL_ACCH;
86
87 return ret;
88}
89
Sylvain Munautabf66e72011-09-26 13:23:19 +020090/*! \brief create an arbitrary type GSMTAP message
91 * \param[in] type The GSMTAP_TYPE_xxx constant of the message to create
Harald Welte47379ca2011-08-17 16:35:24 +020092 * \param[in] arfcn GSM ARFCN (Channel Number)
93 * \param[in] ts GSM time slot
94 * \param[in] chan_type Channel Type
95 * \param[in] ss Sub-slot
96 * \param[in] fn GSM Frame Number
97 * \param[in] signal_dbm Signal Strength (dBm)
98 * \param[in] snr Signal/Noise Ratio (SNR)
99 * \param[in] data Pointer to data buffer
100 * \param[in] len Length of \ref data
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200101 * \return dynamically allocated message buffer containing data
Harald Welte47379ca2011-08-17 16:35:24 +0200102 *
103 * This function will allocate a new msgb and fill it with a GSMTAP
104 * header containing the information
105 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200106struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type,
Harald Weltee34a9402010-06-29 22:31:21 +0200107 uint8_t ss, uint32_t fn, int8_t signal_dbm,
108 uint8_t snr, const uint8_t *data, unsigned int len)
Harald Weltee779c362010-06-29 20:51:13 +0200109{
110 struct msgb *msg;
111 struct gsmtap_hdr *gh;
112 uint8_t *dst;
113
Harald Weltee779c362010-06-29 20:51:13 +0200114 msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
115 if (!msg)
Harald Weltee34a9402010-06-29 22:31:21 +0200116 return NULL;
Harald Weltee779c362010-06-29 20:51:13 +0200117
118 gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
119
120 gh->version = GSMTAP_VERSION;
121 gh->hdr_len = sizeof(*gh)/4;
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200122 gh->type = type;
Harald Weltee779c362010-06-29 20:51:13 +0200123 gh->timeslot = ts;
124 gh->sub_slot = ss;
Harald Welte95871da2017-05-15 12:11:36 +0200125 gh->arfcn = osmo_htons(arfcn);
Harald Weltee779c362010-06-29 20:51:13 +0200126 gh->snr_db = snr;
127 gh->signal_dbm = signal_dbm;
Harald Welte95871da2017-05-15 12:11:36 +0200128 gh->frame_number = osmo_htonl(fn);
Harald Weltee779c362010-06-29 20:51:13 +0200129 gh->sub_type = chan_type;
130 gh->antenna_nr = 0;
131
132 dst = msgb_put(msg, len);
133 memcpy(dst, data, len);
134
Harald Weltee34a9402010-06-29 22:31:21 +0200135 return msg;
136}
137
Sylvain Munautabf66e72011-09-26 13:23:19 +0200138/*! \brief create L1/L2 data and put it into GSMTAP
139 * \param[in] arfcn GSM ARFCN (Channel Number)
140 * \param[in] ts GSM time slot
141 * \param[in] chan_type Channel Type
142 * \param[in] ss Sub-slot
143 * \param[in] fn GSM Frame Number
144 * \param[in] signal_dbm Signal Strength (dBm)
145 * \param[in] snr Signal/Noise Ratio (SNR)
146 * \param[in] data Pointer to data buffer
147 * \param[in] len Length of \ref data
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200148 * \return message buffer or NULL in case of error
Sylvain Munautabf66e72011-09-26 13:23:19 +0200149 *
150 * This function will allocate a new msgb and fill it with a GSMTAP
151 * header containing the information
152 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200153struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
154 uint8_t ss, uint32_t fn, int8_t signal_dbm,
155 uint8_t snr, const uint8_t *data, unsigned int len)
156{
157 return gsmtap_makemsg_ex(GSMTAP_TYPE_UM, arfcn, ts, chan_type,
158 ss, fn, signal_dbm, snr, data, len);
159}
160
Harald Weltee4764422011-05-22 12:25:57 +0200161#ifdef HAVE_SYS_SOCKET_H
162
163#include <sys/socket.h>
164#include <netinet/in.h>
165
Harald Welte47379ca2011-08-17 16:35:24 +0200166/*! \brief Create a new (sending) GSMTAP source socket
167 * \param[in] host host name or IP address in string format
168 * \param[in] port UDP port number in host byte order
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200169 * \return file descriptor of the new socket
Harald Welte47379ca2011-08-17 16:35:24 +0200170 *
171 * Opens a GSMTAP source (sending) socket, conncet it to host/port and
172 * return resulting fd. If \a host is NULL, the destination address
173 * will be localhost. If \a port is 0, the default \ref
174 * GSMTAP_UDP_PORT will be used.
175 * */
Harald Welte33cb71a2011-05-21 18:54:32 +0200176int gsmtap_source_init_fd(const char *host, uint16_t port)
177{
178 if (port == 0)
179 port = GSMTAP_UDP_PORT;
180 if (host == NULL)
181 host = "localhost";
182
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200183 return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port,
184 OSMO_SOCK_F_CONNECT);
Harald Welte33cb71a2011-05-21 18:54:32 +0200185}
186
Harald Weltede6e4982012-12-06 21:25:27 +0100187/*! \brief Add a local sink to an existing GSMTAP source and return fd
188 * \param[in] gsmtap_fd file descriptor of the gsmtap socket
189 * \returns file descriptor of locally bound receive socket
190 *
191 * In case the GSMTAP socket is connected to a local destination
192 * IP/port, this function creates a corresponding receiving socket
193 * bound to that destination IP + port.
194 *
195 * In case the gsmtap socket is not connected to a local IP/port, or
196 * creation of the receiving socket fails, a negative error code is
197 * returned.
198 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200199int gsmtap_source_add_sink_fd(int gsmtap_fd)
200{
201 struct sockaddr_storage ss;
202 socklen_t ss_len = sizeof(ss);
203 int rc;
204
205 rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len);
206 if (rc < 0)
207 return rc;
208
209 if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) {
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200210 rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM,
211 IPPROTO_UDP, OSMO_SOCK_F_BIND);
Harald Welte33cb71a2011-05-21 18:54:32 +0200212 if (rc >= 0)
213 return rc;
214 }
215
216 return -ENODEV;
217}
218
Harald Welte47379ca2011-08-17 16:35:24 +0200219/*! \brief Send a \ref msgb through a GSMTAP source
220 * \param[in] gti GSMTAP instance
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100221 * \param[in] msg message buffer
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200222 * \return 0 in case of success; negative in case of error
Harald Welte47379ca2011-08-17 16:35:24 +0200223 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200224int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg)
225{
Harald Welte13692a62011-05-22 20:06:11 +0200226 if (!gti)
227 return -ENODEV;
228
Harald Welte33cb71a2011-05-21 18:54:32 +0200229 if (gti->ofd_wq_mode)
230 return osmo_wqueue_enqueue(&gti->wq, msg);
231 else {
232 /* try immediate send and return error if any */
233 int rc;
234
235 rc = write(gsmtap_inst_fd(gti), msg->data, msg->len);
236 if (rc <= 0) {
237 return rc;
238 } else if (rc >= msg->len) {
239 msgb_free(msg);
240 return 0;
241 } else {
242 /* short write */
243 return -EIO;
244 }
245 }
246}
247
Sylvain Munautabf66e72011-09-26 13:23:19 +0200248/*! \brief send an arbitrary type through GSMTAP.
249 * See \ref gsmtap_makemsg_ex for arguments
250 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200251int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts,
Harald Welte33cb71a2011-05-21 18:54:32 +0200252 uint8_t chan_type, uint8_t ss, uint32_t fn,
253 int8_t signal_dbm, uint8_t snr, const uint8_t *data,
254 unsigned int len)
Harald Weltee34a9402010-06-29 22:31:21 +0200255{
256 struct msgb *msg;
257
Harald Welte13692a62011-05-22 20:06:11 +0200258 if (!gti)
259 return -ENODEV;
260
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200261 msg = gsmtap_makemsg_ex(type, arfcn, ts, chan_type, ss, fn, signal_dbm,
Harald Weltee34a9402010-06-29 22:31:21 +0200262 snr, data, len);
263 if (!msg)
264 return -ENOMEM;
265
Harald Welte33cb71a2011-05-21 18:54:32 +0200266 return gsmtap_sendmsg(gti, msg);
Harald Weltee779c362010-06-29 20:51:13 +0200267}
268
Sylvain Munautabf66e72011-09-26 13:23:19 +0200269/*! \brief send a message from L1/L2 through GSMTAP.
270 * See \ref gsmtap_makemsg for arguments
271 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200272int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
273 uint8_t chan_type, uint8_t ss, uint32_t fn,
274 int8_t signal_dbm, uint8_t snr, const uint8_t *data,
275 unsigned int len)
276{
277 return gsmtap_send_ex(gti, GSMTAP_TYPE_UM, arfcn, ts, chan_type, ss, fn,
278 signal_dbm, snr, data, len);
279}
280
Harald Weltee779c362010-06-29 20:51:13 +0200281/* Callback from select layer if we can write to the socket */
Harald Welte33cb71a2011-05-21 18:54:32 +0200282static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg)
Harald Weltee779c362010-06-29 20:51:13 +0200283{
Harald Weltee779c362010-06-29 20:51:13 +0200284 int rc;
285
Harald Welte33cb71a2011-05-21 18:54:32 +0200286 rc = write(ofd->fd, msg->data, msg->len);
Harald Weltee779c362010-06-29 20:51:13 +0200287 if (rc < 0) {
Harald Weltee779c362010-06-29 20:51:13 +0200288 return rc;
289 }
290 if (rc != msg->len) {
Harald Weltee779c362010-06-29 20:51:13 +0200291 return -EIO;
292 }
293
Harald Weltee779c362010-06-29 20:51:13 +0200294 return 0;
295}
296
Harald Welted58ba462011-04-27 10:57:49 +0200297/* Callback from select layer if we can read from the sink socket */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200298static int gsmtap_sink_fd_cb(struct osmo_fd *fd, unsigned int flags)
Harald Welted58ba462011-04-27 10:57:49 +0200299{
300 int rc;
301 uint8_t buf[4096];
302
303 if (!(flags & BSC_FD_READ))
304 return 0;
305
306 rc = read(fd->fd, buf, sizeof(buf));
307 if (rc < 0) {
Harald Welted58ba462011-04-27 10:57:49 +0200308 return rc;
309 }
310 /* simply discard any data arriving on the socket */
311
312 return 0;
313}
314
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200315/*! \brief Add a local sink to an existing GSMTAP source and return fd
316 * \param[in] gsmtap_fd file descriptor of the gsmtap socket
317 * \returns file descriptor of locally bound receive socket
318 *
319 * In case the GSMTAP socket is connected to a local destination
320 * IP/port, this function creates a corresponding receiving socket
321 * bound to that destination IP + port.
322 *
323 * In case the gsmtap socket is not connected to a local IP/port, or
324 * creation of the receiving socket fails, a negative error code is
325 * returned.
326 *
327 * The file descriptor of the receiving socket is automatically added
328 * to the libosmocore select() handling.
329 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200330int gsmtap_source_add_sink(struct gsmtap_inst *gti)
Harald Welted58ba462011-04-27 10:57:49 +0200331{
Harald Welte9d862c82016-11-26 00:10:07 +0100332 int fd, rc;
Harald Welted58ba462011-04-27 10:57:49 +0200333
Harald Welte33cb71a2011-05-21 18:54:32 +0200334 fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti));
335 if (fd < 0)
336 return fd;
Harald Welted58ba462011-04-27 10:57:49 +0200337
Harald Welte33cb71a2011-05-21 18:54:32 +0200338 if (gti->ofd_wq_mode) {
339 struct osmo_fd *sink_ofd;
340
341 sink_ofd = &gti->sink_ofd;
342 sink_ofd->fd = fd;
343 sink_ofd->when = BSC_FD_READ;
344 sink_ofd->cb = gsmtap_sink_fd_cb;
345
Harald Welte9d862c82016-11-26 00:10:07 +0100346 rc = osmo_fd_register(sink_ofd);
347 if (rc < 0) {
348 close(fd);
349 return rc;
350 }
Harald Welted58ba462011-04-27 10:57:49 +0200351 }
352
Harald Welte33cb71a2011-05-21 18:54:32 +0200353 return fd;
354}
Harald Welted58ba462011-04-27 10:57:49 +0200355
Harald Welte47379ca2011-08-17 16:35:24 +0200356
357/*! \brief Open GSMTAP source socket, connect and register osmo_fd
358 * \param[in] host host name or IP address in string format
359 * \param[in] port UDP port number in host byte order
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100360 * \param[in] ofd_wq_mode Register \ref osmo_wqueue (1) or not (0)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200361 * \return callee-allocated \ref gsmtap_inst
Harald Welte47379ca2011-08-17 16:35:24 +0200362 *
363 * Open GSMTAP source (sending) socket, connect it to host/port,
364 * allocate 'struct gsmtap_inst' and optionally osmo_fd/osmo_wqueue
365 * registration. This means it is like \ref gsmtap_init2 but integrated
366 * with libosmocore \ref select */
Harald Welte33cb71a2011-05-21 18:54:32 +0200367struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port,
368 int ofd_wq_mode)
369{
370 struct gsmtap_inst *gti;
Harald Welte9d862c82016-11-26 00:10:07 +0100371 int fd, rc;
Harald Welted58ba462011-04-27 10:57:49 +0200372
Harald Welte33cb71a2011-05-21 18:54:32 +0200373 fd = gsmtap_source_init_fd(host, port);
374 if (fd < 0)
375 return NULL;
376
377 gti = talloc_zero(NULL, struct gsmtap_inst);
378 gti->ofd_wq_mode = ofd_wq_mode;
379 gti->wq.bfd.fd = fd;
380 gti->sink_ofd.fd = -1;
381
382 if (ofd_wq_mode) {
383 osmo_wqueue_init(&gti->wq, 64);
384 gti->wq.write_cb = &gsmtap_wq_w_cb;
385
Harald Welte9d862c82016-11-26 00:10:07 +0100386 rc = osmo_fd_register(&gti->wq.bfd);
387 if (rc < 0) {
388 close(fd);
389 return NULL;
390 }
Harald Welte33cb71a2011-05-21 18:54:32 +0200391 }
392
393 return gti;
Harald Welted58ba462011-04-27 10:57:49 +0200394}
395
Harald Weltee4764422011-05-22 12:25:57 +0200396#endif /* HAVE_SYS_SOCKET_H */
Harald Weltede6e4982012-12-06 21:25:27 +0100397
398/*! @} */