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