Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file gsmtap_util.c |
| 2 | * GSMTAP support code in libosmocore. */ |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 3 | /* |
Harald Welte | 93713a5 | 2017-07-12 23:43:40 +0200 | [diff] [blame] | 4 | * (C) 2010-2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 10 | * 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 Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 28 | #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 Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 32 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 33 | #include <osmocom/core/select.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 34 | #include <osmocom/core/socket.h> |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 35 | #include <osmocom/core/byteswap.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 36 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 37 | #include <osmocom/gsm/rsl.h> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 38 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 39 | #include <sys/types.h> |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 40 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 41 | #include <stdio.h> |
| 42 | #include <unistd.h> |
| 43 | #include <stdint.h> |
| 44 | #include <string.h> |
| 45 | #include <errno.h> |
| 46 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 47 | /*! \addtogroup gsmtap |
| 48 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 49 | * GSMTAP utility routines. Encapsulates GSM messages over UDP. |
| 50 | * |
| 51 | * \file gsmtap_util.c */ |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 52 | |
| 53 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 54 | /*! convert RSL channel number to GSMTAP channel type |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 55 | * \param[in] rsl_chantype RSL channel type |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 56 | * \param[in] link_id RSL link identifier |
Harald Welte | 6773304 | 2020-03-08 17:21:29 +0100 | [diff] [blame] | 57 | * \param[in] user_plane Is this voice/csd user plane (1) or signaling (0) |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 58 | * \returns GSMTAP channel type |
| 59 | */ |
Harald Welte | 6773304 | 2020-03-08 17:21:29 +0100 | [diff] [blame] | 60 | uint8_t chantype_rsl2gsmtap2(uint8_t rsl_chantype, uint8_t link_id, bool user_plane) |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 61 | { |
| 62 | uint8_t ret = GSMTAP_CHANNEL_UNKNOWN; |
| 63 | |
| 64 | switch (rsl_chantype) { |
| 65 | case RSL_CHAN_Bm_ACCHs: |
Vadim Yanitskiy | fc02ff4 | 2021-05-26 18:19:03 +0200 | [diff] [blame] | 66 | case RSL_CHAN_OSMO_VAMOS_Bm_ACCHs: |
Harald Welte | 6773304 | 2020-03-08 17:21:29 +0100 | [diff] [blame] | 67 | if (user_plane) |
| 68 | ret = GSMTAP_CHANNEL_VOICE_F; |
| 69 | else |
| 70 | ret = GSMTAP_CHANNEL_FACCH_F; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 71 | break; |
| 72 | case RSL_CHAN_Lm_ACCHs: |
Vadim Yanitskiy | fc02ff4 | 2021-05-26 18:19:03 +0200 | [diff] [blame] | 73 | case RSL_CHAN_OSMO_VAMOS_Lm_ACCHs: |
Harald Welte | 6773304 | 2020-03-08 17:21:29 +0100 | [diff] [blame] | 74 | if (user_plane) |
| 75 | ret = GSMTAP_CHANNEL_VOICE_H; |
| 76 | else |
| 77 | ret = GSMTAP_CHANNEL_FACCH_H; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 78 | 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 Welte | 3b7cd0b | 2017-07-27 14:12:15 +0200 | [diff] [blame] | 95 | case RSL_CHAN_OSMO_PDCH: |
| 96 | ret = GSMTAP_CHANNEL_PDCH; |
| 97 | break; |
Harald Welte | ac7fabe | 2020-02-26 17:24:02 +0100 | [diff] [blame] | 98 | 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 Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (link_id & 0x40) |
| 107 | ret |= GSMTAP_CHANNEL_ACCH; |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
Harald Welte | 6773304 | 2020-03-08 17:21:29 +0100 | [diff] [blame] | 112 | /*! 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 | */ |
| 117 | uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id) |
| 118 | { |
| 119 | return chantype_rsl2gsmtap2(rsl_chantype, link_id, false); |
| 120 | } |
| 121 | |
Harald Welte | 93713a5 | 2017-07-12 23:43:40 +0200 | [diff] [blame] | 122 | /*! 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 | */ |
| 127 | void 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 Welte | 6773304 | 2020-03-08 17:21:29 +0100 | [diff] [blame] | 131 | case GSMTAP_CHANNEL_FACCH_F: |
| 132 | case GSMTAP_CHANNEL_VOICE_F: // TCH/F |
Harald Welte | 93713a5 | 2017-07-12 23:43:40 +0200 | [diff] [blame] | 133 | *rsl_chantype = RSL_CHAN_Bm_ACCHs; |
| 134 | break; |
Harald Welte | 6773304 | 2020-03-08 17:21:29 +0100 | [diff] [blame] | 135 | case GSMTAP_CHANNEL_FACCH_H: |
| 136 | case GSMTAP_CHANNEL_VOICE_H: // TCH/H |
Harald Welte | 93713a5 | 2017-07-12 23:43:40 +0200 | [diff] [blame] | 137 | *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 Welte | 3b7cd0b | 2017-07-27 14:12:15 +0200 | [diff] [blame] | 156 | *rsl_chantype = RSL_CHAN_OSMO_PDCH; |
Harald Welte | 93713a5 | 2017-07-12 23:43:40 +0200 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | |
| 160 | *link_id = gsmtap_chantype & GSMTAP_CHANNEL_ACCH ? 0x40 : 0x00; |
| 161 | } |
| 162 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 163 | /*! create an arbitrary type GSMTAP message |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 164 | * \param[in] type The GSMTAP_TYPE_xxx constant of the message to create |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 165 | * \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 Pedrol | 9a5d90a | 2020-06-30 18:16:33 +0200 | [diff] [blame] | 174 | * \return dynamically allocated message buffer containing data |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 175 | * |
| 176 | * This function will allocate a new msgb and fill it with a GSMTAP |
| 177 | * header containing the information |
| 178 | */ |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 179 | struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type, |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 180 | uint8_t ss, uint32_t fn, int8_t signal_dbm, |
Vadim Yanitskiy | 833e8fa | 2021-01-04 17:40:05 +0100 | [diff] [blame] | 181 | int8_t snr, const uint8_t *data, unsigned int len) |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 182 | { |
| 183 | struct msgb *msg; |
| 184 | struct gsmtap_hdr *gh; |
| 185 | uint8_t *dst; |
| 186 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 187 | msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx"); |
| 188 | if (!msg) |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 189 | return NULL; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 190 | |
| 191 | gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh)); |
| 192 | |
| 193 | gh->version = GSMTAP_VERSION; |
| 194 | gh->hdr_len = sizeof(*gh)/4; |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 195 | gh->type = type; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 196 | gh->timeslot = ts; |
| 197 | gh->sub_slot = ss; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 198 | gh->arfcn = osmo_htons(arfcn); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 199 | gh->snr_db = snr; |
| 200 | gh->signal_dbm = signal_dbm; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 201 | gh->frame_number = osmo_htonl(fn); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 202 | gh->sub_type = chan_type; |
| 203 | gh->antenna_nr = 0; |
| 204 | |
| 205 | dst = msgb_put(msg, len); |
| 206 | memcpy(dst, data, len); |
| 207 | |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 208 | return msg; |
| 209 | } |
| 210 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 211 | /*! create L1/L2 data and put it into GSMTAP |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 212 | * \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 Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 221 | * \return message buffer or NULL in case of error |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 222 | * |
| 223 | * This function will allocate a new msgb and fill it with a GSMTAP |
| 224 | * header containing the information |
| 225 | */ |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 226 | struct 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 Yanitskiy | 833e8fa | 2021-01-04 17:40:05 +0100 | [diff] [blame] | 228 | int8_t snr, const uint8_t *data, unsigned int len) |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 229 | { |
| 230 | return gsmtap_makemsg_ex(GSMTAP_TYPE_UM, arfcn, ts, chan_type, |
| 231 | ss, fn, signal_dbm, snr, data, len); |
| 232 | } |
| 233 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 234 | #ifdef HAVE_SYS_SOCKET_H |
| 235 | |
| 236 | #include <sys/socket.h> |
| 237 | #include <netinet/in.h> |
| 238 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 239 | /*! Create a new (sending) GSMTAP source socket |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 240 | * \param[in] host host name or IP address in string format |
| 241 | * \param[in] port UDP port number in host byte order |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 242 | * \return file descriptor of the new socket |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 243 | * |
| 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 Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 249 | int 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 Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 256 | return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port, |
| 257 | OSMO_SOCK_F_CONNECT); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 260 | /*! Add a local sink to an existing GSMTAP source and return fd |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 261 | * \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 Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 272 | int 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 Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 283 | rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM, |
Philipp Maier | b8a9162 | 2018-08-23 20:18:55 +0200 | [diff] [blame] | 284 | IPPROTO_UDP, |
| 285 | OSMO_SOCK_F_BIND | |
| 286 | OSMO_SOCK_F_UDP_REUSEADDR); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 287 | if (rc >= 0) |
| 288 | return rc; |
| 289 | } |
| 290 | |
| 291 | return -ENODEV; |
| 292 | } |
| 293 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 294 | /*! Send a \ref msgb through a GSMTAP source |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 295 | * \param[in] gti GSMTAP instance |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 296 | * \param[in] msg message buffer |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 297 | * \return 0 in case of success; negative in case of error |
Neels Hofmeyr | c9a4ce6 | 2018-02-16 01:23:29 +0100 | [diff] [blame] | 298 | * 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 Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 301 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 302 | int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg) |
| 303 | { |
Harald Welte | 13692a6 | 2011-05-22 20:06:11 +0200 | [diff] [blame] | 304 | if (!gti) |
| 305 | return -ENODEV; |
| 306 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 307 | if (gti->ofd_wq_mode) |
| 308 | return osmo_wqueue_enqueue(>i->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 Hofmeyr | 90539ac | 2018-02-16 01:24:03 +0100 | [diff] [blame] | 314 | if (rc < 0) { |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 315 | 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 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 326 | /*! send an arbitrary type through GSMTAP. |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 327 | * See \ref gsmtap_makemsg_ex for arguments |
| 328 | */ |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 329 | int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts, |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 330 | uint8_t chan_type, uint8_t ss, uint32_t fn, |
Vadim Yanitskiy | 833e8fa | 2021-01-04 17:40:05 +0100 | [diff] [blame] | 331 | int8_t signal_dbm, int8_t snr, const uint8_t *data, |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 332 | unsigned int len) |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 333 | { |
| 334 | struct msgb *msg; |
Neels Hofmeyr | a4952aa | 2018-02-16 01:26:00 +0100 | [diff] [blame] | 335 | int rc; |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 336 | |
Harald Welte | 13692a6 | 2011-05-22 20:06:11 +0200 | [diff] [blame] | 337 | if (!gti) |
| 338 | return -ENODEV; |
| 339 | |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 340 | msg = gsmtap_makemsg_ex(type, arfcn, ts, chan_type, ss, fn, signal_dbm, |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 341 | snr, data, len); |
| 342 | if (!msg) |
| 343 | return -ENOMEM; |
| 344 | |
Neels Hofmeyr | a4952aa | 2018-02-16 01:26:00 +0100 | [diff] [blame] | 345 | rc = gsmtap_sendmsg(gti, msg); |
| 346 | if (rc) |
| 347 | msgb_free(msg); |
| 348 | return rc; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 351 | /*! send a message from L1/L2 through GSMTAP. |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 352 | * See \ref gsmtap_makemsg for arguments |
| 353 | */ |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 354 | int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts, |
| 355 | uint8_t chan_type, uint8_t ss, uint32_t fn, |
Vadim Yanitskiy | 833e8fa | 2021-01-04 17:40:05 +0100 | [diff] [blame] | 356 | int8_t signal_dbm, int8_t snr, const uint8_t *data, |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 357 | unsigned int len) |
| 358 | { |
| 359 | return gsmtap_send_ex(gti, GSMTAP_TYPE_UM, arfcn, ts, chan_type, ss, fn, |
| 360 | signal_dbm, snr, data, len); |
| 361 | } |
| 362 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 363 | /* Callback from select layer if we can write to the socket */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 364 | static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg) |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 365 | { |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 366 | int rc; |
| 367 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 368 | rc = write(ofd->fd, msg->data, msg->len); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 369 | if (rc < 0) { |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 370 | return rc; |
| 371 | } |
| 372 | if (rc != msg->len) { |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 373 | return -EIO; |
| 374 | } |
| 375 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 379 | /* Callback from select layer if we can read from the sink socket */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 380 | static int gsmtap_sink_fd_cb(struct osmo_fd *fd, unsigned int flags) |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 381 | { |
| 382 | int rc; |
| 383 | uint8_t buf[4096]; |
| 384 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 385 | if (!(flags & OSMO_FD_READ)) |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 386 | return 0; |
| 387 | |
| 388 | rc = read(fd->fd, buf, sizeof(buf)); |
| 389 | if (rc < 0) { |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 390 | return rc; |
| 391 | } |
| 392 | /* simply discard any data arriving on the socket */ |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 397 | /*! Add a local sink to an existing GSMTAP source and return fd |
Vadim Yanitskiy | 2f65bb1 | 2019-03-25 15:57:09 +0700 | [diff] [blame] | 398 | * \param[in] gti existing GSMTAP source |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 399 | * \returns file descriptor of locally bound receive socket |
| 400 | * |
| 401 | * In case the GSMTAP socket is connected to a local destination |
| 402 | * IP/port, this function creates a corresponding receiving socket |
| 403 | * bound to that destination IP + port. |
| 404 | * |
| 405 | * In case the gsmtap socket is not connected to a local IP/port, or |
| 406 | * creation of the receiving socket fails, a negative error code is |
| 407 | * returned. |
| 408 | * |
| 409 | * The file descriptor of the receiving socket is automatically added |
| 410 | * to the libosmocore select() handling. |
| 411 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 412 | int gsmtap_source_add_sink(struct gsmtap_inst *gti) |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 413 | { |
Harald Welte | 9d862c8 | 2016-11-26 00:10:07 +0100 | [diff] [blame] | 414 | int fd, rc; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 415 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 416 | fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti)); |
| 417 | if (fd < 0) |
| 418 | return fd; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 419 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 420 | if (gti->ofd_wq_mode) { |
| 421 | struct osmo_fd *sink_ofd; |
| 422 | |
| 423 | sink_ofd = >i->sink_ofd; |
| 424 | sink_ofd->fd = fd; |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 425 | sink_ofd->when = OSMO_FD_READ; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 426 | sink_ofd->cb = gsmtap_sink_fd_cb; |
| 427 | |
Harald Welte | 9d862c8 | 2016-11-26 00:10:07 +0100 | [diff] [blame] | 428 | rc = osmo_fd_register(sink_ofd); |
| 429 | if (rc < 0) { |
| 430 | close(fd); |
| 431 | return rc; |
| 432 | } |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 435 | return fd; |
| 436 | } |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 437 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 438 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 439 | /*! Open GSMTAP source socket, connect and register osmo_fd |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 440 | * \param[in] host host name or IP address in string format |
| 441 | * \param[in] port UDP port number in host byte order |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 442 | * \param[in] ofd_wq_mode Register \ref osmo_wqueue (1) or not (0) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 443 | * \return callee-allocated \ref gsmtap_inst |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 444 | * |
| 445 | * Open GSMTAP source (sending) socket, connect it to host/port, |
| 446 | * allocate 'struct gsmtap_inst' and optionally osmo_fd/osmo_wqueue |
Vadim Yanitskiy | 2f65bb1 | 2019-03-25 15:57:09 +0700 | [diff] [blame] | 447 | * registration. |
| 448 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 449 | struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port, |
| 450 | int ofd_wq_mode) |
| 451 | { |
| 452 | struct gsmtap_inst *gti; |
Harald Welte | 9d862c8 | 2016-11-26 00:10:07 +0100 | [diff] [blame] | 453 | int fd, rc; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 454 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 455 | fd = gsmtap_source_init_fd(host, port); |
| 456 | if (fd < 0) |
| 457 | return NULL; |
| 458 | |
| 459 | gti = talloc_zero(NULL, struct gsmtap_inst); |
| 460 | gti->ofd_wq_mode = ofd_wq_mode; |
| 461 | gti->wq.bfd.fd = fd; |
| 462 | gti->sink_ofd.fd = -1; |
| 463 | |
| 464 | if (ofd_wq_mode) { |
| 465 | osmo_wqueue_init(>i->wq, 64); |
| 466 | gti->wq.write_cb = &gsmtap_wq_w_cb; |
| 467 | |
Harald Welte | 9d862c8 | 2016-11-26 00:10:07 +0100 | [diff] [blame] | 468 | rc = osmo_fd_register(>i->wq.bfd); |
| 469 | if (rc < 0) { |
Vadim Yanitskiy | b9baf02 | 2019-03-24 00:01:27 +0700 | [diff] [blame] | 470 | talloc_free(gti); |
Harald Welte | 9d862c8 | 2016-11-26 00:10:07 +0100 | [diff] [blame] | 471 | close(fd); |
| 472 | return NULL; |
| 473 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | return gti; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 479 | #endif /* HAVE_SYS_SOCKET_H */ |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 480 | |
Harald Welte | aa3ba46 | 2017-07-13 00:01:02 +0200 | [diff] [blame] | 481 | const struct value_string gsmtap_gsm_channel_names[] = { |
| 482 | { GSMTAP_CHANNEL_UNKNOWN, "UNKNOWN" }, |
| 483 | { GSMTAP_CHANNEL_BCCH, "BCCH" }, |
| 484 | { GSMTAP_CHANNEL_CCCH, "CCCH" }, |
| 485 | { GSMTAP_CHANNEL_RACH, "RACH" }, |
| 486 | { GSMTAP_CHANNEL_AGCH, "AGCH" }, |
| 487 | { GSMTAP_CHANNEL_PCH, "PCH" }, |
| 488 | { GSMTAP_CHANNEL_SDCCH, "SDCCH" }, |
| 489 | { GSMTAP_CHANNEL_SDCCH4, "SDCCH/4" }, |
| 490 | { GSMTAP_CHANNEL_SDCCH8, "SDCCH/8" }, |
Harald Welte | 6773304 | 2020-03-08 17:21:29 +0100 | [diff] [blame] | 491 | { GSMTAP_CHANNEL_FACCH_F, "FACCH/F" }, |
| 492 | { GSMTAP_CHANNEL_FACCH_H, "FACCH/H" }, |
Harald Welte | aa3ba46 | 2017-07-13 00:01:02 +0200 | [diff] [blame] | 493 | { GSMTAP_CHANNEL_PACCH, "PACCH" }, |
| 494 | { GSMTAP_CHANNEL_CBCH52, "CBCH" }, |
| 495 | { GSMTAP_CHANNEL_PDCH, "PDCH" } , |
| 496 | { GSMTAP_CHANNEL_PTCCH, "PTTCH" }, |
| 497 | { GSMTAP_CHANNEL_CBCH51, "CBCH" }, |
| 498 | { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH, "LSACCH" }, |
| 499 | { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH4, "SACCH/4" }, |
| 500 | { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH8, "SACCH/8" }, |
Harald Welte | 6773304 | 2020-03-08 17:21:29 +0100 | [diff] [blame] | 501 | { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_FACCH_F, "SACCH/F" }, |
| 502 | { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_FACCH_H, "SACCH/H" }, |
| 503 | { GSMTAP_CHANNEL_VOICE_F, "TCH/F" }, |
| 504 | { GSMTAP_CHANNEL_VOICE_H, "TCH/H" }, |
Harald Welte | aa3ba46 | 2017-07-13 00:01:02 +0200 | [diff] [blame] | 505 | { 0, NULL } |
| 506 | }; |
| 507 | |
| 508 | /* for debugging */ |
| 509 | const struct value_string gsmtap_type_names[] = { |
| 510 | { GSMTAP_TYPE_UM, "GSM Um (MS<->BTS)" }, |
| 511 | { GSMTAP_TYPE_ABIS, "GSM Abis (BTS<->BSC)" }, |
| 512 | { GSMTAP_TYPE_UM_BURST, "GSM Um burst (MS<->BTS)" }, |
| 513 | { GSMTAP_TYPE_SIM, "SIM Card" }, |
| 514 | { GSMTAP_TYPE_TETRA_I1, "TETRA V+D" }, |
| 515 | { GSMTAP_TYPE_TETRA_I1_BURST, "TETRA bursts" }, |
| 516 | { GSMTAP_TYPE_WMX_BURST, "WiMAX burst" }, |
| 517 | { GSMTAP_TYPE_GMR1_UM, "GMR-1 air interfeace (MES-MS<->GTS)"}, |
| 518 | { GSMTAP_TYPE_UMTS_RLC_MAC, "UMTS RLC/MAC" }, |
| 519 | { GSMTAP_TYPE_UMTS_RRC, "UMTS RRC" }, |
| 520 | { GSMTAP_TYPE_LTE_RRC, "LTE RRC" }, |
| 521 | { GSMTAP_TYPE_LTE_MAC, "LTE MAC" }, |
| 522 | { GSMTAP_TYPE_LTE_MAC_FRAMED, "LTE MAC with context hdr" }, |
| 523 | { GSMTAP_TYPE_OSMOCORE_LOG, "libosmocore logging" }, |
| 524 | { GSMTAP_TYPE_QC_DIAG, "Qualcomm DIAG" }, |
| 525 | { 0, NULL } |
| 526 | }; |
| 527 | |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 528 | /*! @} */ |