Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 1 | /* GSMTAP support code in libmsomcore */ |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 2 | /* |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 3 | * (C) 2010-2011 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 4 | * |
| 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 Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 25 | #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 Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 29 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 30 | #include <osmocom/core/select.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 31 | #include <osmocom/core/socket.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 32 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 33 | #include <osmocom/gsm/rsl.h> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 34 | |
| 35 | #include <arpa/inet.h> |
| 36 | #include <sys/socket.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 37 | #include <sys/types.h> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 38 | #include <netinet/in.h> |
| 39 | |
| 40 | #include <stdio.h> |
| 41 | #include <unistd.h> |
| 42 | #include <stdint.h> |
| 43 | #include <string.h> |
| 44 | #include <errno.h> |
| 45 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 46 | uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id) |
| 47 | { |
| 48 | uint8_t ret = GSMTAP_CHANNEL_UNKNOWN; |
| 49 | |
| 50 | switch (rsl_chantype) { |
| 51 | case RSL_CHAN_Bm_ACCHs: |
| 52 | ret = GSMTAP_CHANNEL_TCH_F; |
| 53 | break; |
| 54 | case RSL_CHAN_Lm_ACCHs: |
| 55 | ret = GSMTAP_CHANNEL_TCH_H; |
| 56 | break; |
| 57 | case RSL_CHAN_SDCCH4_ACCH: |
| 58 | ret = GSMTAP_CHANNEL_SDCCH4; |
| 59 | break; |
| 60 | case RSL_CHAN_SDCCH8_ACCH: |
| 61 | ret = GSMTAP_CHANNEL_SDCCH8; |
| 62 | break; |
| 63 | case RSL_CHAN_BCCH: |
| 64 | ret = GSMTAP_CHANNEL_BCCH; |
| 65 | break; |
| 66 | case RSL_CHAN_RACH: |
| 67 | ret = GSMTAP_CHANNEL_RACH; |
| 68 | break; |
| 69 | case RSL_CHAN_PCH_AGCH: |
| 70 | /* it could also be AGCH... */ |
| 71 | ret = GSMTAP_CHANNEL_PCH; |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | if (link_id & 0x40) |
| 76 | ret |= GSMTAP_CHANNEL_ACCH; |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | /* receive a message from L1/L2 and put it in GSMTAP */ |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 82 | struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, |
| 83 | uint8_t ss, uint32_t fn, int8_t signal_dbm, |
| 84 | uint8_t snr, const uint8_t *data, unsigned int len) |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 85 | { |
| 86 | struct msgb *msg; |
| 87 | struct gsmtap_hdr *gh; |
| 88 | uint8_t *dst; |
| 89 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 90 | msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx"); |
| 91 | if (!msg) |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 92 | return NULL; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 93 | |
| 94 | gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh)); |
| 95 | |
| 96 | gh->version = GSMTAP_VERSION; |
| 97 | gh->hdr_len = sizeof(*gh)/4; |
| 98 | gh->type = GSMTAP_TYPE_UM; |
| 99 | gh->timeslot = ts; |
| 100 | gh->sub_slot = ss; |
| 101 | gh->arfcn = htons(arfcn); |
| 102 | gh->snr_db = snr; |
| 103 | gh->signal_dbm = signal_dbm; |
| 104 | gh->frame_number = htonl(fn); |
| 105 | gh->sub_type = chan_type; |
| 106 | gh->antenna_nr = 0; |
| 107 | |
| 108 | dst = msgb_put(msg, len); |
| 109 | memcpy(dst, data, len); |
| 110 | |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 111 | return msg; |
| 112 | } |
| 113 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 114 | /* Open a GSMTAP source (sending) socket, conncet it to host/port and |
| 115 | * return resulting fd */ |
| 116 | int gsmtap_source_init_fd(const char *host, uint16_t port) |
| 117 | { |
| 118 | if (port == 0) |
| 119 | port = GSMTAP_UDP_PORT; |
| 120 | if (host == NULL) |
| 121 | host = "localhost"; |
| 122 | |
| 123 | return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port, 0); |
| 124 | } |
| 125 | |
| 126 | int gsmtap_source_add_sink_fd(int gsmtap_fd) |
| 127 | { |
| 128 | struct sockaddr_storage ss; |
| 129 | socklen_t ss_len = sizeof(ss); |
| 130 | int rc; |
| 131 | |
| 132 | rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len); |
| 133 | if (rc < 0) |
| 134 | return rc; |
| 135 | |
| 136 | if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) { |
| 137 | rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM, IPPROTO_UDP, 1); |
| 138 | if (rc >= 0) |
| 139 | return rc; |
| 140 | } |
| 141 | |
| 142 | return -ENODEV; |
| 143 | } |
| 144 | |
| 145 | #ifdef HAVE_SYS_SELECT_H |
| 146 | |
| 147 | int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg) |
| 148 | { |
| 149 | if (gti->ofd_wq_mode) |
| 150 | return osmo_wqueue_enqueue(>i->wq, msg); |
| 151 | else { |
| 152 | /* try immediate send and return error if any */ |
| 153 | int rc; |
| 154 | |
| 155 | rc = write(gsmtap_inst_fd(gti), msg->data, msg->len); |
| 156 | if (rc <= 0) { |
| 157 | return rc; |
| 158 | } else if (rc >= msg->len) { |
| 159 | msgb_free(msg); |
| 160 | return 0; |
| 161 | } else { |
| 162 | /* short write */ |
| 163 | return -EIO; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 168 | /* receive a message from L1/L2 and put it in GSMTAP */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 169 | int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts, |
| 170 | uint8_t chan_type, uint8_t ss, uint32_t fn, |
| 171 | int8_t signal_dbm, uint8_t snr, const uint8_t *data, |
| 172 | unsigned int len) |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 173 | { |
| 174 | struct msgb *msg; |
| 175 | |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 176 | msg = gsmtap_makemsg(arfcn, ts, chan_type, ss, fn, signal_dbm, |
| 177 | snr, data, len); |
| 178 | if (!msg) |
| 179 | return -ENOMEM; |
| 180 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 181 | return gsmtap_sendmsg(gti, msg); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* Callback from select layer if we can write to the socket */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 185 | 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] | 186 | { |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 187 | int rc; |
| 188 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 189 | rc = write(ofd->fd, msg->data, msg->len); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 190 | if (rc < 0) { |
| 191 | perror("writing msgb to gsmtap fd"); |
| 192 | msgb_free(msg); |
| 193 | return rc; |
| 194 | } |
| 195 | if (rc != msg->len) { |
| 196 | perror("short write to gsmtap fd"); |
| 197 | msgb_free(msg); |
| 198 | return -EIO; |
| 199 | } |
| 200 | |
| 201 | msgb_free(msg); |
| 202 | return 0; |
| 203 | } |
| 204 | |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 205 | /* 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] | 206 | 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] | 207 | { |
| 208 | int rc; |
| 209 | uint8_t buf[4096]; |
| 210 | |
| 211 | if (!(flags & BSC_FD_READ)) |
| 212 | return 0; |
| 213 | |
| 214 | rc = read(fd->fd, buf, sizeof(buf)); |
| 215 | if (rc < 0) { |
| 216 | perror("reading from gsmtap sink fd"); |
| 217 | return rc; |
| 218 | } |
| 219 | /* simply discard any data arriving on the socket */ |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 224 | /* Add a local sink to an existing GSMTAP source instance */ |
| 225 | int gsmtap_source_add_sink(struct gsmtap_inst *gti) |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 226 | { |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 227 | int fd; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 228 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 229 | fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti)); |
| 230 | if (fd < 0) |
| 231 | return fd; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 232 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 233 | if (gti->ofd_wq_mode) { |
| 234 | struct osmo_fd *sink_ofd; |
| 235 | |
| 236 | sink_ofd = >i->sink_ofd; |
| 237 | sink_ofd->fd = fd; |
| 238 | sink_ofd->when = BSC_FD_READ; |
| 239 | sink_ofd->cb = gsmtap_sink_fd_cb; |
| 240 | |
| 241 | osmo_fd_register(sink_ofd); |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 244 | return fd; |
| 245 | } |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 246 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 247 | /* like gsmtap_init2() but integrated with libosmocore select.c */ |
| 248 | struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port, |
| 249 | int ofd_wq_mode) |
| 250 | { |
| 251 | struct gsmtap_inst *gti; |
| 252 | int fd; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 253 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame^] | 254 | fd = gsmtap_source_init_fd(host, port); |
| 255 | if (fd < 0) |
| 256 | return NULL; |
| 257 | |
| 258 | gti = talloc_zero(NULL, struct gsmtap_inst); |
| 259 | gti->ofd_wq_mode = ofd_wq_mode; |
| 260 | gti->wq.bfd.fd = fd; |
| 261 | gti->sink_ofd.fd = -1; |
| 262 | |
| 263 | if (ofd_wq_mode) { |
| 264 | osmo_wqueue_init(>i->wq, 64); |
| 265 | gti->wq.write_cb = &gsmtap_wq_w_cb; |
| 266 | |
| 267 | osmo_fd_register(>i->wq.bfd); |
| 268 | } |
| 269 | |
| 270 | return gti; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 273 | #endif /* HAVE_SYS_SELECT_H */ |