1 /* GSMTAP output for Osmocom Layer2 (will only work on the host PC) */
3 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
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.
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.
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.
23 #include "../config.h"
25 #ifdef HAVE_SYS_SELECT_H
27 #include <osmocom/core/gsmtap_util.h>
28 #include <osmocom/core/logging.h>
29 #include <osmocom/core/gsmtap.h>
30 #include <osmocom/core/msgb.h>
31 #include <osmocom/core/select.h>
32 #include <osmocom/gsm/protocol/gsm_04_08.h>
33 #include <osmocom/gsm/rsl.h>
35 #include <arpa/inet.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
45 static struct bsc_fd gsmtap_bfd = { .fd = -1 };
46 static struct bsc_fd gsmtap_sink_bfd = { .fd = -1 };
47 static LLIST_HEAD(gsmtap_txqueue);
49 uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id)
51 uint8_t ret = GSMTAP_CHANNEL_UNKNOWN;
53 switch (rsl_chantype) {
54 case RSL_CHAN_Bm_ACCHs:
55 ret = GSMTAP_CHANNEL_TCH_F;
57 case RSL_CHAN_Lm_ACCHs:
58 ret = GSMTAP_CHANNEL_TCH_H;
60 case RSL_CHAN_SDCCH4_ACCH:
61 ret = GSMTAP_CHANNEL_SDCCH4;
63 case RSL_CHAN_SDCCH8_ACCH:
64 ret = GSMTAP_CHANNEL_SDCCH8;
67 ret = GSMTAP_CHANNEL_BCCH;
70 ret = GSMTAP_CHANNEL_RACH;
72 case RSL_CHAN_PCH_AGCH:
73 /* it could also be AGCH... */
74 ret = GSMTAP_CHANNEL_PCH;
79 ret |= GSMTAP_CHANNEL_ACCH;
84 /* receive a message from L1/L2 and put it in GSMTAP */
85 struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
86 uint8_t ss, uint32_t fn, int8_t signal_dbm,
87 uint8_t snr, const uint8_t *data, unsigned int len)
90 struct gsmtap_hdr *gh;
93 msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
97 gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
99 gh->version = GSMTAP_VERSION;
100 gh->hdr_len = sizeof(*gh)/4;
101 gh->type = GSMTAP_TYPE_UM;
104 gh->arfcn = htons(arfcn);
106 gh->signal_dbm = signal_dbm;
107 gh->frame_number = htonl(fn);
108 gh->sub_type = chan_type;
111 dst = msgb_put(msg, len);
112 memcpy(dst, data, len);
117 /* receive a message from L1/L2 and put it in GSMTAP */
118 int gsmtap_sendmsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, uint8_t ss,
119 uint32_t fn, int8_t signal_dbm, uint8_t snr,
120 const uint8_t *data, unsigned int len)
124 /* gsmtap was never initialized, so don't try to send anything */
125 if (gsmtap_bfd.fd == -1)
128 msg = gsmtap_makemsg(arfcn, ts, chan_type, ss, fn, signal_dbm,
133 msgb_enqueue(&gsmtap_txqueue, msg);
134 gsmtap_bfd.when |= BSC_FD_WRITE;
139 /* Callback from select layer if we can write to the socket */
140 static int gsmtap_fd_cb(struct bsc_fd *fd, unsigned int flags)
145 if (!(flags & BSC_FD_WRITE))
148 msg = msgb_dequeue(&gsmtap_txqueue);
150 /* no more messages in the queue, disable READ cb */
154 rc = write(gsmtap_bfd.fd, msg->data, msg->len);
156 perror("writing msgb to gsmtap fd");
160 if (rc != msg->len) {
161 perror("short write to gsmtap fd");
170 int gsmtap_init(uint32_t dst_ip)
173 struct sockaddr_in sin;
175 sin.sin_family = AF_INET;
176 sin.sin_port = htons(GSMTAP_UDP_PORT);
177 sin.sin_addr.s_addr = htonl(dst_ip);
180 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
182 perror("creating UDP socket");
186 rc = connect(rc, (struct sockaddr *)&sin, sizeof(sin));
188 perror("connecting UDP socket");
189 close(gsmtap_bfd.fd);
194 gsmtap_bfd.when = BSC_FD_WRITE;
195 gsmtap_bfd.cb = gsmtap_fd_cb;
196 gsmtap_bfd.data = NULL;
198 return bsc_register_fd(&gsmtap_bfd);
201 /* Callback from select layer if we can read from the sink socket */
202 static int gsmtap_sink_fd_cb(struct bsc_fd *fd, unsigned int flags)
207 if (!(flags & BSC_FD_READ))
210 rc = read(fd->fd, buf, sizeof(buf));
212 perror("reading from gsmtap sink fd");
215 /* simply discard any data arriving on the socket */
220 /* Create a local 'gsmtap sink' avoiding the UDP packets being rejected
221 * with ICMP reject messages */
222 int gsmtap_sink_init(uint32_t bind_ip)
225 struct sockaddr_in sin;
227 sin.sin_family = AF_INET;
228 sin.sin_port = htons(GSMTAP_UDP_PORT);
229 sin.sin_addr.s_addr = htonl(bind_ip);
231 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
233 perror("creating UDP socket");
236 gsmtap_sink_bfd.fd = rc;
237 rc = bind(rc, (struct sockaddr *)&sin, sizeof(sin));
239 perror("binding UDP socket");
240 close(gsmtap_sink_bfd.fd);
241 gsmtap_sink_bfd.fd = -1;
245 gsmtap_sink_bfd.when = BSC_FD_READ;
246 gsmtap_sink_bfd.cb = gsmtap_sink_fd_cb;
247 gsmtap_sink_bfd.data = NULL;
249 return bsc_register_fd(&gsmtap_sink_bfd);
253 #endif /* HAVE_SYS_SELECT_H */