Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 1 | /* GSM 08.08 like API for OpenBSC. The bridge from MSC to BSC */ |
| 2 | |
| 3 | /* (C) 2010 by Holger Hans Peter Freyther |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 4 | * (C) 2010 by On Waves |
Holger Hans Peter Freyther | 2a9285c | 2010-06-14 18:03:06 +0800 | [diff] [blame] | 5 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <openbsc/bsc_api.h> |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 26 | #include <openbsc/bsc_rll.h> |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 27 | #include <openbsc/gsm_data.h> |
| 28 | #include <openbsc/signal.h> |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 29 | #include <openbsc/abis_rsl.h> |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 30 | #include <openbsc/chan_alloc.h> |
Holger Hans Peter Freyther | f2553a6 | 2010-06-30 12:58:14 +0800 | [diff] [blame] | 31 | #include <openbsc/handover.h> |
Holger Hans Peter Freyther | 49ab278 | 2010-07-05 11:38:37 +0800 | [diff] [blame] | 32 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 33 | |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 34 | #include <osmocore/talloc.h> |
| 35 | |
Holger Hans Peter Freyther | 79f763f | 2010-09-16 00:53:37 +0800 | [diff] [blame] | 36 | static LLIST_HEAD(sub_connections); |
| 37 | |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 38 | static void rll_ind_cb(struct gsm_lchan *, uint8_t, void *, enum bsc_rllr_ind); |
| 39 | static void send_sapi_reject(struct gsm_subscriber_connection *conn, int link_id); |
| 40 | |
Holger Hans Peter Freyther | a2aedad | 2010-09-16 00:47:32 +0800 | [diff] [blame] | 41 | struct gsm_subscriber_connection *subscr_con_allocate(struct gsm_lchan *lchan) |
| 42 | { |
| 43 | struct gsm_subscriber_connection *conn; |
| 44 | |
| 45 | conn = talloc_zero(lchan->ts->trx->bts->network, struct gsm_subscriber_connection); |
| 46 | if (!conn) |
| 47 | return NULL; |
| 48 | |
| 49 | /* Configure the time and start it so it will be closed */ |
| 50 | conn->lchan = lchan; |
| 51 | conn->bts = lchan->ts->trx->bts; |
| 52 | lchan->conn = conn; |
Holger Hans Peter Freyther | 79f763f | 2010-09-16 00:53:37 +0800 | [diff] [blame] | 53 | llist_add_tail(&conn->entry, &sub_connections); |
Holger Hans Peter Freyther | a2aedad | 2010-09-16 00:47:32 +0800 | [diff] [blame] | 54 | return conn; |
| 55 | } |
| 56 | |
| 57 | /* TODO: move subscriber put here... */ |
| 58 | void subscr_con_free(struct gsm_subscriber_connection *conn) |
| 59 | { |
| 60 | struct gsm_lchan *lchan; |
| 61 | |
| 62 | |
| 63 | if (!conn) |
| 64 | return; |
| 65 | |
| 66 | |
| 67 | if (conn->subscr) { |
| 68 | subscr_put(conn->subscr); |
| 69 | conn->subscr = NULL; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | if (conn->ho_lchan) |
| 74 | LOGP(DNM, LOGL_ERROR, "The ho_lchan should have been cleared.\n"); |
| 75 | |
Holger Hans Peter Freyther | 79f763f | 2010-09-16 00:53:37 +0800 | [diff] [blame] | 76 | llist_del(&conn->entry); |
| 77 | |
Holger Hans Peter Freyther | a2aedad | 2010-09-16 00:47:32 +0800 | [diff] [blame] | 78 | lchan = conn->lchan; |
| 79 | talloc_free(conn); |
| 80 | |
| 81 | if (lchan) |
| 82 | lchan->conn = NULL; |
| 83 | } |
| 84 | |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 85 | int bsc_api_init(struct gsm_network *network, struct bsc_api *api) |
| 86 | { |
| 87 | network->bsc_api = api; |
| 88 | return 0; |
| 89 | } |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 90 | |
| 91 | int gsm0808_submit_dtap(struct gsm_subscriber_connection *conn, |
| 92 | struct msgb *msg, int link_id) |
| 93 | { |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 94 | uint8_t sapi = link_id & 0x7; |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 95 | msg->lchan = conn->lchan; |
| 96 | msg->trx = msg->lchan->ts->trx; |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 97 | |
Holger Hans Peter Freyther | f6d0e06 | 2010-06-17 17:48:13 +0800 | [diff] [blame] | 98 | msg->l3h = msg->data; |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 99 | if (conn->lchan->sapis[sapi] == LCHAN_SAPI_UNUSED) { |
| 100 | OBSC_LINKID_CB(msg) = link_id; |
| 101 | if (rll_establish(msg->lchan, sapi, rll_ind_cb, msg) != 0) { |
| 102 | msgb_free(msg); |
| 103 | send_sapi_reject(conn, link_id); |
| 104 | return -1; |
| 105 | } |
| 106 | return 0; |
| 107 | } else { |
| 108 | return rsl_data_request(msg, link_id); |
| 109 | } |
Holger Hans Peter Freyther | ec32b58 | 2010-03-23 07:40:46 +0100 | [diff] [blame] | 110 | } |
Holger Hans Peter Freyther | 2a9285c | 2010-06-14 18:03:06 +0800 | [diff] [blame] | 111 | |
Holger Hans Peter Freyther | c354de8 | 2010-07-23 16:49:38 +0800 | [diff] [blame] | 112 | /** |
| 113 | * Send a GSM08.08 Assignment Request. Right now this does not contain the |
| 114 | * audio codec type or the allowed rates for the config. |
| 115 | */ |
| 116 | int gsm0808_assign_req(struct gsm_subscriber_connection *conn, int chan_type, int audio) |
| 117 | { |
| 118 | struct bsc_api *api; |
| 119 | api = conn->bts->network->bsc_api; |
| 120 | |
| 121 | api->assign_fail(conn, 0); |
| 122 | return 0; |
| 123 | } |
| 124 | |
Holger Hans Peter Freyther | 7700846 | 2010-06-16 14:10:45 +0800 | [diff] [blame] | 125 | int gsm0808_page(struct gsm_bts *bts, unsigned int page_group, unsigned int mi_len, |
| 126 | uint8_t *mi, int chan_type) |
| 127 | { |
| 128 | return rsl_paging_cmd(bts, page_group, mi_len, mi, chan_type); |
| 129 | } |
| 130 | |
Holger Hans Peter Freyther | 2a9285c | 2010-06-14 18:03:06 +0800 | [diff] [blame] | 131 | /* dequeue messages to layer 4 */ |
| 132 | int bsc_upqueue(struct gsm_network *net) |
| 133 | { |
| 134 | struct gsm_mncc *mncc; |
| 135 | struct msgb *msg; |
| 136 | int work = 0; |
| 137 | |
| 138 | if (net) |
| 139 | while ((msg = msgb_dequeue(&net->upqueue))) { |
| 140 | mncc = (struct gsm_mncc *)msg->data; |
| 141 | if (net->mncc_recv) |
| 142 | net->mncc_recv(net, mncc->msg_type, mncc); |
| 143 | work = 1; /* work done */ |
| 144 | talloc_free(msg); |
| 145 | } |
| 146 | |
| 147 | return work; |
| 148 | } |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 149 | |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 150 | int gsm0408_rcvmsg(struct msgb *msg, uint8_t link_id) |
| 151 | { |
| 152 | int rc; |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 153 | struct bsc_api *api = msg->lchan->ts->trx->bts->network->bsc_api; |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 154 | struct gsm_lchan *lchan; |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 155 | |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 156 | lchan = msg->lchan; |
Holger Hans Peter Freyther | 49ab278 | 2010-07-05 11:38:37 +0800 | [diff] [blame] | 157 | if (lchan->state != LCHAN_S_ACTIVE) { |
| 158 | LOGP(DRSL, LOGL_ERROR, "Got data in non active state. discarding.\n"); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 163 | if (lchan->conn) { |
| 164 | api->dtap(lchan->conn, msg); |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 165 | } else { |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 166 | rc = BSC_API_CONN_POL_REJECT; |
| 167 | lchan->conn = subscr_con_allocate(msg->lchan); |
| 168 | |
| 169 | if (lchan->conn) |
| 170 | rc = api->compl_l3(lchan->conn, msg, 0); |
| 171 | |
| 172 | if (rc != BSC_API_CONN_POL_ACCEPT) { |
| 173 | subscr_con_free(lchan->conn); |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 174 | lchan_release(lchan, 0, 0); |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 175 | } |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Holger Hans Peter Freyther | dbc62db | 2010-07-23 16:55:24 +0800 | [diff] [blame] | 181 | int gsm0808_cipher_mode(struct gsm_subscriber_connection *conn, int cipher, |
| 182 | uint8_t *key, int len) |
| 183 | { |
| 184 | return -1; |
| 185 | } |
| 186 | |
Holger Hans Peter Freyther | aaa40b8 | 2010-09-16 20:48:15 +0800 | [diff] [blame] | 187 | /* |
| 188 | * Release all occupied RF Channels but stay around for more. |
| 189 | */ |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 190 | int gsm0808_clear(struct gsm_subscriber_connection* conn) |
| 191 | { |
Holger Hans Peter Freyther | 1f69b46 | 2010-09-16 22:49:37 +0800 | [diff] [blame] | 192 | if (conn->ho_lchan) |
| 193 | bsc_clear_handover(conn); |
Holger Hans Peter Freyther | d065165 | 2010-06-30 12:17:35 +0800 | [diff] [blame] | 194 | |
Holger Hans Peter Freyther | 1f69b46 | 2010-09-16 22:49:37 +0800 | [diff] [blame] | 195 | if (conn->lchan) { |
| 196 | lchan_release(conn->lchan, 1, 0); |
| 197 | conn->lchan->conn = NULL; |
| 198 | } |
| 199 | |
Holger Hans Peter Freyther | aaa40b8 | 2010-09-16 20:48:15 +0800 | [diff] [blame] | 200 | conn->lchan = NULL; |
| 201 | conn->ho_lchan = NULL; |
| 202 | conn->bts = NULL; |
| 203 | |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 207 | static void send_sapi_reject(struct gsm_subscriber_connection *conn, int link_id) |
| 208 | { |
| 209 | struct bsc_api *api; |
| 210 | |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 211 | if (!conn) |
| 212 | return; |
| 213 | |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 214 | api = conn->bts->network->bsc_api; |
| 215 | if (!api || !api->sapi_n_reject) |
| 216 | return; |
| 217 | |
| 218 | api->sapi_n_reject(conn, link_id); |
| 219 | } |
| 220 | |
| 221 | static void rll_ind_cb(struct gsm_lchan *lchan, uint8_t link_id, void *_data, enum bsc_rllr_ind rllr_ind) |
| 222 | { |
| 223 | struct msgb *msg = _data; |
| 224 | |
| 225 | switch (rllr_ind) { |
| 226 | case BSC_RLLR_IND_EST_CONF: |
| 227 | rsl_data_request(msg, OBSC_LINKID_CB(msg)); |
| 228 | break; |
| 229 | case BSC_RLLR_IND_REL_IND: |
| 230 | case BSC_RLLR_IND_ERR_IND: |
| 231 | case BSC_RLLR_IND_TIMEOUT: |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 232 | send_sapi_reject(lchan->conn, OBSC_LINKID_CB(msg)); |
Holger Hans Peter Freyther | 9c595b7 | 2010-06-15 11:20:52 +0800 | [diff] [blame] | 233 | msgb_free(msg); |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 238 | static int bsc_handle_lchan_signal(unsigned int subsys, unsigned int signal, |
| 239 | void *handler_data, void *signal_data) |
| 240 | { |
| 241 | struct bsc_api *bsc; |
| 242 | struct gsm_lchan *lchan; |
Holger Hans Peter Freyther | aaa40b8 | 2010-09-16 20:48:15 +0800 | [diff] [blame] | 243 | struct gsm_subscriber_connection *conn; |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 244 | |
| 245 | if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE) |
| 246 | return 0; |
| 247 | |
| 248 | lchan = (struct gsm_lchan *)signal_data; |
Holger Hans Peter Freyther | 2412a07 | 2010-06-28 15:47:12 +0800 | [diff] [blame] | 249 | if (!lchan || !lchan->conn) |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 250 | return 0; |
| 251 | |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 252 | bsc = lchan->ts->trx->bts->network->bsc_api; |
Holger Hans Peter Freyther | aaa40b8 | 2010-09-16 20:48:15 +0800 | [diff] [blame] | 253 | if (!bsc) |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 254 | return 0; |
| 255 | |
Holger Hans Peter Freyther | aaa40b8 | 2010-09-16 20:48:15 +0800 | [diff] [blame] | 256 | conn = lchan->conn; |
| 257 | if (bsc->clear_request) |
| 258 | bsc->clear_request(conn, 0); |
| 259 | |
| 260 | /* now give up all channels */ |
| 261 | if (conn->lchan == lchan) |
| 262 | conn->lchan = NULL; |
| 263 | if (conn->ho_lchan == lchan) |
| 264 | conn->ho_lchan = NULL; |
| 265 | gsm0808_clear(conn); |
| 266 | |
Holger Hans Peter Freyther | abcddf1 | 2010-06-14 18:20:15 +0800 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static __attribute__((constructor)) void on_dso_load_bsc(void) |
| 271 | { |
| 272 | register_signal_handler(SS_LCHAN, bsc_handle_lchan_signal, NULL); |
Holger Hans Peter Freyther | 0cfbe26 | 2010-06-15 11:20:24 +0800 | [diff] [blame] | 273 | } |