Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 1 | /* Simple TRAU frame reflector to route voice calls */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 15 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 24 | |
| 25 | #include <openbsc/gsm_data.h> |
| 26 | #include <openbsc/trau_frame.h> |
| 27 | #include <openbsc/trau_mux.h> |
| 28 | #include <openbsc/subchan_demux.h> |
| 29 | #include <openbsc/e1_input.h> |
Harald Welte | 41e1668 | 2009-02-18 03:34:55 +0000 | [diff] [blame] | 30 | #include <openbsc/debug.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 31 | #include <osmocom/core/talloc.h> |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 32 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 33 | uint8_t gsm_fr_map[] = { |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 34 | 6, 6, 5, 5, 4, 4, 3, 3, |
| 35 | 7, 2, 2, 6, 3, 3, 3, 3, |
| 36 | 3, 3, 3, 3, 3, 3, 3, 3, |
| 37 | 3, 7, 2, 2, 6, 3, 3, 3, |
| 38 | 3, 3, 3, 3, 3, 3, 3, 3, |
| 39 | 3, 3, 7, 2, 2, 6, 3, 3, |
| 40 | 3, 3, 3, 3, 3, 3, 3, 3, |
| 41 | 3, 3, 3, 7, 2, 2, 6, 3, |
| 42 | 3, 3, 3, 3, 3, 3, 3, 3, |
| 43 | 3, 3, 3, 3 |
| 44 | }; |
| 45 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 46 | struct map_entry { |
| 47 | struct llist_head list; |
| 48 | struct gsm_e1_subslot src, dst; |
| 49 | }; |
| 50 | |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 51 | struct upqueue_entry { |
| 52 | struct llist_head list; |
| 53 | struct gsm_network *net; |
| 54 | struct gsm_e1_subslot src; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 55 | uint32_t callref; |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 58 | static LLIST_HEAD(ss_map); |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 59 | static LLIST_HEAD(ss_upqueue); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 60 | |
Harald Welte (local) | d19e58b | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 61 | void *tall_map_ctx, *tall_upq_ctx; |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 62 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 63 | /* map one particular subslot to another subslot */ |
| 64 | int trau_mux_map(const struct gsm_e1_subslot *src, |
| 65 | const struct gsm_e1_subslot *dst) |
| 66 | { |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 67 | struct map_entry *me; |
| 68 | |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 69 | me = talloc(tall_map_ctx, struct map_entry); |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 70 | if (!me) { |
| 71 | LOGP(DMIB, LOGL_FATAL, "Out of memory\n"); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 72 | return -ENOMEM; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 73 | } |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 74 | |
Harald Welte | dbd2ea8 | 2009-02-19 17:07:01 +0000 | [diff] [blame] | 75 | DEBUGP(DCC, "Setting up TRAU mux map between (e1=%u,ts=%u,ss=%u) " |
| 76 | "and (e1=%u,ts=%u,ss=%u)\n", |
| 77 | src->e1_nr, src->e1_ts, src->e1_ts_ss, |
| 78 | dst->e1_nr, dst->e1_ts, dst->e1_ts_ss); |
| 79 | |
Harald Welte | 26aa6a1 | 2009-02-19 15:14:23 +0000 | [diff] [blame] | 80 | /* make sure to get rid of any stale old mappings */ |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 81 | trau_mux_unmap(src, 0); |
| 82 | trau_mux_unmap(dst, 0); |
Harald Welte | 26aa6a1 | 2009-02-19 15:14:23 +0000 | [diff] [blame] | 83 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 84 | memcpy(&me->src, src, sizeof(me->src)); |
| 85 | memcpy(&me->dst, dst, sizeof(me->dst)); |
| 86 | llist_add(&me->list, &ss_map); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Harald Welte | 26aa6a1 | 2009-02-19 15:14:23 +0000 | [diff] [blame] | 91 | int trau_mux_map_lchan(const struct gsm_lchan *src, |
| 92 | const struct gsm_lchan *dst) |
| 93 | { |
| 94 | struct gsm_e1_subslot *src_ss, *dst_ss; |
| 95 | |
| 96 | src_ss = &src->ts->e1_link; |
| 97 | dst_ss = &dst->ts->e1_link; |
| 98 | |
| 99 | return trau_mux_map(src_ss, dst_ss); |
| 100 | } |
| 101 | |
| 102 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 103 | /* unmap one particular subslot from another subslot */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 104 | int trau_mux_unmap(const struct gsm_e1_subslot *ss, uint32_t callref) |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 105 | { |
| 106 | struct map_entry *me, *me2; |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 107 | struct upqueue_entry *ue, *ue2; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 108 | |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 109 | if (ss) |
| 110 | llist_for_each_entry_safe(me, me2, &ss_map, list) { |
| 111 | if (!memcmp(&me->src, ss, sizeof(*ss)) || |
| 112 | !memcmp(&me->dst, ss, sizeof(*ss))) { |
| 113 | llist_del(&me->list); |
| 114 | return 0; |
| 115 | } |
| 116 | } |
| 117 | llist_for_each_entry_safe(ue, ue2, &ss_upqueue, list) { |
| 118 | if (ue->callref == callref) { |
| 119 | llist_del(&ue->list); |
| 120 | return 0; |
| 121 | } |
| 122 | if (ss && !memcmp(&ue->src, ss, sizeof(*ss))) { |
| 123 | llist_del(&ue->list); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | } |
| 127 | return -ENOENT; |
| 128 | } |
| 129 | |
| 130 | /* look-up an enty in the TRAU mux map */ |
| 131 | static struct gsm_e1_subslot * |
| 132 | lookup_trau_mux_map(const struct gsm_e1_subslot *src) |
| 133 | { |
| 134 | struct map_entry *me; |
| 135 | |
| 136 | llist_for_each_entry(me, &ss_map, list) { |
| 137 | if (!memcmp(&me->src, src, sizeof(*src))) |
| 138 | return &me->dst; |
| 139 | if (!memcmp(&me->dst, src, sizeof(*src))) |
| 140 | return &me->src; |
| 141 | } |
| 142 | return NULL; |
| 143 | } |
| 144 | |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 145 | /* look-up an enty in the TRAU upqueue */ |
| 146 | struct upqueue_entry * |
| 147 | lookup_trau_upqueue(const struct gsm_e1_subslot *src) |
| 148 | { |
| 149 | struct upqueue_entry *ue; |
| 150 | |
| 151 | llist_for_each_entry(ue, &ss_upqueue, list) { |
| 152 | if (!memcmp(&ue->src, src, sizeof(*src))) |
| 153 | return ue; |
| 154 | } |
| 155 | return NULL; |
| 156 | } |
| 157 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 158 | static const uint8_t c_bits_check[] = { 0, 0, 0, 1, 0 }; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 159 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 160 | /* we get called by subchan_demux */ |
| 161 | int trau_mux_input(struct gsm_e1_subslot *src_e1_ss, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 162 | const uint8_t *trau_bits, int num_bits) |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 163 | { |
| 164 | struct decoded_trau_frame tf; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 165 | uint8_t trau_bits_out[TRAU_FRAME_BITS]; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 166 | struct gsm_e1_subslot *dst_e1_ss = lookup_trau_mux_map(src_e1_ss); |
| 167 | struct subch_mux *mx; |
Harald Welte | 4bfdfe7 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 168 | struct upqueue_entry *ue; |
Harald Welte | 41e1668 | 2009-02-18 03:34:55 +0000 | [diff] [blame] | 169 | int rc; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 170 | |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 171 | /* decode TRAU, change it to downlink, re-encode */ |
| 172 | rc = decode_trau_frame(&tf, trau_bits); |
| 173 | if (rc) |
| 174 | return rc; |
| 175 | |
Harald Welte | 4bfdfe7 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 176 | if (!dst_e1_ss) { |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 177 | struct msgb *msg; |
| 178 | struct gsm_data_frame *frame; |
| 179 | unsigned char *data; |
| 180 | int i, j, k, l, o; |
Harald Welte | 4bfdfe7 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 181 | /* frame shall be sent to upqueue */ |
| 182 | if (!(ue = lookup_trau_upqueue(src_e1_ss))) |
| 183 | return -EINVAL; |
| 184 | if (!ue->callref) |
| 185 | return -EINVAL; |
Andreas Eversberg | 56315f0 | 2009-12-21 01:18:44 +0100 | [diff] [blame] | 186 | if (memcmp(tf.c_bits, c_bits_check, sizeof(c_bits_check))) |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 187 | DEBUGPC(DMUX, "illegal trau (C1-C5) %s\n", |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 188 | osmo_hexdump(tf.c_bits, sizeof(c_bits_check))); |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 189 | msg = msgb_alloc(sizeof(struct gsm_data_frame) + 33, |
| 190 | "GSM-DATA"); |
Harald Welte | 4bfdfe7 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 191 | if (!msg) |
| 192 | return -ENOMEM; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 193 | |
| 194 | frame = (struct gsm_data_frame *)msg->data; |
| 195 | memset(frame, 0, sizeof(struct gsm_data_frame)); |
| 196 | data = frame->data; |
| 197 | data[0] = 0xd << 4; |
| 198 | /* reassemble d-bits */ |
| 199 | i = 0; /* counts bits */ |
| 200 | j = 4; /* counts output bits */ |
| 201 | k = gsm_fr_map[0]-1; /* current number bit in element */ |
| 202 | l = 0; /* counts element bits */ |
| 203 | o = 0; /* offset input bits */ |
| 204 | while (i < 260) { |
| 205 | data[j/8] |= (tf.d_bits[k+o] << (7-(j%8))); |
| 206 | if (--k < 0) { |
| 207 | o += gsm_fr_map[l]; |
| 208 | k = gsm_fr_map[++l]-1; |
| 209 | } |
| 210 | i++; |
| 211 | j++; |
| 212 | } |
| 213 | frame->msg_type = GSM_TCHF_FRAME; |
Harald Welte | 4bfdfe7 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 214 | frame->callref = ue->callref; |
Pablo Neira Ayuso | f7dc761 | 2011-08-10 02:56:58 +0200 | [diff] [blame^] | 215 | msgb_put(msg, sizeof(struct gsm_data_frame) + 33); |
Harald Welte | 31c00f7 | 2011-03-03 23:29:05 +0100 | [diff] [blame] | 216 | trau_tx_to_mncc(ue->net, msg); |
Harald Welte | 4bfdfe7 | 2009-06-10 23:11:52 +0800 | [diff] [blame] | 217 | |
| 218 | return 0; |
| 219 | } |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 220 | |
| 221 | mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts); |
| 222 | if (!mx) |
| 223 | return -EINVAL; |
| 224 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 225 | trau_frame_up2down(&tf); |
| 226 | encode_trau_frame(trau_bits_out, &tf); |
| 227 | |
| 228 | /* and send it to the muxer */ |
| 229 | return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out, |
| 230 | TRAU_FRAME_BITS); |
| 231 | } |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 232 | |
| 233 | /* add receiver instance for lchan and callref */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 234 | int trau_recv_lchan(struct gsm_lchan *lchan, uint32_t callref) |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 235 | { |
| 236 | struct gsm_e1_subslot *src_ss; |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 237 | struct upqueue_entry *ue; |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 238 | |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 239 | ue = talloc(tall_upq_ctx, struct upqueue_entry); |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 240 | if (!ue) |
| 241 | return -ENOMEM; |
| 242 | |
| 243 | src_ss = &lchan->ts->e1_link; |
| 244 | |
| 245 | DEBUGP(DCC, "Setting up TRAU receiver (e1=%u,ts=%u,ss=%u) " |
| 246 | "and (callref 0x%x)\n", |
| 247 | src_ss->e1_nr, src_ss->e1_ts, src_ss->e1_ts_ss, |
| 248 | callref); |
| 249 | |
| 250 | /* make sure to get rid of any stale old mappings */ |
| 251 | trau_mux_unmap(src_ss, callref); |
| 252 | |
| 253 | memcpy(&ue->src, src_ss, sizeof(ue->src)); |
| 254 | ue->net = lchan->ts->trx->bts->network; |
| 255 | ue->callref = callref; |
| 256 | llist_add(&ue->list, &ss_upqueue); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 261 | int trau_send_frame(struct gsm_lchan *lchan, struct gsm_data_frame *frame) |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 262 | { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 263 | uint8_t trau_bits_out[TRAU_FRAME_BITS]; |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 264 | struct gsm_e1_subslot *dst_e1_ss = &lchan->ts->e1_link; |
| 265 | struct subch_mux *mx; |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 266 | int i, j, k, l, o; |
| 267 | unsigned char *data = frame->data; |
| 268 | struct decoded_trau_frame tf; |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 269 | |
| 270 | mx = e1inp_get_mux(dst_e1_ss->e1_nr, dst_e1_ss->e1_ts); |
| 271 | if (!mx) |
| 272 | return -EINVAL; |
| 273 | |
Harald Welte | da7ab74 | 2009-12-19 22:23:05 +0100 | [diff] [blame] | 274 | switch (frame->msg_type) { |
| 275 | case GSM_TCHF_FRAME: |
| 276 | /* set c-bits and t-bits */ |
| 277 | tf.c_bits[0] = 1; |
| 278 | tf.c_bits[1] = 1; |
| 279 | tf.c_bits[2] = 1; |
| 280 | tf.c_bits[3] = 0; |
| 281 | tf.c_bits[4] = 0; |
| 282 | memset(&tf.c_bits[5], 0, 6); |
| 283 | memset(&tf.c_bits[11], 1, 10); |
| 284 | memset(&tf.t_bits[0], 1, 4); |
| 285 | /* reassemble d-bits */ |
| 286 | i = 0; /* counts bits */ |
| 287 | j = 4; /* counts input bits */ |
| 288 | k = gsm_fr_map[0]-1; /* current number bit in element */ |
| 289 | l = 0; /* counts element bits */ |
| 290 | o = 0; /* offset output bits */ |
| 291 | while (i < 260) { |
| 292 | tf.d_bits[k+o] = (data[j/8] >> (7-(j%8))) & 1; |
| 293 | if (--k < 0) { |
| 294 | o += gsm_fr_map[l]; |
| 295 | k = gsm_fr_map[++l]-1; |
| 296 | } |
| 297 | i++; |
| 298 | j++; |
| 299 | } |
| 300 | break; |
| 301 | default: |
| 302 | DEBUGPC(DMUX, "unsupported message type %d\n", |
| 303 | frame->msg_type); |
| 304 | return -EINVAL; |
| 305 | } |
| 306 | |
| 307 | encode_trau_frame(trau_bits_out, &tf); |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 308 | |
| 309 | /* and send it to the muxer */ |
| 310 | return subchan_mux_enqueue(mx, dst_e1_ss->e1_ts_ss, trau_bits_out, |
| 311 | TRAU_FRAME_BITS); |
| 312 | } |