Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* Point-to-Point (PP) Short Message Service (SMS) |
| 2 | * Support on Mobile Radio Interface |
| 3 | * 3GPP TS 04.11 version 7.1.0 Release 1998 / ETSI TS 100 942 V7.1.0 */ |
| 4 | |
| 5 | /* (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de> |
| 6 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 7 | * |
| 8 | * All Rights Reserved |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <errno.h> |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 31 | #include <time.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 32 | #include <netinet/in.h> |
| 33 | |
| 34 | #include <openbsc/msgb.h> |
| 35 | #include <openbsc/tlv.h> |
| 36 | #include <openbsc/debug.h> |
| 37 | #include <openbsc/gsm_data.h> |
| 38 | #include <openbsc/gsm_subscriber.h> |
| 39 | #include <openbsc/gsm_04_11.h> |
| 40 | #include <openbsc/gsm_04_08.h> |
| 41 | #include <openbsc/gsm_utils.h> |
| 42 | #include <openbsc/abis_rsl.h> |
| 43 | #include <openbsc/signal.h> |
| 44 | #include <openbsc/db.h> |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 45 | #include <openbsc/talloc.h> |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 46 | #include <openbsc/transaction.h> |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 47 | #include <openbsc/paging.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 48 | |
| 49 | #define GSM411_ALLOC_SIZE 1024 |
| 50 | #define GSM411_ALLOC_HEADROOM 128 |
| 51 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 52 | static void *tall_gsms_ctx; |
| 53 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 54 | static u_int32_t new_callref = 0x40000001; |
| 55 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 56 | struct gsm_sms *sms_alloc(void) |
| 57 | { |
| 58 | return talloc_zero(tall_gsms_ctx, struct gsm_sms); |
| 59 | } |
| 60 | |
| 61 | void sms_free(struct gsm_sms *sms) |
| 62 | { |
| 63 | /* drop references to subscriber structure */ |
| 64 | if (sms->sender) |
| 65 | subscr_put(sms->sender); |
| 66 | if (sms->receiver) |
| 67 | subscr_put(sms->receiver); |
| 68 | |
| 69 | talloc_free(sms); |
| 70 | } |
| 71 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 72 | struct msgb *gsm411_msgb_alloc(void) |
| 73 | { |
Harald Welte | 9cfc935 | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 74 | return msgb_alloc_headroom(GSM411_ALLOC_SIZE, GSM411_ALLOC_HEADROOM, |
| 75 | "GSM 04.11"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 76 | } |
| 77 | |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 78 | static int gsm411_sendmsg(struct msgb *msg) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 79 | { |
| 80 | if (msg->lchan) |
| 81 | msg->trx = msg->lchan->ts->trx; |
| 82 | |
| 83 | msg->l3h = msg->data; |
| 84 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 85 | DEBUGP(DSMS, "GSM4.11 TX %s\n", hexdump(msg->data, msg->len)); |
| 86 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 87 | return rsl_data_request(msg, 0); |
| 88 | } |
| 89 | |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 90 | /* Prefix msg with a 04.08/04.11 CP header */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 91 | static int gsm411_cp_sendmsg(struct msgb *msg, struct gsm_trans *trans, |
| 92 | u_int8_t msg_type) |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 93 | { |
| 94 | struct gsm48_hdr *gh; |
| 95 | |
| 96 | gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh)); |
| 97 | /* Outgoing needs the highest bit set */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 98 | gh->proto_discr = trans->protocol | (trans->transaction_id<<4); |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 99 | gh->msg_type = msg_type; |
| 100 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 101 | /* assign the outgoing lchan */ |
| 102 | msg->lchan = trans->lchan; |
| 103 | |
| 104 | /* mobile originating */ |
| 105 | switch (gh->msg_type) { |
| 106 | case GSM411_MT_CP_DATA: |
| 107 | /* 5.2.3.1.2: enter MO-wait for CP-ack */ |
| 108 | trans->sms.cp_state = GSM411_CPS_WAIT_CP_ACK; |
| 109 | break; |
| 110 | } |
| 111 | |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 112 | return gsm411_sendmsg(msg); |
| 113 | } |
| 114 | |
| 115 | /* Prefix msg with a RP-DATA header and send as CP-DATA */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 116 | static int gsm411_rp_sendmsg(struct msgb *msg, struct gsm_trans *trans, |
| 117 | u_int8_t rp_msg_type, u_int8_t rp_msg_ref) |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 118 | { |
| 119 | struct gsm411_rp_hdr *rp; |
| 120 | |
| 121 | /* GSM 04.11 RP-DATA header */ |
| 122 | rp = (struct gsm411_rp_hdr *)msgb_push(msg, sizeof(*rp)); |
| 123 | rp->len = msg->len; |
| 124 | rp->msg_type = rp_msg_type; |
| 125 | rp->msg_ref = rp_msg_ref; /* FIXME: Choose randomly */ |
| 126 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 127 | return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_DATA); |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 130 | static unsigned long gsm340_validity_period(u_int8_t sms_vpf, u_int8_t *sms_vp) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 131 | { |
| 132 | u_int8_t vp; |
| 133 | unsigned long minutes; |
| 134 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 135 | switch (sms_vpf) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 136 | case GSM340_TP_VPF_RELATIVE: |
| 137 | /* Chapter 9.2.3.12.1 */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 138 | vp = *(sms_vp); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 139 | if (vp <= 143) |
| 140 | minutes = vp + 1 * 5; |
| 141 | else if (vp <= 167) |
| 142 | minutes = 12*60 + (vp-143) * 30; |
| 143 | else if (vp <= 196) |
| 144 | minutes = vp-166 * 60 * 24; |
| 145 | else |
| 146 | minutes = vp-192 * 60 * 24 * 7; |
| 147 | break; |
| 148 | case GSM340_TP_VPF_ABSOLUTE: |
| 149 | /* Chapter 9.2.3.12.2 */ |
| 150 | /* FIXME: like service center time stamp */ |
| 151 | DEBUGP(DSMS, "VPI absolute not implemented yet\n"); |
| 152 | break; |
| 153 | case GSM340_TP_VPF_ENHANCED: |
| 154 | /* Chapter 9.2.3.12.3 */ |
| 155 | /* FIXME: implementation */ |
| 156 | DEBUGP(DSMS, "VPI enhanced not implemented yet\n"); |
| 157 | break; |
| 158 | } |
| 159 | return minutes; |
| 160 | } |
| 161 | |
| 162 | /* determine coding alphabet dependent on GSM 03.38 Section 4 DCS */ |
| 163 | enum sms_alphabet gsm338_get_sms_alphabet(u_int8_t dcs) |
| 164 | { |
| 165 | u_int8_t cgbits = dcs >> 4; |
| 166 | enum sms_alphabet alpha = DCS_NONE; |
| 167 | |
| 168 | if ((cgbits & 0xc) == 0) { |
| 169 | if (cgbits & 2) |
| 170 | DEBUGP(DSMS, "Compressed SMS not supported yet\n"); |
| 171 | |
| 172 | switch (dcs & 3) { |
| 173 | case 0: |
| 174 | alpha = DCS_7BIT_DEFAULT; |
| 175 | break; |
| 176 | case 1: |
| 177 | alpha = DCS_8BIT_DATA; |
| 178 | break; |
| 179 | case 2: |
| 180 | alpha = DCS_UCS2; |
| 181 | break; |
| 182 | } |
| 183 | } else if (cgbits == 0xc || cgbits == 0xd) |
| 184 | alpha = DCS_7BIT_DEFAULT; |
| 185 | else if (cgbits == 0xe) |
| 186 | alpha = DCS_UCS2; |
| 187 | else if (cgbits == 0xf) { |
| 188 | if (dcs & 4) |
| 189 | alpha = DCS_8BIT_DATA; |
| 190 | else |
| 191 | alpha = DCS_7BIT_DEFAULT; |
| 192 | } |
| 193 | |
| 194 | return alpha; |
| 195 | } |
| 196 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 197 | static int gsm340_rx_sms_submit(struct msgb *msg, struct gsm_sms *gsms) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 198 | { |
| 199 | if (db_sms_store(gsms) != 0) { |
| 200 | DEBUGP(DSMS, "Failed to store SMS in Database\n"); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 201 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 202 | } |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 203 | /* dispatch a signal to tell higher level about it */ |
| 204 | dispatch_signal(SS_SMS, S_SMS_SUBMITTED, gsms); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 208 | /* generate a TPDU address field compliant with 03.40 sec. 9.1.2.5 */ |
| 209 | static int gsm340_gen_oa(u_int8_t *oa, unsigned int oa_len, |
| 210 | struct gsm_subscriber *subscr) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 211 | { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 212 | int len_in_bytes; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 213 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 214 | oa[1] = 0xb9; /* networks-specific number, private numbering plan */ |
| 215 | |
| 216 | len_in_bytes = encode_bcd_number(oa, oa_len, 1, subscr->extension); |
| 217 | |
| 218 | /* GSM 03.40 tells us the length is in 'useful semi-octets' */ |
| 219 | oa[0] = strlen(subscr->extension) & 0xff; |
| 220 | |
| 221 | return len_in_bytes; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static u_int8_t bcdify(u_int8_t value) |
| 225 | { |
| 226 | u_int8_t ret; |
| 227 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 228 | ret = value / 10; |
| 229 | ret |= (value % 10) << 4; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | /* Generate 03.40 TP-SCTS */ |
| 235 | static void gsm340_gen_scts(u_int8_t *scts, time_t time) |
| 236 | { |
| 237 | struct tm *tm = localtime(&time); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 238 | |
| 239 | *scts++ = bcdify(tm->tm_year % 100); |
| 240 | *scts++ = bcdify(tm->tm_mon); |
| 241 | *scts++ = bcdify(tm->tm_mday); |
| 242 | *scts++ = bcdify(tm->tm_hour); |
| 243 | *scts++ = bcdify(tm->tm_min); |
| 244 | *scts++ = bcdify(tm->tm_sec); |
| 245 | *scts++ = 0; /* FIXME: timezone */ |
| 246 | } |
| 247 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 248 | /* generate a msgb containing a TPDU derived from struct gsm_sms, |
| 249 | * returns total size of TPDU */ |
| 250 | static int gsm340_gen_tpdu(struct msgb *msg, struct gsm_sms *sms) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 251 | { |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 252 | u_int8_t *smsp; |
| 253 | u_int8_t oa[12]; /* max len per 03.40 */ |
| 254 | u_int8_t oa_len = 0; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 255 | unsigned int old_msg_len = msg->len; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 256 | |
| 257 | /* generate first octet with masked bits */ |
| 258 | smsp = msgb_put(msg, 1); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 259 | /* TP-MTI (message type indicator) */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 260 | *smsp = GSM340_SMS_DELIVER_SC2MS; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 261 | /* TP-MMS (more messages to send) */ |
| 262 | if (0 /* FIXME */) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 263 | *smsp |= 0x04; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 264 | /* TP-SRI(deliver)/SRR(submit) */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 265 | if (sms->status_rep_req) |
| 266 | *smsp |= 0x20; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 267 | /* TP-UDHI (indicating TP-UD contains a header) */ |
| 268 | if (sms->ud_hdr_ind) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 269 | *smsp |= 0x40; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 270 | #if 0 |
| 271 | /* TP-RP (indicating that a reply path exists) */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 272 | if (sms-> |
| 273 | *smsp |= 0x80; |
| 274 | #endif |
| 275 | |
| 276 | /* generate originator address */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 277 | oa_len = gsm340_gen_oa(oa, sizeof(oa), sms->sender); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 278 | smsp = msgb_put(msg, oa_len); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 279 | memcpy(smsp, oa, oa_len); |
| 280 | |
| 281 | /* generate TP-PID */ |
| 282 | smsp = msgb_put(msg, 1); |
| 283 | *smsp = sms->protocol_id; |
| 284 | |
| 285 | /* generate TP-DCS */ |
| 286 | smsp = msgb_put(msg, 1); |
| 287 | *smsp = sms->data_coding_scheme; |
| 288 | |
| 289 | /* generate TP-SCTS */ |
| 290 | smsp = msgb_put(msg, 7); |
| 291 | gsm340_gen_scts(smsp, time(NULL)); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 292 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 293 | /* generate TP-UDL */ |
| 294 | smsp = msgb_put(msg, 1); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 295 | *smsp = sms->user_data_len; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 296 | |
| 297 | /* generate TP-UD */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 298 | smsp = msgb_put(msg, sms->user_data_len); |
| 299 | memcpy(smsp, sms->user_data, sms->user_data_len); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 300 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 301 | return msg->len - old_msg_len; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 304 | /* process an incoming TPDU (called from RP-DATA) |
| 305 | * return value > 0: RP CAUSE for ERROR; < 0: silent error; 0 = success */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 306 | static int gsm340_rx_tpdu(struct msgb *msg) |
| 307 | { |
Harald Welte | 7535041 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 308 | struct gsm_bts *bts = msg->lchan->ts->trx->bts; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 309 | u_int8_t *smsp = msgb_sms(msg); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 310 | struct gsm_sms *gsms; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 311 | u_int8_t sms_mti, sms_mms, sms_vpf, sms_alphabet, sms_rp; |
| 312 | u_int8_t *sms_vp; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 313 | u_int8_t da_len_bytes; |
| 314 | u_int8_t address_lv[12]; /* according to 03.40 / 9.1.2.5 */ |
| 315 | int rc = 0; |
| 316 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 317 | gsms = sms_alloc(); |
| 318 | if (!gsms) |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 319 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 320 | |
| 321 | /* invert those fields where 0 means active/present */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 322 | sms_mti = *smsp & 0x03; |
| 323 | sms_mms = !!(*smsp & 0x04); |
| 324 | sms_vpf = (*smsp & 0x18) >> 3; |
| 325 | gsms->status_rep_req = (*smsp & 0x20); |
| 326 | gsms->ud_hdr_ind = (*smsp & 0x40); |
| 327 | sms_rp = (*smsp & 0x80); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 328 | |
| 329 | smsp++; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 330 | gsms->msg_ref = *smsp++; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 331 | |
| 332 | /* length in bytes of the destination address */ |
| 333 | da_len_bytes = 2 + *smsp/2 + *smsp%2; |
| 334 | if (da_len_bytes > 12) { |
| 335 | DEBUGP(DSMS, "Destination Address > 12 bytes ?!?\n"); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 336 | rc = GSM411_RP_CAUSE_SEMANT_INC_MSG; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 337 | goto out; |
| 338 | } |
Harald Welte | 3794e15 | 2009-06-12 02:42:11 +0800 | [diff] [blame] | 339 | memset(address_lv, 0, sizeof(address_lv)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 340 | memcpy(address_lv, smsp, da_len_bytes); |
| 341 | /* mangle first byte to reflect length in bytes, not digits */ |
Harald Welte | 3794e15 | 2009-06-12 02:42:11 +0800 | [diff] [blame] | 342 | address_lv[0] = da_len_bytes - 1; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 343 | /* convert to real number */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 344 | decode_bcd_number(gsms->dest_addr, sizeof(gsms->dest_addr), address_lv, 1); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 345 | smsp += da_len_bytes; |
| 346 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 347 | gsms->protocol_id = *smsp++; |
| 348 | gsms->data_coding_scheme = *smsp++; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 349 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 350 | sms_alphabet = gsm338_get_sms_alphabet(gsms->data_coding_scheme); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 351 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 352 | switch (sms_vpf) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 353 | case GSM340_TP_VPF_RELATIVE: |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 354 | sms_vp = smsp++; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 355 | break; |
| 356 | case GSM340_TP_VPF_ABSOLUTE: |
| 357 | case GSM340_TP_VPF_ENHANCED: |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 358 | sms_vp = smsp; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 359 | smsp += 7; |
| 360 | break; |
| 361 | default: |
| 362 | DEBUGP(DSMS, "SMS Validity period not implemented: 0x%02x\n", |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 363 | sms_vpf); |
| 364 | return GSM411_RP_CAUSE_MO_NET_OUT_OF_ORDER; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 365 | } |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 366 | gsms->user_data_len = *smsp++; |
| 367 | if (gsms->user_data_len) { |
| 368 | memcpy(gsms->user_data, smsp, gsms->user_data_len); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 369 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 370 | switch (sms_alphabet) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 371 | case DCS_7BIT_DEFAULT: |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 372 | gsm_7bit_decode(gsms->text, smsp, gsms->user_data_len); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 373 | break; |
| 374 | case DCS_8BIT_DATA: |
| 375 | case DCS_UCS2: |
| 376 | case DCS_NONE: |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | DEBUGP(DSMS, "SMS:\nMTI: 0x%02x, VPF: 0x%02x, MR: 0x%02x " |
| 382 | "PID: 0x%02x, DCS: 0x%02x, DA: %s, UserDataLength: 0x%02x " |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 383 | "UserData: \"%s\"\n", sms_mti, sms_vpf, gsms->msg_ref, |
| 384 | gsms->protocol_id, gsms->data_coding_scheme, |
| 385 | gsms->dest_addr, gsms->user_data_len, |
| 386 | sms_alphabet == DCS_7BIT_DEFAULT ? gsms->text : |
| 387 | hexdump(gsms->user_data, gsms->user_data_len)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 388 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 389 | gsms->sender = subscr_get(msg->lchan->subscr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 390 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 391 | gsms->validity_minutes = gsm340_validity_period(sms_vpf, sms_vp); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 392 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 393 | dispatch_signal(SS_SMS, 0, gsms); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 394 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 395 | /* determine gsms->receiver based on dialled number */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 396 | gsms->receiver = subscr_get_by_extension(bts->network, gsms->dest_addr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 397 | if (!gsms->receiver) { |
| 398 | rc = 1; /* cause 1: unknown subscriber */ |
| 399 | goto out; |
| 400 | } |
| 401 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 402 | switch (sms_mti) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 403 | case GSM340_SMS_SUBMIT_MS2SC: |
| 404 | /* MS is submitting a SMS */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 405 | rc = gsm340_rx_sms_submit(msg, gsms); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 406 | break; |
| 407 | case GSM340_SMS_COMMAND_MS2SC: |
| 408 | case GSM340_SMS_DELIVER_REP_MS2SC: |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 409 | DEBUGP(DSMS, "Unimplemented MTI 0x%02x\n", sms_mti); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 410 | rc = GSM411_RP_CAUSE_IE_NOTEXIST; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 411 | break; |
| 412 | default: |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 413 | DEBUGP(DSMS, "Undefined MTI 0x%02x\n", sms_mti); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 414 | rc = GSM411_RP_CAUSE_IE_NOTEXIST; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 415 | break; |
| 416 | } |
| 417 | |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 418 | if (!rc && !gsms->receiver) |
| 419 | rc = GSM411_RP_CAUSE_MO_NUM_UNASSIGNED; |
| 420 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 421 | out: |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 422 | sms_free(gsms); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 423 | |
| 424 | return rc; |
| 425 | } |
| 426 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 427 | static int gsm411_send_rp_ack(struct gsm_trans *trans, u_int8_t msg_ref) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 428 | { |
| 429 | struct msgb *msg = gsm411_msgb_alloc(); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 430 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 431 | DEBUGP(DSMS, "TX: SMS RP ACK\n"); |
| 432 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 433 | return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ACK_MT, msg_ref); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 434 | } |
| 435 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 436 | static int gsm411_send_rp_error(struct gsm_trans *trans, |
| 437 | u_int8_t msg_ref, u_int8_t cause) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 438 | { |
| 439 | struct msgb *msg = gsm411_msgb_alloc(); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 440 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 441 | msgb_tv_put(msg, 1, cause); |
| 442 | |
| 443 | DEBUGP(DSMS, "TX: SMS RP ERROR (cause %02d)\n", cause); |
| 444 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 445 | return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_ERROR_MT, msg_ref); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* Receive a 04.11 TPDU inside RP-DATA / user data */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 449 | static int gsm411_rx_rp_ud(struct msgb *msg, struct gsm_trans *trans, |
| 450 | struct gsm411_rp_hdr *rph, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 451 | u_int8_t src_len, u_int8_t *src, |
| 452 | u_int8_t dst_len, u_int8_t *dst, |
| 453 | u_int8_t tpdu_len, u_int8_t *tpdu) |
| 454 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 455 | int rc = 0; |
| 456 | |
| 457 | if (src_len && src) |
| 458 | DEBUGP(DSMS, "RP-DATA (MO) with SRC ?!?\n"); |
| 459 | |
| 460 | if (!dst_len || !dst || !tpdu_len || !tpdu) { |
| 461 | DEBUGP(DSMS, "RP-DATA (MO) without DST or TPDU ?!?\n"); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 462 | gsm411_send_rp_error(trans, rph->msg_ref, |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 463 | GSM411_RP_CAUSE_INV_MAND_INF); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 464 | return -EIO; |
| 465 | } |
| 466 | msg->smsh = tpdu; |
| 467 | |
| 468 | DEBUGP(DSMS, "DST(%u,%s)\n", dst_len, hexdump(dst, dst_len)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 469 | |
| 470 | rc = gsm340_rx_tpdu(msg); |
| 471 | if (rc == 0) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 472 | return gsm411_send_rp_ack(trans, rph->msg_ref); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 473 | else if (rc > 0) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 474 | return gsm411_send_rp_error(trans, rph->msg_ref, rc); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 475 | else |
| 476 | return rc; |
| 477 | } |
| 478 | |
| 479 | /* Receive a 04.11 RP-DATA message in accordance with Section 7.3.1.2 */ |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 480 | static int gsm411_rx_rp_data(struct msgb *msg, struct gsm_trans *trans, |
| 481 | struct gsm411_rp_hdr *rph) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 482 | { |
| 483 | u_int8_t src_len, dst_len, rpud_len; |
| 484 | u_int8_t *src = NULL, *dst = NULL , *rp_ud = NULL; |
| 485 | |
| 486 | /* in the MO case, this should always be zero length */ |
| 487 | src_len = rph->data[0]; |
| 488 | if (src_len) |
| 489 | src = &rph->data[1]; |
| 490 | |
| 491 | dst_len = rph->data[1+src_len]; |
| 492 | if (dst_len) |
| 493 | dst = &rph->data[1+src_len+1]; |
| 494 | |
| 495 | rpud_len = rph->data[1+src_len+1+dst_len]; |
| 496 | if (rpud_len) |
| 497 | rp_ud = &rph->data[1+src_len+1+dst_len+1]; |
| 498 | |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 499 | DEBUGP(DSMS, "RX_RP-DATA: src_len=%u, dst_len=%u ud_len=%u\n", |
| 500 | src_len, dst_len, rpud_len); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 501 | return gsm411_rx_rp_ud(msg, trans, rph, src_len, src, dst_len, dst, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 502 | rpud_len, rp_ud); |
| 503 | } |
| 504 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 505 | static int gsm411_rx_rp_ack(struct msgb *msg, struct gsm_trans *trans, |
| 506 | struct gsm411_rp_hdr *rph) |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 507 | { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 508 | struct gsm_sms *sms = trans->sms.sms; |
| 509 | |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 510 | /* Acnkowledgement to MT RP_DATA, i.e. the MS confirms it |
| 511 | * successfully received a SMS. We can now safely mark it as |
| 512 | * transmitted */ |
| 513 | |
Harald Welte | dd62b16 | 2009-07-09 23:52:59 +0200 | [diff] [blame] | 514 | /* we need to look-up the transaction based on rph->msg_ref to |
| 515 | * identify which particular RP_DATA/SMS-submit was ACKed */ |
| 516 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 517 | if (!sms) { |
| 518 | DEBUGP(DSMS, "RX RP-ACK (MT) but no sms in transaction?!?\n"); |
| 519 | put_lchan(trans->lchan); |
| 520 | return -EIO; |
| 521 | } |
| 522 | |
| 523 | /* mark this SMS as sent in database */ |
| 524 | db_sms_mark_sent(sms); |
| 525 | |
| 526 | dispatch_signal(SS_SMS, S_SMS_DELIVERED, sms); |
| 527 | |
| 528 | sms_free(sms); |
| 529 | trans->sms.sms = NULL; |
| 530 | |
| 531 | put_lchan(trans->lchan); |
| 532 | |
| 533 | return 0; |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 536 | static int gsm411_rx_rp_error(struct msgb *msg, struct gsm_trans *trans, |
| 537 | struct gsm411_rp_hdr *rph) |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 538 | { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 539 | struct gsm_sms *sms = trans->sms.sms; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 540 | u_int8_t cause_len = rph->data[0]; |
| 541 | u_int8_t cause = rph->data[1]; |
| 542 | |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 543 | /* Error in response to MT RP_DATA, i.e. the MS did not |
| 544 | * successfully receive the SMS. We need to investigate |
| 545 | * the cause and take action depending on it */ |
| 546 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 547 | DEBUGP(DSMS, "RX SMS RP-ERROR Cause=0x%02x\n", cause); |
| 548 | |
Harald Welte | dd62b16 | 2009-07-09 23:52:59 +0200 | [diff] [blame] | 549 | /* we need to look-up the transaction based on rph->msg_ref to |
| 550 | * identify which particular RP_DATA/SMS-submit failed */ |
| 551 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 552 | if (!sms) { |
| 553 | DEBUGP(DSMS, "RX RP-ERR (MT) but no sms in transaction?!?\n"); |
| 554 | put_lchan(trans->lchan); |
| 555 | return -EIO; |
| 556 | } |
| 557 | |
| 558 | sms_free(sms); |
| 559 | trans->sms.sms = NULL; |
| 560 | |
| 561 | put_lchan(trans->lchan); |
| 562 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 563 | return 0; |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 566 | static int gsm411_rx_rp_smma(struct msgb *msg, struct gsm_trans *trans, |
| 567 | struct gsm411_rp_hdr *rph) |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 568 | { |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 569 | int rc; |
| 570 | |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 571 | /* MS tells us that it has memory for more SMS, we need |
| 572 | * to check if we have any pending messages for it and then |
| 573 | * transfer those */ |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 574 | dispatch_signal(SS_SMS, S_SMS_SMMA, trans->subscr); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 575 | |
| 576 | rc = gsm411_send_rp_ack(trans, rph->msg_ref); |
| 577 | trans->sms.rp_state = GSM411_RPS_IDLE; |
| 578 | |
| 579 | return rc; |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 580 | } |
| 581 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 582 | static int gsm411_rx_cp_data(struct msgb *msg, struct gsm48_hdr *gh, |
| 583 | struct gsm_trans *trans) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 584 | { |
| 585 | struct gsm411_rp_hdr *rp_data = (struct gsm411_rp_hdr*)&gh->data; |
| 586 | u_int8_t msg_type = rp_data->msg_type & 0x07; |
| 587 | int rc = 0; |
| 588 | |
| 589 | switch (msg_type) { |
| 590 | case GSM411_MT_RP_DATA_MO: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 591 | DEBUGP(DSMS, "RX SMS RP-DATA (MO)\n"); |
| 592 | /* start TR2N and enter 'wait to send RP-ACK state' */ |
| 593 | trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK; |
| 594 | rc = gsm411_rx_rp_data(msg, trans, rp_data); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 595 | break; |
| 596 | case GSM411_MT_RP_ACK_MO: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 597 | DEBUGP(DSMS,"RX SMS RP-ACK (MO)\n"); |
| 598 | rc = gsm411_rx_rp_ack(msg, trans, rp_data); |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 599 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 600 | case GSM411_MT_RP_SMMA_MO: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 601 | DEBUGP(DSMS, "RX SMS RP-SMMA\n"); |
| 602 | /* start TR2N and enter 'wait to send RP-ACK state' */ |
| 603 | trans->sms.rp_state = GSM411_RPS_WAIT_TO_TX_RP_ACK; |
| 604 | rc = gsm411_rx_rp_smma(msg, trans, rp_data); |
| 605 | break; |
| 606 | case GSM411_MT_RP_ERROR_MO: |
| 607 | rc = gsm411_rx_rp_error(msg, trans, rp_data); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 608 | break; |
| 609 | default: |
| 610 | DEBUGP(DSMS, "Invalid RP type 0x%02x\n", msg_type); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 611 | rc = gsm411_send_rp_error(trans, rp_data->msg_ref, |
| 612 | GSM411_RP_CAUSE_MSGTYPE_NOTEXIST); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 613 | break; |
| 614 | } |
| 615 | |
| 616 | return rc; |
| 617 | } |
| 618 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 619 | /* send CP-ACK to given transaction */ |
| 620 | static int gsm411_tx_cp_ack(struct gsm_trans *trans) |
| 621 | { |
| 622 | struct msgb *msg = gsm411_msgb_alloc(); |
| 623 | |
| 624 | return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ACK); |
| 625 | } |
| 626 | |
| 627 | static int gsm411_tx_cp_error(struct gsm_trans *trans, u_int8_t cause) |
| 628 | { |
| 629 | struct msgb *msg = gsm411_msgb_alloc(); |
| 630 | u_int8_t *causep; |
| 631 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 632 | causep = msgb_put(msg, 1); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 633 | *causep = cause; |
| 634 | |
| 635 | return gsm411_cp_sendmsg(msg, trans, GSM411_MT_CP_ERROR); |
| 636 | } |
| 637 | |
| 638 | /* Entry point for incoming GSM48_PDISC_SMS from abis_rsl.c */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 639 | int gsm0411_rcv_sms(struct msgb *msg) |
| 640 | { |
| 641 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 642 | u_int8_t msg_type = gh->msg_type; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 643 | u_int8_t transaction_id = ((gh->proto_discr >> 4) ^ 0x8); /* flip */ |
| 644 | struct gsm_lchan *lchan = msg->lchan; |
| 645 | struct gsm_trans *trans; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 646 | int rc = 0; |
| 647 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 648 | if (!lchan->subscr) |
| 649 | return -EIO; |
| 650 | /* FIXME: send some error message */ |
| 651 | |
| 652 | trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_SMS, |
| 653 | transaction_id); |
| 654 | if (!trans) { |
| 655 | DEBUGP(DSMS, "Unknown transaction ID %x, " |
| 656 | "creating new trans\n", transaction_id); |
| 657 | trans = trans_alloc(lchan->subscr, GSM48_PDISC_SMS, |
| 658 | transaction_id, new_callref++); |
| 659 | if (!trans) { |
| 660 | DEBUGP(DSMS, "No memory for trans\n"); |
| 661 | /* FIXME: send some error message */ |
| 662 | return -ENOMEM; |
| 663 | } |
| 664 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 665 | trans->sms.rp_state = GSM411_RPS_IDLE; |
| 666 | trans->sms.is_mt = 0; |
| 667 | |
| 668 | trans->lchan = lchan; |
| 669 | use_lchan(lchan); |
| 670 | } |
| 671 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 672 | switch(msg_type) { |
| 673 | case GSM411_MT_CP_DATA: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 674 | DEBUGP(DSMS, "RX SMS CP-DATA\n"); |
| 675 | if (!trans->sms.is_mt) { |
| 676 | /* 5.2.3.1.3: MO state exists when SMC has received |
| 677 | * CP-DATA, including sending of the assoc. CP-ACK */ |
| 678 | trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED; |
| 679 | } |
| 680 | |
| 681 | rc = gsm411_rx_cp_data(msg, gh, trans); |
| 682 | /* Send CP-ACK or CP-ERORR in response */ |
| 683 | if (rc < 0) { |
| 684 | rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_NET_FAIL); |
| 685 | } else |
| 686 | rc = gsm411_tx_cp_ack(trans); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 687 | break; |
| 688 | case GSM411_MT_CP_ACK: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 689 | /* previous CP-DATA in this transaction was confirmed */ |
| 690 | DEBUGP(DSMS, "RX SMS CP-ACK\n"); |
| 691 | if (!trans->sms.is_mt) { |
| 692 | /* 5.2.3.1.3: MO state exists when SMC has received |
| 693 | * CP-ACK */ |
| 694 | trans->sms.cp_state = GSM411_CPS_MM_ESTABLISHED; |
| 695 | /* FIXME: we have sont one CP-DATA, which was now |
| 696 | * acknowledged. Check if we want to transfer more, |
| 697 | * i.e. multi-part message */ |
| 698 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 699 | trans_free(trans); |
| 700 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 701 | break; |
| 702 | case GSM411_MT_CP_ERROR: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 703 | DEBUGP(DSMS, "RX SMS CP-ERROR, cause 0x%02x\n", gh->data[0]); |
| 704 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 705 | trans_free(trans); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 706 | break; |
| 707 | default: |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 708 | DEBUGP(DSMS, "RX Unimplemented CP msg_type: 0x%02x\n", msg_type); |
| 709 | rc = gsm411_tx_cp_error(trans, GSM411_CP_CAUSE_MSGTYPE_NOTEXIST); |
| 710 | trans_free(trans); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 711 | break; |
| 712 | } |
| 713 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 714 | return rc; |
| 715 | } |
| 716 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 717 | #if 0 |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 718 | /* Test TPDU - ALL YOUR */ |
| 719 | static u_int8_t tpdu_test[] = { |
| 720 | 0x04, 0x04, 0x81, 0x32, 0x24, 0x00, 0x00, 0x80, 0x21, 0x03, 0x41, 0x24, |
| 721 | 0x32, 0x40, 0x1F, 0x41, 0x26, 0x13, 0x94, 0x7D, 0x56, 0xA5, 0x20, 0x28, |
| 722 | 0xF2, 0xE9, 0x2C, 0x82, 0x82, 0xD2, 0x22, 0x48, 0x58, 0x64, 0x3E, 0x9D, |
| 723 | 0x47, 0x10, 0xF5, 0x09, 0xAA, 0x4E, 0x01 |
| 724 | }; |
| 725 | #endif |
| 726 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 727 | /* Take a SMS in gsm_sms structure and send it through lchan */ |
| 728 | int gsm411_send_sms_lchan(struct gsm_lchan *lchan, struct gsm_sms *sms) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 729 | { |
| 730 | struct msgb *msg = gsm411_msgb_alloc(); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 731 | struct gsm_trans *trans; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 732 | u_int8_t *data, *rp_ud_len; |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 733 | u_int8_t msg_ref = 42; |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 734 | u_int8_t transaction_id = 1; /* FIXME: random */ |
| 735 | int rc; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 736 | |
| 737 | msg->lchan = lchan; |
| 738 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 739 | DEBUGP(DSMS, "send_sms_lchan()\n"); |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 740 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 741 | /* FIXME: allocate transaction with message reference */ |
| 742 | trans = trans_alloc(lchan->subscr, GSM48_PDISC_SMS, |
| 743 | transaction_id, new_callref++); |
| 744 | if (!trans) { |
| 745 | DEBUGP(DSMS, "No memory for trans\n"); |
| 746 | /* FIXME: send some error message */ |
| 747 | return -ENOMEM; |
| 748 | } |
| 749 | trans->sms.cp_state = GSM411_CPS_IDLE; |
| 750 | trans->sms.rp_state = GSM411_RPS_IDLE; |
| 751 | trans->sms.is_mt = 1; |
| 752 | trans->sms.sms = sms; |
| 753 | |
| 754 | trans->lchan = lchan; |
| 755 | use_lchan(lchan); |
| 756 | |
| 757 | /* Hardcode SMSC Originating Address for now */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 758 | data = (u_int8_t *)msgb_put(msg, 8); |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 759 | data[0] = 0x07; /* originator length == 7 */ |
Harald Welte | 156c5e6 | 2009-07-05 14:02:46 +0200 | [diff] [blame] | 760 | data[1] = 0x91; /* type of number: international, ISDN */ |
| 761 | data[2] = 0x44; /* 447785016005 */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 762 | data[3] = 0x77; |
| 763 | data[4] = 0x58; |
| 764 | data[5] = 0x10; |
| 765 | data[6] = 0x06; |
| 766 | data[7] = 0x50; |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 767 | |
| 768 | /* Hardcoded Destination Address */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 769 | data = (u_int8_t *)msgb_put(msg, 1); |
Harald Welte | 7e2f57d | 2009-07-04 17:39:00 +0200 | [diff] [blame] | 770 | data[0] = 0; /* destination length == 0 */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 771 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 772 | /* obtain a pointer for the rp_ud_len, so we can fill it later */ |
| 773 | rp_ud_len = (u_int8_t *)msgb_put(msg, 1); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 774 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 775 | #if 1 |
| 776 | /* generate the 03.40 TPDU */ |
| 777 | rc = gsm340_gen_tpdu(msg, sms); |
| 778 | if (rc < 0) { |
| 779 | msgb_free(msg); |
| 780 | return rc; |
| 781 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 782 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 783 | *rp_ud_len = rc; |
| 784 | #else |
| 785 | data = msgb_put(msg, sizeof(tpdu_test)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 786 | memcpy(data, tpdu_test, sizeof(tpdu_test)); |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 787 | *rp_ud_len = sizeof(tpdu_test); |
| 788 | #endif |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 789 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 790 | DEBUGP(DSMS, "TX: SMS DELIVER\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 791 | |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 792 | return gsm411_rp_sendmsg(msg, trans, GSM411_MT_RP_DATA_MT, msg_ref); |
| 793 | /* FIXME: enter 'wait for RP-ACK' state, start TR1N */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 794 | } |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 795 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 796 | /* paging callback */ |
| 797 | static int paging_cb_send_sms(unsigned int hooknum, unsigned int event, |
| 798 | struct msgb *msg, void *_lchan, void *_sms) |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 799 | { |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 800 | struct gsm_lchan *lchan = _lchan; |
| 801 | struct gsm_sms *sms = _sms; |
| 802 | int rc; |
Harald Welte | b78996d | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 803 | |
Harald Welte | 68b7df2 | 2009-08-08 16:03:15 +0200 | [diff] [blame^] | 804 | DEBUGP(DSMS, "paging_cb_send_sms(hooknum=%u, event=%u, msg=%p," |
| 805 | "lchan=%p, sms=%p)\n", hooknum, event, msg, lchan, sms); |
| 806 | |
| 807 | if (hooknum != GSM_HOOK_RR_PAGING) |
| 808 | return -EINVAL; |
| 809 | |
| 810 | switch (event) { |
| 811 | case GSM_PAGING_SUCCEEDED: |
| 812 | /* Paging aborted without lchan ?!? */ |
| 813 | if (!lchan) { |
| 814 | sms_free(sms); |
| 815 | rc = -EIO; |
| 816 | break; |
| 817 | } |
| 818 | rc = gsm411_send_sms_lchan(lchan, sms); |
| 819 | break; |
| 820 | case GSM_PAGING_EXPIRED: |
| 821 | sms_free(sms); |
| 822 | rc = -ETIMEDOUT; |
| 823 | break; |
| 824 | default: |
| 825 | rc = -EINVAL; |
| 826 | break; |
| 827 | } |
| 828 | |
| 829 | return rc; |
| 830 | } |
| 831 | |
| 832 | int gsm411_send_sms_subscr(struct gsm_subscriber *subscr, |
| 833 | struct gsm_sms *sms) |
| 834 | { |
| 835 | /* check if we already have an open lchan to the subscriber. |
| 836 | * if yes, send the SMS this way */ |
| 837 | //if (subscr->lchan) |
| 838 | //return gsm411_send_sms_lchan(subscr->lchan, sms); |
| 839 | |
| 840 | /* if not, we have to start paging */ |
| 841 | paging_request(subscr->net, subscr, RSL_CHANNEED_SDCCH, |
| 842 | paging_cb_send_sms, sms); |
| 843 | |
| 844 | return 0; |
| 845 | } |
Harald Welte | 5b359d8 | 2009-07-28 00:44:49 +0200 | [diff] [blame] | 846 | |
Harald Welte | 932e20d | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 847 | static __attribute__((constructor)) void on_dso_load_sms(void) |
| 848 | { |
Harald Welte | 932e20d | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 849 | tall_gsms_ctx = talloc_named_const(tall_bsc_ctx, 1, "sms"); |
| 850 | } |