blob: e5adb2f6a82e2f6f57dc55bc2da2d1d53c047386 [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:
Vadim Yanitskiyfc02ff42021-05-26 18:19:03 +020066 case RSL_CHAN_OSMO_VAMOS_Bm_ACCHs:
Harald Welte67733042020-03-08 17:21:29 +010067 if (user_plane)
68 ret = GSMTAP_CHANNEL_VOICE_F;
69 else
70 ret = GSMTAP_CHANNEL_FACCH_F;
Harald Weltee779c362010-06-29 20:51:13 +020071 break;
72 case RSL_CHAN_Lm_ACCHs:
Vadim Yanitskiyfc02ff42021-05-26 18:19:03 +020073 case RSL_CHAN_OSMO_VAMOS_Lm_ACCHs:
Harald Welte67733042020-03-08 17:21:29 +010074 if (user_plane)
75 ret = GSMTAP_CHANNEL_VOICE_H;
76 else
77 ret = GSMTAP_CHANNEL_FACCH_H;
Harald Weltee779c362010-06-29 20:51:13 +020078 break;
79 case RSL_CHAN_SDCCH4_ACCH:
80 ret = GSMTAP_CHANNEL_SDCCH4;
81 break;
82 case RSL_CHAN_SDCCH8_ACCH:
83 ret = GSMTAP_CHANNEL_SDCCH8;
84 break;
85 case RSL_CHAN_BCCH:
86 ret = GSMTAP_CHANNEL_BCCH;
87 break;
88 case RSL_CHAN_RACH:
89 ret = GSMTAP_CHANNEL_RACH;
90 break;
91 case RSL_CHAN_PCH_AGCH:
92 /* it could also be AGCH... */
93 ret = GSMTAP_CHANNEL_PCH;
94 break;
Harald Welte3b7cd0b2017-07-27 14:12:15 +020095 case RSL_CHAN_OSMO_PDCH:
96 ret = GSMTAP_CHANNEL_PDCH;
97 break;
Harald Welteac7fabe2020-02-26 17:24:02 +010098 case RSL_CHAN_OSMO_CBCH4:
99 ret = GSMTAP_CHANNEL_CBCH51;
100 break;
101 case RSL_CHAN_OSMO_CBCH8:
102 ret = GSMTAP_CHANNEL_CBCH52;
103 break;
Harald Weltee779c362010-06-29 20:51:13 +0200104 }
105
106 if (link_id & 0x40)
107 ret |= GSMTAP_CHANNEL_ACCH;
108
109 return ret;
110}
111
Harald Welte67733042020-03-08 17:21:29 +0100112/*! convert RSL channel number to GSMTAP channel type
113 * \param[in] rsl_chantype RSL channel type
114 * \param[in] link_id RSL link identifier
115 * \returns GSMTAP channel type
116 */
117uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id)
118{
119 return chantype_rsl2gsmtap2(rsl_chantype, link_id, false);
120}
121
Harald Welte93713a52017-07-12 23:43:40 +0200122/*! convert GSMTAP channel type to RSL channel number + Link ID
123 * \param[in] gsmtap_chantype GSMTAP channel type
124 * \param[out] rsl_chantype RSL channel mumber
125 * \param[out] link_id RSL link identifier
126 */
127void chantype_gsmtap2rsl(uint8_t gsmtap_chantype, uint8_t *rsl_chantype,
128 uint8_t *link_id)
129{
130 switch (gsmtap_chantype & ~GSMTAP_CHANNEL_ACCH & 0xff) {
Harald Welte67733042020-03-08 17:21:29 +0100131 case GSMTAP_CHANNEL_FACCH_F:
132 case GSMTAP_CHANNEL_VOICE_F: // TCH/F
Harald Welte93713a52017-07-12 23:43:40 +0200133 *rsl_chantype = RSL_CHAN_Bm_ACCHs;
134 break;
Harald Welte67733042020-03-08 17:21:29 +0100135 case GSMTAP_CHANNEL_FACCH_H:
136 case GSMTAP_CHANNEL_VOICE_H: // TCH/H
Harald Welte93713a52017-07-12 23:43:40 +0200137 *rsl_chantype = RSL_CHAN_Lm_ACCHs;
138 break;
139 case GSMTAP_CHANNEL_SDCCH4: // SDCCH/4
140 *rsl_chantype = RSL_CHAN_SDCCH4_ACCH;
141 break;
142 case GSMTAP_CHANNEL_SDCCH8: // SDCCH/8
143 *rsl_chantype = RSL_CHAN_SDCCH8_ACCH;
144 break;
145 case GSMTAP_CHANNEL_BCCH: // BCCH
146 *rsl_chantype = RSL_CHAN_BCCH;
147 break;
148 case GSMTAP_CHANNEL_RACH: // RACH
149 *rsl_chantype = RSL_CHAN_RACH;
150 break;
151 case GSMTAP_CHANNEL_PCH: // PCH
152 case GSMTAP_CHANNEL_AGCH: // AGCH
153 *rsl_chantype = RSL_CHAN_PCH_AGCH;
154 break;
155 case GSMTAP_CHANNEL_PDCH:
Harald Welte3b7cd0b2017-07-27 14:12:15 +0200156 *rsl_chantype = RSL_CHAN_OSMO_PDCH;
Harald Welte93713a52017-07-12 23:43:40 +0200157 break;
158 }
159
160 *link_id = gsmtap_chantype & GSMTAP_CHANNEL_ACCH ? 0x40 : 0x00;
161}
162
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200163/*! create an arbitrary type GSMTAP message
Sylvain Munautabf66e72011-09-26 13:23:19 +0200164 * \param[in] type The GSMTAP_TYPE_xxx constant of the message to create
Harald Welte47379ca2011-08-17 16:35:24 +0200165 * \param[in] arfcn GSM ARFCN (Channel Number)
166 * \param[in] ts GSM time slot
167 * \param[in] chan_type Channel Type
168 * \param[in] ss Sub-slot
169 * \param[in] fn GSM Frame Number
170 * \param[in] signal_dbm Signal Strength (dBm)
171 * \param[in] snr Signal/Noise Ratio (SNR)
172 * \param[in] data Pointer to data buffer
173 * \param[in] len Length of \ref data
Pau Espin Pedrol9a5d90a2020-06-30 18:16:33 +0200174 * \return dynamically allocated message buffer containing data
Harald Welte47379ca2011-08-17 16:35:24 +0200175 *
176 * This function will allocate a new msgb and fill it with a GSMTAP
177 * header containing the information
178 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200179struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type,
Harald Weltee34a9402010-06-29 22:31:21 +0200180 uint8_t ss, uint32_t fn, int8_t signal_dbm,
Vadim Yanitskiy833e8fa2021-01-04 17:40:05 +0100181 int8_t snr, const uint8_t *data, unsigned int len)
Harald Weltee779c362010-06-29 20:51:13 +0200182{
183 struct msgb *msg;
184 struct gsmtap_hdr *gh;
185 uint8_t *dst;
186
Harald Weltee779c362010-06-29 20:51:13 +0200187 msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
188 if (!msg)
Harald Weltee34a9402010-06-29 22:31:21 +0200189 return NULL;
Harald Weltee779c362010-06-29 20:51:13 +0200190
191 gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
192
193 gh->version = GSMTAP_VERSION;
194 gh->hdr_len = sizeof(*gh)/4;
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200195 gh->type = type;
Harald Weltee779c362010-06-29 20:51:13 +0200196 gh->timeslot = ts;
197 gh->sub_slot = ss;
Harald Welte95871da2017-05-15 12:11:36 +0200198 gh->arfcn = osmo_htons(arfcn);
Harald Weltee779c362010-06-29 20:51:13 +0200199 gh->snr_db = snr;
200 gh->signal_dbm = signal_dbm;
Harald Welte95871da2017-05-15 12:11:36 +0200201 gh->frame_number = osmo_htonl(fn);
Harald Weltee779c362010-06-29 20:51:13 +0200202 gh->sub_type = chan_type;
203 gh->antenna_nr = 0;
204
205 dst = msgb_put(msg, len);
206 memcpy(dst, data, len);
207
Harald Weltee34a9402010-06-29 22:31:21 +0200208 return msg;
209}
210
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200211/*! create L1/L2 data and put it into GSMTAP
Sylvain Munautabf66e72011-09-26 13:23:19 +0200212 * \param[in] arfcn GSM ARFCN (Channel Number)
213 * \param[in] ts GSM time slot
214 * \param[in] chan_type Channel Type
215 * \param[in] ss Sub-slot
216 * \param[in] fn GSM Frame Number
217 * \param[in] signal_dbm Signal Strength (dBm)
218 * \param[in] snr Signal/Noise Ratio (SNR)
219 * \param[in] data Pointer to data buffer
220 * \param[in] len Length of \ref data
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200221 * \return message buffer or NULL in case of error
Sylvain Munautabf66e72011-09-26 13:23:19 +0200222 *
223 * This function will allocate a new msgb and fill it with a GSMTAP
224 * header containing the information
225 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200226struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
227 uint8_t ss, uint32_t fn, int8_t signal_dbm,
Vadim Yanitskiy833e8fa2021-01-04 17:40:05 +0100228 int8_t snr, const uint8_t *data, unsigned int len)
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200229{
230 return gsmtap_makemsg_ex(GSMTAP_TYPE_UM, arfcn, ts, chan_type,
231 ss, fn, signal_dbm, snr, data, len);
232}
233
Harald Weltee4764422011-05-22 12:25:57 +0200234#ifdef HAVE_SYS_SOCKET_H
235
236#include <sys/socket.h>
237#include <netinet/in.h>
238
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200239/*! Create a new (sending) GSMTAP source socket
Harald Welte47379ca2011-08-17 16:35:24 +0200240 * \param[in] host host name or IP address in string format
241 * \param[in] port UDP port number in host byte order
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200242 * \return file descriptor of the new socket
Harald Welte47379ca2011-08-17 16:35:24 +0200243 *
244 * Opens a GSMTAP source (sending) socket, conncet it to host/port and
245 * return resulting fd. If \a host is NULL, the destination address
246 * will be localhost. If \a port is 0, the default \ref
247 * GSMTAP_UDP_PORT will be used.
248 * */
Harald Welte33cb71a2011-05-21 18:54:32 +0200249int gsmtap_source_init_fd(const char *host, uint16_t port)
250{
251 if (port == 0)
252 port = GSMTAP_UDP_PORT;
253 if (host == NULL)
254 host = "localhost";
255
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200256 return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port,
257 OSMO_SOCK_F_CONNECT);
Harald Welte33cb71a2011-05-21 18:54:32 +0200258}
259
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200260/*! Add a local sink to an existing GSMTAP source and return fd
Harald Weltede6e4982012-12-06 21:25:27 +0100261 * \param[in] gsmtap_fd file descriptor of the gsmtap socket
262 * \returns file descriptor of locally bound receive socket
263 *
264 * In case the GSMTAP socket is connected to a local destination
265 * IP/port, this function creates a corresponding receiving socket
266 * bound to that destination IP + port.
267 *
268 * In case the gsmtap socket is not connected to a local IP/port, or
269 * creation of the receiving socket fails, a negative error code is
270 * returned.
271 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200272int gsmtap_source_add_sink_fd(int gsmtap_fd)
273{
274 struct sockaddr_storage ss;
275 socklen_t ss_len = sizeof(ss);
276 int rc;
277
278 rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len);
279 if (rc < 0)
280 return rc;
281
282 if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) {
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200283 rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM,
Philipp Maierb8a91622018-08-23 20:18:55 +0200284 IPPROTO_UDP,
285 OSMO_SOCK_F_BIND |
286 OSMO_SOCK_F_UDP_REUSEADDR);
Harald Welte33cb71a2011-05-21 18:54:32 +0200287 if (rc >= 0)
288 return rc;
289 }
290
291 return -ENODEV;
292}
293
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200294/*! Send a \ref msgb through a GSMTAP source
Harald Welte47379ca2011-08-17 16:35:24 +0200295 * \param[in] gti GSMTAP instance
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100296 * \param[in] msg message buffer
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200297 * \return 0 in case of success; negative in case of error
Neels Hofmeyrc9a4ce62018-02-16 01:23:29 +0100298 * NOTE: in case of nonzero return value, the *caller* must free the msg!
299 * (This enables the caller to attempt re-sending the message.)
300 * If 0 is returned, the msgb was freed by this function.
Harald Welte47379ca2011-08-17 16:35:24 +0200301 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200302int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg)
303{
Harald Welte13692a62011-05-22 20:06:11 +0200304 if (!gti)
305 return -ENODEV;
306
Harald Welte33cb71a2011-05-21 18:54:32 +0200307 if (gti->ofd_wq_mode)
308 return osmo_wqueue_enqueue(&gti->wq, msg);
309 else {
310 /* try immediate send and return error if any */
311 int rc;
312
313 rc = write(gsmtap_inst_fd(gti), msg->data, msg->len);
Neels Hofmeyr90539ac2018-02-16 01:24:03 +0100314 if (rc < 0) {
Harald Welte33cb71a2011-05-21 18:54:32 +0200315 return rc;
316 } else if (rc >= msg->len) {
317 msgb_free(msg);
318 return 0;
319 } else {
320 /* short write */
321 return -EIO;
322 }
323 }
324}
325
Harald Welte9e34f082021-11-25 15:35:50 +0100326/*! Send a \ref msgb through a GSMTAP source; free the message even if tx queue full.
327 * \param[in] gti GSMTAP instance
328 * \param[in] msg message buffer; always freed, caller must not reference it later.
329 * \return 0 in case of success; negative in case of error
330 */
331int gsmtap_sendmsg_free(struct gsmtap_inst *gti, struct msgb *msg)
332{
333 int rc;
334 rc = gsmtap_sendmsg(gti, msg);
335 if (rc < 0)
336 msgb_free(msg);
337 return rc;
338}
339
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200340/*! send an arbitrary type through GSMTAP.
Sylvain Munautabf66e72011-09-26 13:23:19 +0200341 * See \ref gsmtap_makemsg_ex for arguments
342 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200343int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts,
Harald Welte33cb71a2011-05-21 18:54:32 +0200344 uint8_t chan_type, uint8_t ss, uint32_t fn,
Vadim Yanitskiy833e8fa2021-01-04 17:40:05 +0100345 int8_t signal_dbm, int8_t snr, const uint8_t *data,
Harald Welte33cb71a2011-05-21 18:54:32 +0200346 unsigned int len)
Harald Weltee34a9402010-06-29 22:31:21 +0200347{
348 struct msgb *msg;
Neels Hofmeyra4952aa2018-02-16 01:26:00 +0100349 int rc;
Harald Weltee34a9402010-06-29 22:31:21 +0200350
Harald Welte13692a62011-05-22 20:06:11 +0200351 if (!gti)
352 return -ENODEV;
353
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200354 msg = gsmtap_makemsg_ex(type, arfcn, ts, chan_type, ss, fn, signal_dbm,
Harald Weltee34a9402010-06-29 22:31:21 +0200355 snr, data, len);
356 if (!msg)
357 return -ENOMEM;
358
Neels Hofmeyra4952aa2018-02-16 01:26:00 +0100359 rc = gsmtap_sendmsg(gti, msg);
360 if (rc)
361 msgb_free(msg);
362 return rc;
Harald Weltee779c362010-06-29 20:51:13 +0200363}
364
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200365/*! send a message from L1/L2 through GSMTAP.
Sylvain Munautabf66e72011-09-26 13:23:19 +0200366 * See \ref gsmtap_makemsg for arguments
367 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200368int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
369 uint8_t chan_type, uint8_t ss, uint32_t fn,
Vadim Yanitskiy833e8fa2021-01-04 17:40:05 +0100370 int8_t signal_dbm, int8_t snr, const uint8_t *data,
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200371 unsigned int len)
372{
373 return gsmtap_send_ex(gti, GSMTAP_TYPE_UM, arfcn, ts, chan_type, ss, fn,
374 signal_dbm, snr, data, len);
375}
376
Harald Weltee779c362010-06-29 20:51:13 +0200377/* Callback from select layer if we can write to the socket */
Harald Welte33cb71a2011-05-21 18:54:32 +0200378static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg)
Harald Weltee779c362010-06-29 20:51:13 +0200379{
Harald Weltee779c362010-06-29 20:51:13 +0200380 int rc;
381
Harald Welte33cb71a2011-05-21 18:54:32 +0200382 rc = write(ofd->fd, msg->data, msg->len);
Harald Weltee779c362010-06-29 20:51:13 +0200383 if (rc < 0) {
Harald Weltee779c362010-06-29 20:51:13 +0200384 return rc;
385 }
386 if (rc != msg->len) {
Harald Weltee779c362010-06-29 20:51:13 +0200387 return -EIO;
388 }
389
Harald Weltee779c362010-06-29 20:51:13 +0200390 return 0;
391}
392
Harald Welted58ba462011-04-27 10:57:49 +0200393/* Callback from select layer if we can read from the sink socket */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200394static int gsmtap_sink_fd_cb(struct osmo_fd *fd, unsigned int flags)
Harald Welted58ba462011-04-27 10:57:49 +0200395{
396 int rc;
397 uint8_t buf[4096];
398
Harald Welte16886992019-03-20 10:26:39 +0100399 if (!(flags & OSMO_FD_READ))
Harald Welted58ba462011-04-27 10:57:49 +0200400 return 0;
401
402 rc = read(fd->fd, buf, sizeof(buf));
403 if (rc < 0) {
Harald Welted58ba462011-04-27 10:57:49 +0200404 return rc;
405 }
406 /* simply discard any data arriving on the socket */
407
408 return 0;
409}
410
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200411/*! Add a local sink to an existing GSMTAP source and return fd
Vadim Yanitskiy2f65bb12019-03-25 15:57:09 +0700412 * \param[in] gti existing GSMTAP source
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200413 * \returns file descriptor of locally bound receive socket
414 *
415 * In case the GSMTAP socket is connected to a local destination
416 * IP/port, this function creates a corresponding receiving socket
417 * bound to that destination IP + port.
418 *
419 * In case the gsmtap socket is not connected to a local IP/port, or
420 * creation of the receiving socket fails, a negative error code is
421 * returned.
422 *
423 * The file descriptor of the receiving socket is automatically added
424 * to the libosmocore select() handling.
425 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200426int gsmtap_source_add_sink(struct gsmtap_inst *gti)
Harald Welted58ba462011-04-27 10:57:49 +0200427{
Harald Welte9d862c82016-11-26 00:10:07 +0100428 int fd, rc;
Harald Welted58ba462011-04-27 10:57:49 +0200429
Harald Welte33cb71a2011-05-21 18:54:32 +0200430 fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti));
431 if (fd < 0)
432 return fd;
Harald Welted58ba462011-04-27 10:57:49 +0200433
Harald Welte33cb71a2011-05-21 18:54:32 +0200434 if (gti->ofd_wq_mode) {
435 struct osmo_fd *sink_ofd;
436
437 sink_ofd = &gti->sink_ofd;
438 sink_ofd->fd = fd;
Harald Welte16886992019-03-20 10:26:39 +0100439 sink_ofd->when = OSMO_FD_READ;
Harald Welte33cb71a2011-05-21 18:54:32 +0200440 sink_ofd->cb = gsmtap_sink_fd_cb;
441
Harald Welte9d862c82016-11-26 00:10:07 +0100442 rc = osmo_fd_register(sink_ofd);
443 if (rc < 0) {
444 close(fd);
445 return rc;
446 }
Harald Welted58ba462011-04-27 10:57:49 +0200447 }
448
Harald Welte33cb71a2011-05-21 18:54:32 +0200449 return fd;
450}
Harald Welted58ba462011-04-27 10:57:49 +0200451
Harald Welte47379ca2011-08-17 16:35:24 +0200452
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200453/*! Open GSMTAP source socket, connect and register osmo_fd
Harald Welte47379ca2011-08-17 16:35:24 +0200454 * \param[in] host host name or IP address in string format
455 * \param[in] port UDP port number in host byte order
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100456 * \param[in] ofd_wq_mode Register \ref osmo_wqueue (1) or not (0)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200457 * \return callee-allocated \ref gsmtap_inst
Harald Welte47379ca2011-08-17 16:35:24 +0200458 *
459 * Open GSMTAP source (sending) socket, connect it to host/port,
460 * allocate 'struct gsmtap_inst' and optionally osmo_fd/osmo_wqueue
Vadim Yanitskiy2f65bb12019-03-25 15:57:09 +0700461 * registration.
462 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200463struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port,
464 int ofd_wq_mode)
465{
466 struct gsmtap_inst *gti;
Harald Welte9d862c82016-11-26 00:10:07 +0100467 int fd, rc;
Harald Welted58ba462011-04-27 10:57:49 +0200468
Harald Welte33cb71a2011-05-21 18:54:32 +0200469 fd = gsmtap_source_init_fd(host, port);
470 if (fd < 0)
471 return NULL;
472
473 gti = talloc_zero(NULL, struct gsmtap_inst);
474 gti->ofd_wq_mode = ofd_wq_mode;
475 gti->wq.bfd.fd = fd;
476 gti->sink_ofd.fd = -1;
477
478 if (ofd_wq_mode) {
479 osmo_wqueue_init(&gti->wq, 64);
480 gti->wq.write_cb = &gsmtap_wq_w_cb;
481
Harald Welte9d862c82016-11-26 00:10:07 +0100482 rc = osmo_fd_register(&gti->wq.bfd);
483 if (rc < 0) {
Vadim Yanitskiyb9baf022019-03-24 00:01:27 +0700484 talloc_free(gti);
Harald Welte9d862c82016-11-26 00:10:07 +0100485 close(fd);
486 return NULL;
487 }
Harald Welte33cb71a2011-05-21 18:54:32 +0200488 }
489
490 return gti;
Harald Welted58ba462011-04-27 10:57:49 +0200491}
492
Harald Weltee4764422011-05-22 12:25:57 +0200493#endif /* HAVE_SYS_SOCKET_H */
Harald Weltede6e4982012-12-06 21:25:27 +0100494
Harald Welteaa3ba462017-07-13 00:01:02 +0200495const struct value_string gsmtap_gsm_channel_names[] = {
496 { GSMTAP_CHANNEL_UNKNOWN, "UNKNOWN" },
497 { GSMTAP_CHANNEL_BCCH, "BCCH" },
498 { GSMTAP_CHANNEL_CCCH, "CCCH" },
499 { GSMTAP_CHANNEL_RACH, "RACH" },
500 { GSMTAP_CHANNEL_AGCH, "AGCH" },
501 { GSMTAP_CHANNEL_PCH, "PCH" },
502 { GSMTAP_CHANNEL_SDCCH, "SDCCH" },
503 { GSMTAP_CHANNEL_SDCCH4, "SDCCH/4" },
504 { GSMTAP_CHANNEL_SDCCH8, "SDCCH/8" },
Harald Welte67733042020-03-08 17:21:29 +0100505 { GSMTAP_CHANNEL_FACCH_F, "FACCH/F" },
506 { GSMTAP_CHANNEL_FACCH_H, "FACCH/H" },
Harald Welteaa3ba462017-07-13 00:01:02 +0200507 { GSMTAP_CHANNEL_PACCH, "PACCH" },
508 { GSMTAP_CHANNEL_CBCH52, "CBCH" },
509 { GSMTAP_CHANNEL_PDCH, "PDCH" } ,
510 { GSMTAP_CHANNEL_PTCCH, "PTTCH" },
511 { GSMTAP_CHANNEL_CBCH51, "CBCH" },
512 { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH, "LSACCH" },
513 { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH4, "SACCH/4" },
514 { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH8, "SACCH/8" },
Harald Welte67733042020-03-08 17:21:29 +0100515 { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_FACCH_F, "SACCH/F" },
516 { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_FACCH_H, "SACCH/H" },
517 { GSMTAP_CHANNEL_VOICE_F, "TCH/F" },
518 { GSMTAP_CHANNEL_VOICE_H, "TCH/H" },
Harald Welteaa3ba462017-07-13 00:01:02 +0200519 { 0, NULL }
520};
521
522/* for debugging */
523const struct value_string gsmtap_type_names[] = {
524 { GSMTAP_TYPE_UM, "GSM Um (MS<->BTS)" },
525 { GSMTAP_TYPE_ABIS, "GSM Abis (BTS<->BSC)" },
526 { GSMTAP_TYPE_UM_BURST, "GSM Um burst (MS<->BTS)" },
527 { GSMTAP_TYPE_SIM, "SIM Card" },
528 { GSMTAP_TYPE_TETRA_I1, "TETRA V+D" },
529 { GSMTAP_TYPE_TETRA_I1_BURST, "TETRA bursts" },
530 { GSMTAP_TYPE_WMX_BURST, "WiMAX burst" },
531 { GSMTAP_TYPE_GMR1_UM, "GMR-1 air interfeace (MES-MS<->GTS)"},
532 { GSMTAP_TYPE_UMTS_RLC_MAC, "UMTS RLC/MAC" },
533 { GSMTAP_TYPE_UMTS_RRC, "UMTS RRC" },
534 { GSMTAP_TYPE_LTE_RRC, "LTE RRC" },
535 { GSMTAP_TYPE_LTE_MAC, "LTE MAC" },
536 { GSMTAP_TYPE_LTE_MAC_FRAMED, "LTE MAC with context hdr" },
537 { GSMTAP_TYPE_OSMOCORE_LOG, "libosmocore logging" },
538 { GSMTAP_TYPE_QC_DIAG, "Qualcomm DIAG" },
539 { 0, NULL }
540};
541
Harald Weltede6e4982012-12-06 21:25:27 +0100542/*! @} */