Holger Hans Peter Freyther | 98753bd | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 1 | /* GSM 08.08 BSSMAP handling */ |
| 2 | /* (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * (C) 2009-2010 by On-Waves |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <openbsc/osmo_bsc.h> |
Holger Hans Peter Freyther | de2bb07 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 23 | #include <openbsc/osmo_bsc_grace.h> |
Holger Hans Peter Freyther | 98753bd | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 24 | #include <openbsc/debug.h> |
| 25 | |
Holger Hans Peter Freyther | de2bb07 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 26 | #include <osmocore/gsm0808.h> |
Holger Hans Peter Freyther | 98753bd | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 27 | #include <osmocore/protocol/gsm_08_08.h> |
| 28 | |
Holger Hans Peter Freyther | de2bb07 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 29 | #include <arpa/inet.h> |
| 30 | |
| 31 | static uint16_t read_data16(const uint8_t *data) |
| 32 | { |
| 33 | uint16_t res; |
| 34 | |
| 35 | memcpy(&res, data, sizeof(res)); |
| 36 | return res; |
| 37 | } |
| 38 | |
| 39 | static int bssmap_handle_reset_ack(struct gsm_network *net, |
| 40 | struct msgb *msg, unsigned int length) |
| 41 | { |
| 42 | LOGP(DMSC, LOGL_NOTICE, "Reset ACK from MSC\n"); |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | /* GSM 08.08 § 3.2.1.19 */ |
| 47 | static int bssmap_handle_paging(struct gsm_network *net, |
| 48 | struct msgb *msg, unsigned int payload_length) |
| 49 | { |
| 50 | struct tlv_parsed tp; |
| 51 | char mi_string[GSM48_MI_SIZE]; |
| 52 | uint32_t tmsi = GSM_RESERVED_TMSI; |
| 53 | unsigned int lac = GSM_LAC_RESERVED_ALL_BTS; |
| 54 | uint8_t data_length; |
| 55 | const uint8_t *data; |
| 56 | uint8_t chan_needed = RSL_CHANNEED_ANY; |
| 57 | |
| 58 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h + 1, payload_length - 1, 0, 0); |
| 59 | |
| 60 | if (!TLVP_PRESENT(&tp, GSM0808_IE_IMSI)) { |
| 61 | LOGP(DMSC, LOGL_ERROR, "Mandantory IMSI not present.\n"); |
| 62 | return -1; |
| 63 | } else if ((TLVP_VAL(&tp, GSM0808_IE_IMSI)[0] & GSM_MI_TYPE_MASK) != GSM_MI_TYPE_IMSI) { |
| 64 | LOGP(DMSC, LOGL_ERROR, "Wrong content in the IMSI\n"); |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
| 69 | LOGP(DMSC, LOGL_ERROR, "Mandantory CELL IDENTIFIER LIST not present.\n"); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | if (TLVP_PRESENT(&tp, GSM0808_IE_TMSI)) { |
| 74 | gsm48_mi_to_string(mi_string, sizeof(mi_string), |
| 75 | TLVP_VAL(&tp, GSM0808_IE_TMSI), TLVP_LEN(&tp, GSM0808_IE_TMSI)); |
| 76 | tmsi = strtoul(mi_string, NULL, 10); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /* |
| 81 | * parse the IMSI |
| 82 | */ |
| 83 | gsm48_mi_to_string(mi_string, sizeof(mi_string), |
| 84 | TLVP_VAL(&tp, GSM0808_IE_IMSI), TLVP_LEN(&tp, GSM0808_IE_IMSI)); |
| 85 | |
| 86 | /* |
| 87 | * parse the cell identifier list |
| 88 | */ |
| 89 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 90 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 91 | |
| 92 | /* |
| 93 | * Support paging to all network or one BTS at one LAC |
| 94 | */ |
| 95 | if (data_length == 3 && data[0] == CELL_IDENT_LAC) { |
| 96 | lac = ntohs(read_data16(&data[1])); |
| 97 | } else if (data_length > 1 || (data[0] & 0x0f) != CELL_IDENT_BSS) { |
| 98 | LOGP(DMSC, LOGL_ERROR, "Unsupported Cell Identifier List: %s\n", hexdump(data, data_length)); |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | if (TLVP_PRESENT(&tp, GSM0808_IE_CHANNEL_NEEDED) && TLVP_LEN(&tp, GSM0808_IE_CHANNEL_NEEDED) == 1) |
| 103 | chan_needed = TLVP_VAL(&tp, GSM0808_IE_CHANNEL_NEEDED)[0] & 0x03; |
| 104 | |
| 105 | if (TLVP_PRESENT(&tp, GSM0808_IE_EMLPP_PRIORITY)) { |
| 106 | LOGP(DMSC, LOGL_ERROR, "eMLPP is not handled\n"); |
| 107 | } |
| 108 | |
| 109 | LOGP(DMSC, LOGL_DEBUG, "Paging request from MSC IMSI: '%s' TMSI: '0x%x/%u' LAC: 0x%x\n", mi_string, tmsi, tmsi, lac); |
| 110 | LOGP(DMSC, LOGL_ERROR, "Paging is not implemented...\n"); |
| 111 | return -1; |
| 112 | } |
| 113 | |
Holger Hans Peter Freyther | 2536f33 | 2010-11-10 09:34:47 +0100 | [diff] [blame^] | 114 | /* |
| 115 | * GSM 08.08 § 3.1.9.1 and 3.2.1.21... |
| 116 | * release our gsm_subscriber_connection and send message |
| 117 | */ |
| 118 | static int bssmap_handle_clear_command(struct osmo_bsc_sccp_con *conn, |
| 119 | struct msgb *msg, unsigned int payload_length) |
| 120 | { |
| 121 | struct msgb *resp; |
| 122 | |
| 123 | /* TODO: handle the cause of this package */ |
| 124 | |
| 125 | if (conn->conn) { |
| 126 | LOGP(DMSC, LOGL_DEBUG, "Releasing all transactions on %p\n", conn); |
| 127 | gsm0808_clear(conn->conn); |
| 128 | subscr_con_free(conn->conn); |
| 129 | conn->conn = NULL; |
| 130 | } |
| 131 | |
| 132 | /* send the clear complete message */ |
| 133 | resp = gsm0808_create_clear_complete(); |
| 134 | if (!resp) { |
| 135 | LOGP(DMSC, LOGL_ERROR, "Sending clear complete failed.\n"); |
| 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | bsc_queue_for_msc(conn, resp); |
| 140 | return 0; |
| 141 | } |
| 142 | |
Holger Hans Peter Freyther | de2bb07 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 143 | static int bssmap_rcvmsg_udt(struct gsm_network *net, |
| 144 | struct msgb *msg, unsigned int length) |
| 145 | { |
| 146 | int ret = 0; |
| 147 | |
| 148 | if (length < 1) { |
| 149 | LOGP(DMSC, LOGL_ERROR, "Not enough room: %d\n", length); |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | switch (msg->l4h[0]) { |
| 154 | case BSS_MAP_MSG_RESET_ACKNOWLEDGE: |
| 155 | ret = bssmap_handle_reset_ack(net, msg, length); |
| 156 | break; |
| 157 | case BSS_MAP_MSG_PAGING: |
| 158 | if (bsc_grace_allow_new_connection(net)) |
| 159 | ret = bssmap_handle_paging(net, msg, length); |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | static int bssmap_rcvmsg_dt1(struct osmo_bsc_sccp_con *conn, |
| 167 | struct msgb *msg, unsigned int length) |
| 168 | { |
Holger Hans Peter Freyther | 2536f33 | 2010-11-10 09:34:47 +0100 | [diff] [blame^] | 169 | int ret = 0; |
| 170 | |
| 171 | if (length < 1) { |
| 172 | LOGP(DMSC, LOGL_ERROR, "Not enough room: %d\n", length); |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | switch (msg->l4h[0]) { |
| 177 | case BSS_MAP_MSG_CLEAR_CMD: |
| 178 | ret = bssmap_handle_clear_command(conn, msg, length); |
| 179 | break; |
| 180 | default: |
| 181 | LOGP(DMSC, LOGL_DEBUG, "Unimplemented msg type: %d\n", msg->l4h[0]); |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | return ret; |
Holger Hans Peter Freyther | de2bb07 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static int dtap_rcvmsg(struct osmo_bsc_sccp_con *conn, |
| 189 | struct msgb *msg, unsigned int length) |
| 190 | { |
| 191 | return -1; |
| 192 | } |
| 193 | |
Holger Hans Peter Freyther | 98753bd | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 194 | int bsc_handle_udt(struct gsm_network *network, |
| 195 | struct bsc_msc_connection *conn, |
| 196 | struct msgb *msgb, unsigned int length) |
| 197 | { |
| 198 | struct bssmap_header *bs; |
| 199 | |
| 200 | LOGP(DMSC, LOGL_DEBUG, "Incoming SCCP message ftom MSC: %s\n", |
| 201 | hexdump(msgb->l3h, length)); |
| 202 | |
| 203 | if (length < sizeof(*bs)) { |
| 204 | LOGP(DMSC, LOGL_ERROR, "The header is too short.\n"); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | bs = (struct bssmap_header *) msgb->l3h; |
| 209 | if (bs->length < length - sizeof(*bs)) |
| 210 | return -1; |
| 211 | |
| 212 | switch (bs->type) { |
| 213 | case BSSAP_MSG_BSS_MANAGEMENT: |
Holger Hans Peter Freyther | de2bb07 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 214 | msgb->l4h = &msgb->l3h[sizeof(*bs)]; |
| 215 | bssmap_rcvmsg_udt(network, msgb, length - sizeof(*bs)); |
Holger Hans Peter Freyther | 98753bd | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 216 | break; |
| 217 | default: |
| 218 | LOGP(DMSC, LOGL_ERROR, "Unimplemented msg type: %d\n", bs->type); |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | int bsc_handle_dt1(struct osmo_bsc_sccp_con *conn, |
| 225 | struct msgb *msg, unsigned int len) |
| 226 | { |
Holger Hans Peter Freyther | de2bb07 | 2010-11-10 09:24:37 +0100 | [diff] [blame] | 227 | if (len < sizeof(struct bssmap_header)) { |
| 228 | LOGP(DMSC, LOGL_ERROR, "The header is too short.\n"); |
| 229 | } |
| 230 | |
| 231 | switch (msg->l3h[0]) { |
| 232 | case BSSAP_MSG_BSS_MANAGEMENT: |
| 233 | msg->l4h = &msg->l3h[sizeof(struct bssmap_header)]; |
| 234 | bssmap_rcvmsg_dt1(conn, msg, len - sizeof(struct bssmap_header)); |
| 235 | break; |
| 236 | case BSSAP_MSG_DTAP: |
| 237 | dtap_rcvmsg(conn, msg, len); |
| 238 | break; |
| 239 | default: |
| 240 | LOGP(DMSC, LOGL_DEBUG, "Unimplemented msg type: %d\n", msg->l3h[0]); |
| 241 | } |
| 242 | |
Holger Hans Peter Freyther | 98753bd | 2010-11-09 23:28:33 +0100 | [diff] [blame] | 243 | return -1; |
| 244 | } |