blob: 336e1d031489b816d80135ce9787138bdc0857c1 [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 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Weltee779c362010-06-29 20:51:13 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
26#include "../config.h"
27
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010028#include <osmocom/core/gsmtap_util.h>
29#include <osmocom/core/logging.h>
30#include <osmocom/core/gsmtap.h>
31#include <osmocom/core/msgb.h>
Harald Welte33cb71a2011-05-21 18:54:32 +020032#include <osmocom/core/talloc.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010033#include <osmocom/core/select.h>
Harald Welte33cb71a2011-05-21 18:54:32 +020034#include <osmocom/core/socket.h>
Harald Welte95871da2017-05-15 12:11:36 +020035#include <osmocom/core/byteswap.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010036#include <osmocom/gsm/protocol/gsm_04_08.h>
37#include <osmocom/gsm/rsl.h>
Harald Weltee779c362010-06-29 20:51:13 +020038
Harald Welte33cb71a2011-05-21 18:54:32 +020039#include <sys/types.h>
Harald Weltee4764422011-05-22 12:25:57 +020040
Harald Weltee779c362010-06-29 20:51:13 +020041#include <stdio.h>
42#include <unistd.h>
43#include <stdint.h>
44#include <string.h>
45#include <errno.h>
46
Harald Welte47379ca2011-08-17 16:35:24 +020047/*! \addtogroup gsmtap
48 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020049 * GSMTAP utility routines. Encapsulates GSM messages over UDP.
50 *
51 * \file gsmtap_util.c */
Harald Welte47379ca2011-08-17 16:35:24 +020052
53
Neels Hofmeyr87e45502017-06-20 00:17:59 +020054/*! convert RSL channel number to GSMTAP channel type
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010055 * \param[in] rsl_chantype RSL channel type
Harald Welte47379ca2011-08-17 16:35:24 +020056 * \param[in] link_id RSL link identifier
Harald Welte67733042020-03-08 17:21:29 +010057 * \param[in] user_plane Is this voice/csd user plane (1) or signaling (0)
Harald Welte47379ca2011-08-17 16:35:24 +020058 * \returns GSMTAP channel type
59 */
Harald Welte67733042020-03-08 17:21:29 +010060uint8_t chantype_rsl2gsmtap2(uint8_t rsl_chantype, uint8_t link_id, bool user_plane)
Harald Weltee779c362010-06-29 20:51:13 +020061{
62 uint8_t ret = GSMTAP_CHANNEL_UNKNOWN;
63
64 switch (rsl_chantype) {
65 case RSL_CHAN_Bm_ACCHs:
Harald Welte67733042020-03-08 17:21:29 +010066 if (user_plane)
67 ret = GSMTAP_CHANNEL_VOICE_F;
68 else
69 ret = GSMTAP_CHANNEL_FACCH_F;
Harald Weltee779c362010-06-29 20:51:13 +020070 break;
71 case RSL_CHAN_Lm_ACCHs:
Harald Welte67733042020-03-08 17:21:29 +010072 if (user_plane)
73 ret = GSMTAP_CHANNEL_VOICE_H;
74 else
75 ret = GSMTAP_CHANNEL_FACCH_H;
Harald Weltee779c362010-06-29 20:51:13 +020076 break;
77 case RSL_CHAN_SDCCH4_ACCH:
78 ret = GSMTAP_CHANNEL_SDCCH4;
79 break;
80 case RSL_CHAN_SDCCH8_ACCH:
81 ret = GSMTAP_CHANNEL_SDCCH8;
82 break;
83 case RSL_CHAN_BCCH:
84 ret = GSMTAP_CHANNEL_BCCH;
85 break;
86 case RSL_CHAN_RACH:
87 ret = GSMTAP_CHANNEL_RACH;
88 break;
89 case RSL_CHAN_PCH_AGCH:
90 /* it could also be AGCH... */
91 ret = GSMTAP_CHANNEL_PCH;
92 break;
Harald Welte3b7cd0b2017-07-27 14:12:15 +020093 case RSL_CHAN_OSMO_PDCH:
94 ret = GSMTAP_CHANNEL_PDCH;
95 break;
Harald Welteac7fabe2020-02-26 17:24:02 +010096 case RSL_CHAN_OSMO_CBCH4:
97 ret = GSMTAP_CHANNEL_CBCH51;
98 break;
99 case RSL_CHAN_OSMO_CBCH8:
100 ret = GSMTAP_CHANNEL_CBCH52;
101 break;
Harald Weltee779c362010-06-29 20:51:13 +0200102 }
103
104 if (link_id & 0x40)
105 ret |= GSMTAP_CHANNEL_ACCH;
106
107 return ret;
108}
109
Harald Welte67733042020-03-08 17:21:29 +0100110/*! convert RSL channel number to GSMTAP channel type
111 * \param[in] rsl_chantype RSL channel type
112 * \param[in] link_id RSL link identifier
113 * \returns GSMTAP channel type
114 */
115uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id)
116{
117 return chantype_rsl2gsmtap2(rsl_chantype, link_id, false);
118}
119
Harald Welte93713a52017-07-12 23:43:40 +0200120/*! convert GSMTAP channel type to RSL channel number + Link ID
121 * \param[in] gsmtap_chantype GSMTAP channel type
122 * \param[out] rsl_chantype RSL channel mumber
123 * \param[out] link_id RSL link identifier
124 */
125void chantype_gsmtap2rsl(uint8_t gsmtap_chantype, uint8_t *rsl_chantype,
126 uint8_t *link_id)
127{
128 switch (gsmtap_chantype & ~GSMTAP_CHANNEL_ACCH & 0xff) {
Harald Welte67733042020-03-08 17:21:29 +0100129 case GSMTAP_CHANNEL_FACCH_F:
130 case GSMTAP_CHANNEL_VOICE_F: // TCH/F
Harald Welte93713a52017-07-12 23:43:40 +0200131 *rsl_chantype = RSL_CHAN_Bm_ACCHs;
132 break;
Harald Welte67733042020-03-08 17:21:29 +0100133 case GSMTAP_CHANNEL_FACCH_H:
134 case GSMTAP_CHANNEL_VOICE_H: // TCH/H
Harald Welte93713a52017-07-12 23:43:40 +0200135 *rsl_chantype = RSL_CHAN_Lm_ACCHs;
136 break;
137 case GSMTAP_CHANNEL_SDCCH4: // SDCCH/4
138 *rsl_chantype = RSL_CHAN_SDCCH4_ACCH;
139 break;
140 case GSMTAP_CHANNEL_SDCCH8: // SDCCH/8
141 *rsl_chantype = RSL_CHAN_SDCCH8_ACCH;
142 break;
143 case GSMTAP_CHANNEL_BCCH: // BCCH
144 *rsl_chantype = RSL_CHAN_BCCH;
145 break;
146 case GSMTAP_CHANNEL_RACH: // RACH
147 *rsl_chantype = RSL_CHAN_RACH;
148 break;
149 case GSMTAP_CHANNEL_PCH: // PCH
150 case GSMTAP_CHANNEL_AGCH: // AGCH
151 *rsl_chantype = RSL_CHAN_PCH_AGCH;
152 break;
153 case GSMTAP_CHANNEL_PDCH:
Harald Welte3b7cd0b2017-07-27 14:12:15 +0200154 *rsl_chantype = RSL_CHAN_OSMO_PDCH;
Harald Welte93713a52017-07-12 23:43:40 +0200155 break;
156 }
157
158 *link_id = gsmtap_chantype & GSMTAP_CHANNEL_ACCH ? 0x40 : 0x00;
159}
160
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200161/*! create an arbitrary type GSMTAP message
Sylvain Munautabf66e72011-09-26 13:23:19 +0200162 * \param[in] type The GSMTAP_TYPE_xxx constant of the message to create
Harald Welte47379ca2011-08-17 16:35:24 +0200163 * \param[in] arfcn GSM ARFCN (Channel Number)
164 * \param[in] ts GSM time slot
165 * \param[in] chan_type Channel Type
166 * \param[in] ss Sub-slot
167 * \param[in] fn GSM Frame Number
168 * \param[in] signal_dbm Signal Strength (dBm)
169 * \param[in] snr Signal/Noise Ratio (SNR)
170 * \param[in] data Pointer to data buffer
171 * \param[in] len Length of \ref data
Pau Espin Pedrol9a5d90a2020-06-30 18:16:33 +0200172 * \return dynamically allocated message buffer containing data
Harald Welte47379ca2011-08-17 16:35:24 +0200173 *
174 * This function will allocate a new msgb and fill it with a GSMTAP
175 * header containing the information
176 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200177struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type,
Harald Weltee34a9402010-06-29 22:31:21 +0200178 uint8_t ss, uint32_t fn, int8_t signal_dbm,
Vadim Yanitskiy833e8fa2021-01-04 17:40:05 +0100179 int8_t snr, const uint8_t *data, unsigned int len)
Harald Weltee779c362010-06-29 20:51:13 +0200180{
181 struct msgb *msg;
182 struct gsmtap_hdr *gh;
183 uint8_t *dst;
184
Harald Weltee779c362010-06-29 20:51:13 +0200185 msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
186 if (!msg)
Harald Weltee34a9402010-06-29 22:31:21 +0200187 return NULL;
Harald Weltee779c362010-06-29 20:51:13 +0200188
189 gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
190
191 gh->version = GSMTAP_VERSION;
192 gh->hdr_len = sizeof(*gh)/4;
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200193 gh->type = type;
Harald Weltee779c362010-06-29 20:51:13 +0200194 gh->timeslot = ts;
195 gh->sub_slot = ss;
Harald Welte95871da2017-05-15 12:11:36 +0200196 gh->arfcn = osmo_htons(arfcn);
Harald Weltee779c362010-06-29 20:51:13 +0200197 gh->snr_db = snr;
198 gh->signal_dbm = signal_dbm;
Harald Welte95871da2017-05-15 12:11:36 +0200199 gh->frame_number = osmo_htonl(fn);
Harald Weltee779c362010-06-29 20:51:13 +0200200 gh->sub_type = chan_type;
201 gh->antenna_nr = 0;
202
203 dst = msgb_put(msg, len);
204 memcpy(dst, data, len);
205
Harald Weltee34a9402010-06-29 22:31:21 +0200206 return msg;
207}
208
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200209/*! create L1/L2 data and put it into GSMTAP
Sylvain Munautabf66e72011-09-26 13:23:19 +0200210 * \param[in] arfcn GSM ARFCN (Channel Number)
211 * \param[in] ts GSM time slot
212 * \param[in] chan_type Channel Type
213 * \param[in] ss Sub-slot
214 * \param[in] fn GSM Frame Number
215 * \param[in] signal_dbm Signal Strength (dBm)
216 * \param[in] snr Signal/Noise Ratio (SNR)
217 * \param[in] data Pointer to data buffer
218 * \param[in] len Length of \ref data
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200219 * \return message buffer or NULL in case of error
Sylvain Munautabf66e72011-09-26 13:23:19 +0200220 *
221 * This function will allocate a new msgb and fill it with a GSMTAP
222 * header containing the information
223 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200224struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
225 uint8_t ss, uint32_t fn, int8_t signal_dbm,
Vadim Yanitskiy833e8fa2021-01-04 17:40:05 +0100226 int8_t snr, const uint8_t *data, unsigned int len)
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200227{
228 return gsmtap_makemsg_ex(GSMTAP_TYPE_UM, arfcn, ts, chan_type,
229 ss, fn, signal_dbm, snr, data, len);
230}
231
Harald Weltee4764422011-05-22 12:25:57 +0200232#ifdef HAVE_SYS_SOCKET_H
233
234#include <sys/socket.h>
235#include <netinet/in.h>
236
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200237/*! Create a new (sending) GSMTAP source socket
Harald Welte47379ca2011-08-17 16:35:24 +0200238 * \param[in] host host name or IP address in string format
239 * \param[in] port UDP port number in host byte order
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200240 * \return file descriptor of the new socket
Harald Welte47379ca2011-08-17 16:35:24 +0200241 *
242 * Opens a GSMTAP source (sending) socket, conncet it to host/port and
243 * return resulting fd. If \a host is NULL, the destination address
244 * will be localhost. If \a port is 0, the default \ref
245 * GSMTAP_UDP_PORT will be used.
246 * */
Harald Welte33cb71a2011-05-21 18:54:32 +0200247int gsmtap_source_init_fd(const char *host, uint16_t port)
248{
249 if (port == 0)
250 port = GSMTAP_UDP_PORT;
251 if (host == NULL)
252 host = "localhost";
253
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200254 return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port,
255 OSMO_SOCK_F_CONNECT);
Harald Welte33cb71a2011-05-21 18:54:32 +0200256}
257
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200258/*! Add a local sink to an existing GSMTAP source and return fd
Harald Weltede6e4982012-12-06 21:25:27 +0100259 * \param[in] gsmtap_fd file descriptor of the gsmtap socket
260 * \returns file descriptor of locally bound receive socket
261 *
262 * In case the GSMTAP socket is connected to a local destination
263 * IP/port, this function creates a corresponding receiving socket
264 * bound to that destination IP + port.
265 *
266 * In case the gsmtap socket is not connected to a local IP/port, or
267 * creation of the receiving socket fails, a negative error code is
268 * returned.
269 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200270int gsmtap_source_add_sink_fd(int gsmtap_fd)
271{
272 struct sockaddr_storage ss;
273 socklen_t ss_len = sizeof(ss);
274 int rc;
275
276 rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len);
277 if (rc < 0)
278 return rc;
279
280 if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) {
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200281 rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM,
Philipp Maierb8a91622018-08-23 20:18:55 +0200282 IPPROTO_UDP,
283 OSMO_SOCK_F_BIND |
284 OSMO_SOCK_F_UDP_REUSEADDR);
Harald Welte33cb71a2011-05-21 18:54:32 +0200285 if (rc >= 0)
286 return rc;
287 }
288
289 return -ENODEV;
290}
291
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200292/*! Send a \ref msgb through a GSMTAP source
Harald Welte47379ca2011-08-17 16:35:24 +0200293 * \param[in] gti GSMTAP instance
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100294 * \param[in] msg message buffer
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200295 * \return 0 in case of success; negative in case of error
Neels Hofmeyrc9a4ce62018-02-16 01:23:29 +0100296 * NOTE: in case of nonzero return value, the *caller* must free the msg!
297 * (This enables the caller to attempt re-sending the message.)
298 * If 0 is returned, the msgb was freed by this function.
Harald Welte47379ca2011-08-17 16:35:24 +0200299 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200300int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg)
301{
Harald Welte13692a62011-05-22 20:06:11 +0200302 if (!gti)
303 return -ENODEV;
304
Harald Welte33cb71a2011-05-21 18:54:32 +0200305 if (gti->ofd_wq_mode)
306 return osmo_wqueue_enqueue(&gti->wq, msg);
307 else {
308 /* try immediate send and return error if any */
309 int rc;
310
311 rc = write(gsmtap_inst_fd(gti), msg->data, msg->len);
Neels Hofmeyr90539ac2018-02-16 01:24:03 +0100312 if (rc < 0) {
Harald Welte33cb71a2011-05-21 18:54:32 +0200313 return rc;
314 } else if (rc >= msg->len) {
315 msgb_free(msg);
316 return 0;
317 } else {
318 /* short write */
319 return -EIO;
320 }
321 }
322}
323
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200324/*! send an arbitrary type through GSMTAP.
Sylvain Munautabf66e72011-09-26 13:23:19 +0200325 * See \ref gsmtap_makemsg_ex for arguments
326 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200327int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts,
Harald Welte33cb71a2011-05-21 18:54:32 +0200328 uint8_t chan_type, uint8_t ss, uint32_t fn,
Vadim Yanitskiy833e8fa2021-01-04 17:40:05 +0100329 int8_t signal_dbm, int8_t snr, const uint8_t *data,
Harald Welte33cb71a2011-05-21 18:54:32 +0200330 unsigned int len)
Harald Weltee34a9402010-06-29 22:31:21 +0200331{
332 struct msgb *msg;
Neels Hofmeyra4952aa2018-02-16 01:26:00 +0100333 int rc;
Harald Weltee34a9402010-06-29 22:31:21 +0200334
Harald Welte13692a62011-05-22 20:06:11 +0200335 if (!gti)
336 return -ENODEV;
337
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200338 msg = gsmtap_makemsg_ex(type, arfcn, ts, chan_type, ss, fn, signal_dbm,
Harald Weltee34a9402010-06-29 22:31:21 +0200339 snr, data, len);
340 if (!msg)
341 return -ENOMEM;
342
Neels Hofmeyra4952aa2018-02-16 01:26:00 +0100343 rc = gsmtap_sendmsg(gti, msg);
344 if (rc)
345 msgb_free(msg);
346 return rc;
Harald Weltee779c362010-06-29 20:51:13 +0200347}
348
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200349/*! send a message from L1/L2 through GSMTAP.
Sylvain Munautabf66e72011-09-26 13:23:19 +0200350 * See \ref gsmtap_makemsg for arguments
351 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200352int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
353 uint8_t chan_type, uint8_t ss, uint32_t fn,
Vadim Yanitskiy833e8fa2021-01-04 17:40:05 +0100354 int8_t signal_dbm, int8_t snr, const uint8_t *data,
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200355 unsigned int len)
356{
357 return gsmtap_send_ex(gti, GSMTAP_TYPE_UM, arfcn, ts, chan_type, ss, fn,
358 signal_dbm, snr, data, len);
359}
360
Harald Weltee779c362010-06-29 20:51:13 +0200361/* Callback from select layer if we can write to the socket */
Harald Welte33cb71a2011-05-21 18:54:32 +0200362static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg)
Harald Weltee779c362010-06-29 20:51:13 +0200363{
Harald Weltee779c362010-06-29 20:51:13 +0200364 int rc;
365
Harald Welte33cb71a2011-05-21 18:54:32 +0200366 rc = write(ofd->fd, msg->data, msg->len);
Harald Weltee779c362010-06-29 20:51:13 +0200367 if (rc < 0) {
Harald Weltee779c362010-06-29 20:51:13 +0200368 return rc;
369 }
370 if (rc != msg->len) {
Harald Weltee779c362010-06-29 20:51:13 +0200371 return -EIO;
372 }
373
Harald Weltee779c362010-06-29 20:51:13 +0200374 return 0;
375}
376
Harald Welted58ba462011-04-27 10:57:49 +0200377/* Callback from select layer if we can read from the sink socket */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200378static int gsmtap_sink_fd_cb(struct osmo_fd *fd, unsigned int flags)
Harald Welted58ba462011-04-27 10:57:49 +0200379{
380 int rc;
381 uint8_t buf[4096];
382
Harald Welte16886992019-03-20 10:26:39 +0100383 if (!(flags & OSMO_FD_READ))
Harald Welted58ba462011-04-27 10:57:49 +0200384 return 0;
385
386 rc = read(fd->fd, buf, sizeof(buf));
387 if (rc < 0) {
Harald Welted58ba462011-04-27 10:57:49 +0200388 return rc;
389 }
390 /* simply discard any data arriving on the socket */
391
392 return 0;
393}
394
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200395/*! Add a local sink to an existing GSMTAP source and return fd
Vadim Yanitskiy2f65bb12019-03-25 15:57:09 +0700396 * \param[in] gti existing GSMTAP source
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200397 * \returns file descriptor of locally bound receive socket
398 *
399 * In case the GSMTAP socket is connected to a local destination
400 * IP/port, this function creates a corresponding receiving socket
401 * bound to that destination IP + port.
402 *
403 * In case the gsmtap socket is not connected to a local IP/port, or
404 * creation of the receiving socket fails, a negative error code is
405 * returned.
406 *
407 * The file descriptor of the receiving socket is automatically added
408 * to the libosmocore select() handling.
409 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200410int gsmtap_source_add_sink(struct gsmtap_inst *gti)
Harald Welted58ba462011-04-27 10:57:49 +0200411{
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_add_sink_fd(gsmtap_inst_fd(gti));
415 if (fd < 0)
416 return fd;
Harald Welted58ba462011-04-27 10:57:49 +0200417
Harald Welte33cb71a2011-05-21 18:54:32 +0200418 if (gti->ofd_wq_mode) {
419 struct osmo_fd *sink_ofd;
420
421 sink_ofd = &gti->sink_ofd;
422 sink_ofd->fd = fd;
Harald Welte16886992019-03-20 10:26:39 +0100423 sink_ofd->when = OSMO_FD_READ;
Harald Welte33cb71a2011-05-21 18:54:32 +0200424 sink_ofd->cb = gsmtap_sink_fd_cb;
425
Harald Welte9d862c82016-11-26 00:10:07 +0100426 rc = osmo_fd_register(sink_ofd);
427 if (rc < 0) {
428 close(fd);
429 return rc;
430 }
Harald Welted58ba462011-04-27 10:57:49 +0200431 }
432
Harald Welte33cb71a2011-05-21 18:54:32 +0200433 return fd;
434}
Harald Welted58ba462011-04-27 10:57:49 +0200435
Harald Welte47379ca2011-08-17 16:35:24 +0200436
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200437/*! Open GSMTAP source socket, connect and register osmo_fd
Harald Welte47379ca2011-08-17 16:35:24 +0200438 * \param[in] host host name or IP address in string format
439 * \param[in] port UDP port number in host byte order
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100440 * \param[in] ofd_wq_mode Register \ref osmo_wqueue (1) or not (0)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200441 * \return callee-allocated \ref gsmtap_inst
Harald Welte47379ca2011-08-17 16:35:24 +0200442 *
443 * Open GSMTAP source (sending) socket, connect it to host/port,
444 * allocate 'struct gsmtap_inst' and optionally osmo_fd/osmo_wqueue
Vadim Yanitskiy2f65bb12019-03-25 15:57:09 +0700445 * registration.
446 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200447struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port,
448 int ofd_wq_mode)
449{
450 struct gsmtap_inst *gti;
Harald Welte9d862c82016-11-26 00:10:07 +0100451 int fd, rc;
Harald Welted58ba462011-04-27 10:57:49 +0200452
Harald Welte33cb71a2011-05-21 18:54:32 +0200453 fd = gsmtap_source_init_fd(host, port);
454 if (fd < 0)
455 return NULL;
456
457 gti = talloc_zero(NULL, struct gsmtap_inst);
458 gti->ofd_wq_mode = ofd_wq_mode;
459 gti->wq.bfd.fd = fd;
460 gti->sink_ofd.fd = -1;
461
462 if (ofd_wq_mode) {
463 osmo_wqueue_init(&gti->wq, 64);
464 gti->wq.write_cb = &gsmtap_wq_w_cb;
465
Harald Welte9d862c82016-11-26 00:10:07 +0100466 rc = osmo_fd_register(&gti->wq.bfd);
467 if (rc < 0) {
Vadim Yanitskiyb9baf022019-03-24 00:01:27 +0700468 talloc_free(gti);
Harald Welte9d862c82016-11-26 00:10:07 +0100469 close(fd);
470 return NULL;
471 }
Harald Welte33cb71a2011-05-21 18:54:32 +0200472 }
473
474 return gti;
Harald Welted58ba462011-04-27 10:57:49 +0200475}
476
Harald Weltee4764422011-05-22 12:25:57 +0200477#endif /* HAVE_SYS_SOCKET_H */
Harald Weltede6e4982012-12-06 21:25:27 +0100478
Harald Welteaa3ba462017-07-13 00:01:02 +0200479const struct value_string gsmtap_gsm_channel_names[] = {
480 { GSMTAP_CHANNEL_UNKNOWN, "UNKNOWN" },
481 { GSMTAP_CHANNEL_BCCH, "BCCH" },
482 { GSMTAP_CHANNEL_CCCH, "CCCH" },
483 { GSMTAP_CHANNEL_RACH, "RACH" },
484 { GSMTAP_CHANNEL_AGCH, "AGCH" },
485 { GSMTAP_CHANNEL_PCH, "PCH" },
486 { GSMTAP_CHANNEL_SDCCH, "SDCCH" },
487 { GSMTAP_CHANNEL_SDCCH4, "SDCCH/4" },
488 { GSMTAP_CHANNEL_SDCCH8, "SDCCH/8" },
Harald Welte67733042020-03-08 17:21:29 +0100489 { GSMTAP_CHANNEL_FACCH_F, "FACCH/F" },
490 { GSMTAP_CHANNEL_FACCH_H, "FACCH/H" },
Harald Welteaa3ba462017-07-13 00:01:02 +0200491 { GSMTAP_CHANNEL_PACCH, "PACCH" },
492 { GSMTAP_CHANNEL_CBCH52, "CBCH" },
493 { GSMTAP_CHANNEL_PDCH, "PDCH" } ,
494 { GSMTAP_CHANNEL_PTCCH, "PTTCH" },
495 { GSMTAP_CHANNEL_CBCH51, "CBCH" },
496 { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH, "LSACCH" },
497 { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH4, "SACCH/4" },
498 { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH8, "SACCH/8" },
Harald Welte67733042020-03-08 17:21:29 +0100499 { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_FACCH_F, "SACCH/F" },
500 { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_FACCH_H, "SACCH/H" },
501 { GSMTAP_CHANNEL_VOICE_F, "TCH/F" },
502 { GSMTAP_CHANNEL_VOICE_H, "TCH/H" },
Harald Welteaa3ba462017-07-13 00:01:02 +0200503 { 0, NULL }
504};
505
506/* for debugging */
507const struct value_string gsmtap_type_names[] = {
508 { GSMTAP_TYPE_UM, "GSM Um (MS<->BTS)" },
509 { GSMTAP_TYPE_ABIS, "GSM Abis (BTS<->BSC)" },
510 { GSMTAP_TYPE_UM_BURST, "GSM Um burst (MS<->BTS)" },
511 { GSMTAP_TYPE_SIM, "SIM Card" },
512 { GSMTAP_TYPE_TETRA_I1, "TETRA V+D" },
513 { GSMTAP_TYPE_TETRA_I1_BURST, "TETRA bursts" },
514 { GSMTAP_TYPE_WMX_BURST, "WiMAX burst" },
515 { GSMTAP_TYPE_GMR1_UM, "GMR-1 air interfeace (MES-MS<->GTS)"},
516 { GSMTAP_TYPE_UMTS_RLC_MAC, "UMTS RLC/MAC" },
517 { GSMTAP_TYPE_UMTS_RRC, "UMTS RRC" },
518 { GSMTAP_TYPE_LTE_RRC, "LTE RRC" },
519 { GSMTAP_TYPE_LTE_MAC, "LTE MAC" },
520 { GSMTAP_TYPE_LTE_MAC_FRAMED, "LTE MAC with context hdr" },
521 { GSMTAP_TYPE_OSMOCORE_LOG, "libosmocore logging" },
522 { GSMTAP_TYPE_QC_DIAG, "Qualcomm DIAG" },
523 { 0, NULL }
524};
525
Harald Weltede6e4982012-12-06 21:25:27 +0100526/*! @} */