Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame^] | 1 | /* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface |
| 2 | * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */ |
| 3 | |
| 4 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
| 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 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <errno.h> |
| 28 | |
| 29 | #include "msgb.h" |
| 30 | #include "debug.h" |
| 31 | #include "gsm_data.h" |
| 32 | #include "gsm_subscriber.h" |
| 33 | #include "gsm_04_08.h" |
| 34 | |
| 35 | #define GSM0408_ALLOC_SIZE 1024 |
| 36 | |
| 37 | struct gsm_lai { |
| 38 | u_int16_t mcc; |
| 39 | u_int16_t mnc; |
| 40 | u_int16_t lac; |
| 41 | }; |
| 42 | |
| 43 | static void parse_lai(struct gsm_lai *lai, const struct gsm48_loc_area_id *lai48) |
| 44 | { |
| 45 | u_int8_t dig[4]; |
| 46 | |
| 47 | /* MCC */ |
| 48 | dig[1] = lai48->digits[0] & 0x0f; |
| 49 | dig[2] = lai48->digits[0] >> 4; |
| 50 | dig[3] = lai48->digits[1] & 0x0f; |
| 51 | lai->mcc = dig[3] * 100 + dig[2]; |
| 52 | |
| 53 | /* MNC */ |
| 54 | dig[1] = lai48->digits[1] >> 4; |
| 55 | dig[2] = lai48->digits[2] & 0x0f; |
| 56 | dig[3] = lai48->digits[2] >> 4; |
| 57 | lai->mnc = dig[3] * 100 + dig[2]; |
| 58 | |
| 59 | lai->lac = lai48->lac; |
| 60 | } |
| 61 | |
| 62 | static void to_bcd(u_int8_t *bcd, u_int16_t val) |
| 63 | { |
| 64 | bcd[0] = val % 10; |
| 65 | val = val / 10; |
| 66 | bcd[1] = val % 10; |
| 67 | val = val / 10; |
| 68 | bcd[2] = val % 10; |
| 69 | val = val / 10; |
| 70 | } |
| 71 | |
| 72 | static void generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc, |
| 73 | u_int16_t mnc, u_int16_t lac) |
| 74 | { |
| 75 | u_int8_t bcd[3]; |
| 76 | |
| 77 | to_bcd(bcd, mcc); |
| 78 | lai48->digits[0] = bcd[0] | (bcd[1] << 4); |
| 79 | lai48->digits[1] = bcd[2]; |
| 80 | |
| 81 | to_bcd(bcd, mnc); |
| 82 | lai48->digits[2] |= bcd[2] << 4; |
| 83 | lai48->digits[3] = bcd[0] | (bcd[1] << 4); |
| 84 | |
| 85 | lai48->lac = lac; |
| 86 | } |
| 87 | |
| 88 | #define TMSI_LEN 4 |
| 89 | #define MID_TMSI_LEN (TMSI_LEN + 2) |
| 90 | |
| 91 | static void generate_mid_from_tmsi(u_int8_t *buf, u_int8_t *tmsi_bcd) |
| 92 | { |
| 93 | buf[0] = MID_TMSI_LEN; |
| 94 | buf[1] = 0xf0 | GSM_MI_TYPE_TMSI; |
| 95 | buf[2] = tmsi_bcd[0]; |
| 96 | buf[3] = tmsi_bcd[1]; |
| 97 | buf[4] = tmsi_bcd[2]; |
| 98 | buf[5] = tmsi_bcd[3]; |
| 99 | } |
| 100 | |
| 101 | static int gsm0408_sendmsg(struct msgb *msg) |
| 102 | { |
| 103 | /* FIXME: set data pointer to beginning of L3 data object */ |
| 104 | |
| 105 | return rsl_data_request(msg); |
| 106 | } |
| 107 | |
| 108 | static int gsm0408_rcv_cc(struct msgb *msg) |
| 109 | { |
| 110 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 111 | |
| 112 | switch (gh->msg_type & 0xbf) { |
| 113 | case GSM48_MT_CC_CALL_CONF: |
| 114 | /* Response to SETUP */ |
| 115 | DEBUGP(DCC, "CALL CONFIRM\n"); |
| 116 | break; |
| 117 | case GSM48_MT_CC_RELEASE_COMPL: |
| 118 | DEBUGP(DCC, "RELEASE COMPLETE\n"); |
| 119 | break; |
| 120 | case GSM48_MT_CC_ALERTING: |
| 121 | DEBUGP(DCC, "ALERTING\n"); |
| 122 | break; |
| 123 | case GSM48_MT_CC_CONNECT: |
| 124 | DEBUGP(DCC, "CONNECT\n"); |
| 125 | /* need to respond with CONNECT_ACK */ |
| 126 | break; |
| 127 | case GSM48_MT_CC_RELEASE: |
| 128 | DEBUGP(DCC, "RELEASE\n"); |
| 129 | /* need to respond with RELEASE_COMPLETE */ |
| 130 | break; |
| 131 | case GSM48_MT_CC_EMERG_SETUP: |
| 132 | //DEBUGP(DCC, "EMERGENCY SETUP\n"); |
| 133 | case GSM48_MT_CC_SETUP: |
| 134 | //DEBUGP(DCC, "SETUP\n"); |
| 135 | /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */ |
| 136 | default: |
| 137 | fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n", |
| 138 | gh->msg_type); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* Chapter 9.2.14 : Send LOCATION UPDATE REJECT */ |
| 144 | int gsm0408_loc_upd_rej(struct gsm_bts_link *bts_link, u_int8_t cause) |
| 145 | { |
| 146 | struct msgb *msg = msgb_alloc(GSM0408_ALLOC_SIZE); |
| 147 | struct gsm48_hdr *gh; |
| 148 | |
| 149 | msg->bts_link = bts_link; |
| 150 | |
| 151 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 152 | gh->proto_discr = GSM48_PDISC_MM; |
| 153 | gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT; |
| 154 | gh->data[0] = cause; |
| 155 | |
| 156 | DEBUGP(DMM, "-> LOCATION UPDATE REJECT\n"); |
| 157 | |
| 158 | return gsm0408_sendmsg(msg); |
| 159 | } |
| 160 | |
| 161 | /* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */ |
| 162 | int gsm0408_loc_upd_acc(struct gsm_bts_link *bts_link, u_int8_t *tmsi) |
| 163 | { |
| 164 | struct gsm_bts *bts = bts_link->bts; |
| 165 | struct msgb *msg = msgb_alloc(GSM0408_ALLOC_SIZE); |
| 166 | struct gsm48_hdr *gh; |
| 167 | struct gsm48_loc_area_id *lai; |
| 168 | u_int8_t *mid; |
| 169 | |
| 170 | msg->bts_link = bts_link; |
| 171 | |
| 172 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 173 | gh->proto_discr = GSM48_PDISC_MM; |
| 174 | gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT; |
| 175 | |
| 176 | lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai)); |
| 177 | generate_lai(lai, bts->network->country_code, |
| 178 | bts->network->network_code, bts->location_area_code); |
| 179 | |
| 180 | mid = msgb_put(msg, MID_TMSI_LEN); |
| 181 | generate_mid_from_tmsi(mid, tmsi); |
| 182 | |
| 183 | DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n"); |
| 184 | |
| 185 | return gsm0408_sendmsg(msg); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | /* Chapter 9.2.15 */ |
| 190 | static int mm_loc_upd_req(struct msgb *msg) |
| 191 | { |
| 192 | struct gsm_bts *bts = msg->bts_link->bts; |
| 193 | struct gsm48_loc_upd_req *lu; |
| 194 | struct gsm_subscriber *subscr; |
| 195 | |
| 196 | u_int8_t mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
| 197 | |
| 198 | switch (mi_type) { |
| 199 | case GSM_MI_TYPE_IMSI: |
| 200 | /* look up subscriber based on IMSI */ |
| 201 | subscr = subscr_get_by_imsi(&lu->mi[1]); |
| 202 | break; |
| 203 | case GSM_MI_TYPE_TMSI: |
| 204 | /* look up the subscriber based on TMSI, request IMSI if it fails */ |
| 205 | subscr = subscr_get_by_tmsi(&lu->mi[1]); |
| 206 | if (!subscr) { |
| 207 | /* FIXME: send IDENTITY REQUEST message to get IMSI */ |
| 208 | //gsm0408_identity_request(...GSM_MI_TYPE_IMSI); |
| 209 | } |
| 210 | break; |
| 211 | case GSM_MI_TYPE_IMEI: |
| 212 | case GSM_MI_TYPE_IMEISV: |
| 213 | /* no sim card... FIXME: what to do ? */ |
| 214 | fprintf(stderr, "Unimplemented mobile identity type\n"); |
| 215 | break; |
| 216 | default: |
| 217 | fprintf(stderr, "Unknown mobile identity type\n"); |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | if (!subscr) { |
| 222 | /* 0x16 is congestion */ |
| 223 | gsm0408_loc_upd_rej(msg->bts_link, 0x16); |
| 224 | return -EINVAL; |
| 225 | } |
| 226 | |
| 227 | subscr_update(subscr, bts); |
| 228 | return gsm0408_loc_upd_acc(msg->bts_link, subscr->tmsi); |
| 229 | } |
| 230 | |
| 231 | static int gsm0408_rcv_mm(struct msgb *msg) |
| 232 | { |
| 233 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 234 | int rc; |
| 235 | |
| 236 | switch (gh->msg_type & 0xbf) { |
| 237 | case GSM48_MT_MM_LOC_UPD_REQUEST: |
| 238 | DEBUGP(DMM, "LOCATION UPDATE REQUEST\n"); |
| 239 | rc = mm_loc_upd_req(msg); |
| 240 | break; |
| 241 | case GSM48_MT_MM_ID_RESP: |
| 242 | case GSM48_MT_MM_TMSI_REALL_COMPL: |
| 243 | case GSM48_MT_MM_AUTH_RESP: |
| 244 | case GSM48_MT_MM_IMSI_DETACH_IND: |
| 245 | case GSM48_MT_MM_CM_SERV_REQ: |
| 246 | case GSM48_MT_MM_CM_REEST_REQ: |
| 247 | fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n", |
| 248 | gh->msg_type); |
| 249 | break; |
| 250 | default: |
| 251 | fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n", |
| 252 | gh->msg_type); |
| 253 | break; |
| 254 | } |
| 255 | |
| 256 | return rc; |
| 257 | } |
| 258 | static int gsm0408_rcv_rr(struct msgb *msg) |
| 259 | { |
| 260 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 261 | |
| 262 | switch (gh->msg_type) { |
| 263 | case GSM48_MT_RR_CLSM_CHG: |
| 264 | DEBUGP(DRR, "CLASSMARK CHANGE\n"); |
| 265 | /* FIXME: what to do ?!? */ |
| 266 | break; |
| 267 | case GSM48_MT_RR_PAG_RESP: |
| 268 | default: |
| 269 | fprintf(stderr, "Unimplemented GSM 04.08 msg type 0x%02x\n", |
| 270 | gh->msg_type); |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */ |
| 278 | int gsm0408_rcvmsg(struct msgb *msg) |
| 279 | { |
| 280 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 281 | u_int8_t pdisc = gh->proto_discr & 0x0f; |
| 282 | int rc; |
| 283 | |
| 284 | switch (pdisc) { |
| 285 | case GSM48_PDISC_CC: |
| 286 | rc = gsm0408_rcv_cc(msg); |
| 287 | break; |
| 288 | case GSM48_PDISC_MM: |
| 289 | rc = gsm0408_rcv_mm(msg); |
| 290 | break; |
| 291 | case GSM48_PDISC_RR: |
| 292 | rc = gsm0408_rcv_rr(msg); |
| 293 | break; |
| 294 | case GSM48_PDISC_MM_GPRS: |
| 295 | case GSM48_PDISC_SM: |
| 296 | fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n", |
| 297 | pdisc); |
| 298 | break; |
| 299 | default: |
| 300 | fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n", |
| 301 | pdisc); |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | return rc; |
| 306 | } |