blob: 0e4d61e555917c51b282221da91452018f7c0e17 [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>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010032#include <osmocom/gsm/protocol/gsm_04_08.h>
33#include <osmocom/gsm/rsl.h>
Harald Weltee779c362010-06-29 20:51:13 +020034
Harald Welte33cb71a2011-05-21 18:54:32 +020035#include <sys/types.h>
Harald Weltee4764422011-05-22 12:25:57 +020036
37#include <arpa/inet.h>
Harald Weltee779c362010-06-29 20:51:13 +020038
39#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 * @{
47 */
48/*! \file gsmtap_util.c */
49
50
51/*! \brief convert RSL channel number to GSMTAP channel type
52 * \param[in] rsl_cantype RSL channel type
53 * \param[in] link_id RSL link identifier
54 * \returns GSMTAP channel type
55 */
Harald Weltee779c362010-06-29 20:51:13 +020056uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id)
57{
58 uint8_t ret = GSMTAP_CHANNEL_UNKNOWN;
59
60 switch (rsl_chantype) {
61 case RSL_CHAN_Bm_ACCHs:
62 ret = GSMTAP_CHANNEL_TCH_F;
63 break;
64 case RSL_CHAN_Lm_ACCHs:
65 ret = GSMTAP_CHANNEL_TCH_H;
66 break;
67 case RSL_CHAN_SDCCH4_ACCH:
68 ret = GSMTAP_CHANNEL_SDCCH4;
69 break;
70 case RSL_CHAN_SDCCH8_ACCH:
71 ret = GSMTAP_CHANNEL_SDCCH8;
72 break;
73 case RSL_CHAN_BCCH:
74 ret = GSMTAP_CHANNEL_BCCH;
75 break;
76 case RSL_CHAN_RACH:
77 ret = GSMTAP_CHANNEL_RACH;
78 break;
79 case RSL_CHAN_PCH_AGCH:
80 /* it could also be AGCH... */
81 ret = GSMTAP_CHANNEL_PCH;
82 break;
83 }
84
85 if (link_id & 0x40)
86 ret |= GSMTAP_CHANNEL_ACCH;
87
88 return ret;
89}
90
Harald Welte47379ca2011-08-17 16:35:24 +020091/*! \brief create L1/L2 data and put it into GSMTAP
92 * \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
101 *
102 * This function will allocate a new msgb and fill it with a GSMTAP
103 * header containing the information
104 */
Harald Weltee34a9402010-06-29 22:31:21 +0200105struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
106 uint8_t ss, uint32_t fn, int8_t signal_dbm,
107 uint8_t snr, const uint8_t *data, unsigned int len)
Harald Weltee779c362010-06-29 20:51:13 +0200108{
109 struct msgb *msg;
110 struct gsmtap_hdr *gh;
111 uint8_t *dst;
112
Harald Weltee779c362010-06-29 20:51:13 +0200113 msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
114 if (!msg)
Harald Weltee34a9402010-06-29 22:31:21 +0200115 return NULL;
Harald Weltee779c362010-06-29 20:51:13 +0200116
117 gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
118
119 gh->version = GSMTAP_VERSION;
120 gh->hdr_len = sizeof(*gh)/4;
121 gh->type = GSMTAP_TYPE_UM;
122 gh->timeslot = ts;
123 gh->sub_slot = ss;
124 gh->arfcn = htons(arfcn);
125 gh->snr_db = snr;
126 gh->signal_dbm = signal_dbm;
127 gh->frame_number = htonl(fn);
128 gh->sub_type = chan_type;
129 gh->antenna_nr = 0;
130
131 dst = msgb_put(msg, len);
132 memcpy(dst, data, len);
133
Harald Weltee34a9402010-06-29 22:31:21 +0200134 return msg;
135}
136
Harald Weltee4764422011-05-22 12:25:57 +0200137#ifdef HAVE_SYS_SOCKET_H
138
139#include <sys/socket.h>
140#include <netinet/in.h>
141
Harald Welte47379ca2011-08-17 16:35:24 +0200142/*! \brief Create a new (sending) GSMTAP source socket
143 * \param[in] host host name or IP address in string format
144 * \param[in] port UDP port number in host byte order
145 *
146 * Opens a GSMTAP source (sending) socket, conncet it to host/port and
147 * return resulting fd. If \a host is NULL, the destination address
148 * will be localhost. If \a port is 0, the default \ref
149 * GSMTAP_UDP_PORT will be used.
150 * */
Harald Welte33cb71a2011-05-21 18:54:32 +0200151int gsmtap_source_init_fd(const char *host, uint16_t port)
152{
153 if (port == 0)
154 port = GSMTAP_UDP_PORT;
155 if (host == NULL)
156 host = "localhost";
157
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200158 return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port,
159 OSMO_SOCK_F_CONNECT);
Harald Welte33cb71a2011-05-21 18:54:32 +0200160}
161
Harald Welte47379ca2011-08-17 16:35:24 +0200162/*! \brief Add a local sink to an existing GSMTAP source and return fd */
Harald Welte33cb71a2011-05-21 18:54:32 +0200163int gsmtap_source_add_sink_fd(int gsmtap_fd)
164{
165 struct sockaddr_storage ss;
166 socklen_t ss_len = sizeof(ss);
167 int rc;
168
169 rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len);
170 if (rc < 0)
171 return rc;
172
173 if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) {
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200174 rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM,
175 IPPROTO_UDP, OSMO_SOCK_F_BIND);
Harald Welte33cb71a2011-05-21 18:54:32 +0200176 if (rc >= 0)
177 return rc;
178 }
179
180 return -ENODEV;
181}
182
Harald Welte47379ca2011-08-17 16:35:24 +0200183/*! \brief Send a \ref msgb through a GSMTAP source
184 * \param[in] gti GSMTAP instance
185 * \param[in] msgb message buffer
186 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200187int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg)
188{
Harald Welte13692a62011-05-22 20:06:11 +0200189 if (!gti)
190 return -ENODEV;
191
Harald Welte33cb71a2011-05-21 18:54:32 +0200192 if (gti->ofd_wq_mode)
193 return osmo_wqueue_enqueue(&gti->wq, msg);
194 else {
195 /* try immediate send and return error if any */
196 int rc;
197
198 rc = write(gsmtap_inst_fd(gti), msg->data, msg->len);
199 if (rc <= 0) {
200 return rc;
201 } else if (rc >= msg->len) {
202 msgb_free(msg);
203 return 0;
204 } else {
205 /* short write */
206 return -EIO;
207 }
208 }
209}
210
Harald Welte47379ca2011-08-17 16:35:24 +0200211/*! \brief receive a message from L1/L2 and put it in GSMTAP */
Harald Welte33cb71a2011-05-21 18:54:32 +0200212int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
213 uint8_t chan_type, uint8_t ss, uint32_t fn,
214 int8_t signal_dbm, uint8_t snr, const uint8_t *data,
215 unsigned int len)
Harald Weltee34a9402010-06-29 22:31:21 +0200216{
217 struct msgb *msg;
218
Harald Welte13692a62011-05-22 20:06:11 +0200219 if (!gti)
220 return -ENODEV;
221
Harald Weltee34a9402010-06-29 22:31:21 +0200222 msg = gsmtap_makemsg(arfcn, ts, chan_type, ss, fn, signal_dbm,
223 snr, data, len);
224 if (!msg)
225 return -ENOMEM;
226
Harald Welte33cb71a2011-05-21 18:54:32 +0200227 return gsmtap_sendmsg(gti, msg);
Harald Weltee779c362010-06-29 20:51:13 +0200228}
229
230/* Callback from select layer if we can write to the socket */
Harald Welte33cb71a2011-05-21 18:54:32 +0200231static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg)
Harald Weltee779c362010-06-29 20:51:13 +0200232{
Harald Weltee779c362010-06-29 20:51:13 +0200233 int rc;
234
Harald Welte33cb71a2011-05-21 18:54:32 +0200235 rc = write(ofd->fd, msg->data, msg->len);
Harald Weltee779c362010-06-29 20:51:13 +0200236 if (rc < 0) {
237 perror("writing msgb to gsmtap fd");
Harald Weltee779c362010-06-29 20:51:13 +0200238 return rc;
239 }
240 if (rc != msg->len) {
241 perror("short write to gsmtap fd");
Harald Weltee779c362010-06-29 20:51:13 +0200242 return -EIO;
243 }
244
Harald Weltee779c362010-06-29 20:51:13 +0200245 return 0;
246}
247
Harald Welted58ba462011-04-27 10:57:49 +0200248/* Callback from select layer if we can read from the sink socket */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200249static int gsmtap_sink_fd_cb(struct osmo_fd *fd, unsigned int flags)
Harald Welted58ba462011-04-27 10:57:49 +0200250{
251 int rc;
252 uint8_t buf[4096];
253
254 if (!(flags & BSC_FD_READ))
255 return 0;
256
257 rc = read(fd->fd, buf, sizeof(buf));
258 if (rc < 0) {
259 perror("reading from gsmtap sink fd");
260 return rc;
261 }
262 /* simply discard any data arriving on the socket */
263
264 return 0;
265}
266
Harald Welte47379ca2011-08-17 16:35:24 +0200267/*! \brief Add a local sink to an existing GSMTAP source instance */
Harald Welte33cb71a2011-05-21 18:54:32 +0200268int gsmtap_source_add_sink(struct gsmtap_inst *gti)
Harald Welted58ba462011-04-27 10:57:49 +0200269{
Harald Welte33cb71a2011-05-21 18:54:32 +0200270 int fd;
Harald Welted58ba462011-04-27 10:57:49 +0200271
Harald Welte33cb71a2011-05-21 18:54:32 +0200272 fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti));
273 if (fd < 0)
274 return fd;
Harald Welted58ba462011-04-27 10:57:49 +0200275
Harald Welte33cb71a2011-05-21 18:54:32 +0200276 if (gti->ofd_wq_mode) {
277 struct osmo_fd *sink_ofd;
278
279 sink_ofd = &gti->sink_ofd;
280 sink_ofd->fd = fd;
281 sink_ofd->when = BSC_FD_READ;
282 sink_ofd->cb = gsmtap_sink_fd_cb;
283
284 osmo_fd_register(sink_ofd);
Harald Welted58ba462011-04-27 10:57:49 +0200285 }
286
Harald Welte33cb71a2011-05-21 18:54:32 +0200287 return fd;
288}
Harald Welted58ba462011-04-27 10:57:49 +0200289
Harald Welte47379ca2011-08-17 16:35:24 +0200290
291/*! \brief Open GSMTAP source socket, connect and register osmo_fd
292 * \param[in] host host name or IP address in string format
293 * \param[in] port UDP port number in host byte order
294 * \param[in] osmo_wq_mode Register \ref osmo_wqueue (1) or not (0)
295 *
296 * Open GSMTAP source (sending) socket, connect it to host/port,
297 * allocate 'struct gsmtap_inst' and optionally osmo_fd/osmo_wqueue
298 * registration. This means it is like \ref gsmtap_init2 but integrated
299 * with libosmocore \ref select */
Harald Welte33cb71a2011-05-21 18:54:32 +0200300struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port,
301 int ofd_wq_mode)
302{
303 struct gsmtap_inst *gti;
304 int fd;
Harald Welted58ba462011-04-27 10:57:49 +0200305
Harald Welte33cb71a2011-05-21 18:54:32 +0200306 fd = gsmtap_source_init_fd(host, port);
307 if (fd < 0)
308 return NULL;
309
310 gti = talloc_zero(NULL, struct gsmtap_inst);
311 gti->ofd_wq_mode = ofd_wq_mode;
312 gti->wq.bfd.fd = fd;
313 gti->sink_ofd.fd = -1;
314
315 if (ofd_wq_mode) {
316 osmo_wqueue_init(&gti->wq, 64);
317 gti->wq.write_cb = &gsmtap_wq_w_cb;
318
319 osmo_fd_register(&gti->wq.bfd);
320 }
321
322 return gti;
Harald Welted58ba462011-04-27 10:57:49 +0200323}
324
Harald Weltee4764422011-05-22 12:25:57 +0200325#endif /* HAVE_SYS_SOCKET_H */