Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 1 | /* GSMTAP output for Osmocom Layer2 (will only work on the host PC) */ |
| 2 | /* |
| 3 | * (C) 2010 by Harald Welte <laforge@gnumonks.org> |
| 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 | |
| 25 | #ifdef HAVE_SYS_SELECT_H |
| 26 | |
| 27 | #include <osmocore/gsmtap_util.h> |
| 28 | #include <osmocore/logging.h> |
| 29 | #include <osmocore/protocol/gsm_04_08.h> |
| 30 | #include <osmocore/gsmtap.h> |
| 31 | #include <osmocore/msgb.h> |
| 32 | #include <osmocore/rsl.h> |
| 33 | #include <osmocore/select.h> |
| 34 | |
| 35 | #include <arpa/inet.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <netinet/in.h> |
| 38 | |
| 39 | #include <stdio.h> |
| 40 | #include <unistd.h> |
| 41 | #include <stdint.h> |
| 42 | #include <string.h> |
| 43 | #include <errno.h> |
| 44 | |
| 45 | static struct bsc_fd gsmtap_bfd = { .fd = -1 }; |
| 46 | static LLIST_HEAD(gsmtap_txqueue); |
| 47 | |
| 48 | uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id) |
| 49 | { |
| 50 | uint8_t ret = GSMTAP_CHANNEL_UNKNOWN; |
| 51 | |
| 52 | switch (rsl_chantype) { |
| 53 | case RSL_CHAN_Bm_ACCHs: |
| 54 | ret = GSMTAP_CHANNEL_TCH_F; |
| 55 | break; |
| 56 | case RSL_CHAN_Lm_ACCHs: |
| 57 | ret = GSMTAP_CHANNEL_TCH_H; |
| 58 | break; |
| 59 | case RSL_CHAN_SDCCH4_ACCH: |
| 60 | ret = GSMTAP_CHANNEL_SDCCH4; |
| 61 | break; |
| 62 | case RSL_CHAN_SDCCH8_ACCH: |
| 63 | ret = GSMTAP_CHANNEL_SDCCH8; |
| 64 | break; |
| 65 | case RSL_CHAN_BCCH: |
| 66 | ret = GSMTAP_CHANNEL_BCCH; |
| 67 | break; |
| 68 | case RSL_CHAN_RACH: |
| 69 | ret = GSMTAP_CHANNEL_RACH; |
| 70 | break; |
| 71 | case RSL_CHAN_PCH_AGCH: |
| 72 | /* it could also be AGCH... */ |
| 73 | ret = GSMTAP_CHANNEL_PCH; |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | if (link_id & 0x40) |
| 78 | ret |= GSMTAP_CHANNEL_ACCH; |
| 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | /* receive a message from L1/L2 and put it in GSMTAP */ |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 84 | struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, |
| 85 | uint8_t ss, uint32_t fn, int8_t signal_dbm, |
| 86 | uint8_t snr, const uint8_t *data, unsigned int len) |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 87 | { |
| 88 | struct msgb *msg; |
| 89 | struct gsmtap_hdr *gh; |
| 90 | uint8_t *dst; |
| 91 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 92 | msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx"); |
| 93 | if (!msg) |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 94 | return NULL; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 95 | |
| 96 | gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh)); |
| 97 | |
| 98 | gh->version = GSMTAP_VERSION; |
| 99 | gh->hdr_len = sizeof(*gh)/4; |
| 100 | gh->type = GSMTAP_TYPE_UM; |
| 101 | gh->timeslot = ts; |
| 102 | gh->sub_slot = ss; |
| 103 | gh->arfcn = htons(arfcn); |
| 104 | gh->snr_db = snr; |
| 105 | gh->signal_dbm = signal_dbm; |
| 106 | gh->frame_number = htonl(fn); |
| 107 | gh->sub_type = chan_type; |
| 108 | gh->antenna_nr = 0; |
| 109 | |
| 110 | dst = msgb_put(msg, len); |
| 111 | memcpy(dst, data, len); |
| 112 | |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 113 | return msg; |
| 114 | } |
| 115 | |
| 116 | /* receive a message from L1/L2 and put it in GSMTAP */ |
| 117 | int gsmtap_sendmsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, uint8_t ss, |
| 118 | uint32_t fn, int8_t signal_dbm, uint8_t snr, |
| 119 | const uint8_t *data, unsigned int len) |
| 120 | { |
| 121 | struct msgb *msg; |
| 122 | |
| 123 | /* gsmtap was never initialized, so don't try to send anything */ |
| 124 | if (gsmtap_bfd.fd == -1) |
| 125 | return 0; |
| 126 | |
| 127 | msg = gsmtap_makemsg(arfcn, ts, chan_type, ss, fn, signal_dbm, |
| 128 | snr, data, len); |
| 129 | if (!msg) |
| 130 | return -ENOMEM; |
| 131 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 132 | msgb_enqueue(&gsmtap_txqueue, msg); |
| 133 | gsmtap_bfd.when |= BSC_FD_WRITE; |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /* Callback from select layer if we can write to the socket */ |
| 139 | static int gsmtap_fd_cb(struct bsc_fd *fd, unsigned int flags) |
| 140 | { |
| 141 | struct msgb *msg; |
| 142 | int rc; |
| 143 | |
| 144 | if (!(flags & BSC_FD_WRITE)) |
| 145 | return 0; |
| 146 | |
| 147 | msg = msgb_dequeue(&gsmtap_txqueue); |
| 148 | if (!msg) { |
| 149 | /* no more messages in the queue, disable READ cb */ |
| 150 | gsmtap_bfd.when = 0; |
| 151 | return 0; |
| 152 | } |
| 153 | rc = write(gsmtap_bfd.fd, msg->data, msg->len); |
| 154 | if (rc < 0) { |
| 155 | perror("writing msgb to gsmtap fd"); |
| 156 | msgb_free(msg); |
| 157 | return rc; |
| 158 | } |
| 159 | if (rc != msg->len) { |
| 160 | perror("short write to gsmtap fd"); |
| 161 | msgb_free(msg); |
| 162 | return -EIO; |
| 163 | } |
| 164 | |
| 165 | msgb_free(msg); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | int gsmtap_init(uint32_t dst_ip) |
| 170 | { |
| 171 | int rc; |
| 172 | struct sockaddr_in sin; |
| 173 | |
| 174 | sin.sin_family = AF_INET; |
| 175 | sin.sin_port = htons(GSMTAP_UDP_PORT); |
| 176 | sin.sin_addr.s_addr = htonl(dst_ip); |
| 177 | |
| 178 | /* FIXME: create socket */ |
| 179 | rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 180 | if (rc < 0) { |
| 181 | perror("creating UDP socket"); |
| 182 | return rc; |
| 183 | } |
| 184 | gsmtap_bfd.fd = rc; |
| 185 | rc = connect(rc, (struct sockaddr *)&sin, sizeof(sin)); |
| 186 | if (rc < 0) { |
| 187 | perror("connecting UDP socket"); |
| 188 | close(gsmtap_bfd.fd); |
| 189 | gsmtap_bfd.fd = 0; |
| 190 | return rc; |
| 191 | } |
| 192 | |
| 193 | gsmtap_bfd.when = BSC_FD_WRITE; |
| 194 | gsmtap_bfd.cb = gsmtap_fd_cb; |
| 195 | gsmtap_bfd.data = NULL; |
| 196 | |
| 197 | return bsc_register_fd(&gsmtap_bfd); |
| 198 | } |
| 199 | |
| 200 | #endif /* HAVE_SYS_SELECT_H */ |