blob: dd6a2f9e3b29b67682e8ad92060a39176847a0db [file] [log] [blame]
Harald Weltef1033cc2012-11-08 16:14:37 +01001/* SMPP 3.4 interface, SMSC-side implementation */
2/* (C) 2012 by Harald Welte <laforge@gnumonks.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20#include <stdio.h>
21#include <unistd.h>
22#include <string.h>
23#include <stdint.h>
24#include <errno.h>
25
26#include <sys/socket.h>
27#include <netinet/in.h>
28
29#include <smpp34.h>
30#include <smpp34_structs.h>
31#include <smpp34_params.h>
32
33#include <osmocom/core/utils.h>
34#include <osmocom/core/socket.h>
35#include <osmocom/core/msgb.h>
36#include <osmocom/core/logging.h>
37#include <osmocom/core/write_queue.h>
38#include <osmocom/core/talloc.h>
39
40#include "smpp_smsc.h"
41
42#include <openbsc/debug.h>
43
44enum emse_bind {
45 ESME_BIND_RX = 0x01,
46 ESME_BIND_TX = 0x02,
47};
48
Harald Welted4bdee72012-11-08 19:44:08 +010049static struct osmo_esme *
50esme_by_system_id(const struct smsc *smsc, char *system_id)
51{
52 struct osmo_esme *e;
53
54 llist_for_each_entry(e, &smsc->esme_list, list) {
55 if (!strcmp(e->system_id, system_id))
56 return e;
57 }
58 return NULL;
59}
Harald Weltef1033cc2012-11-08 16:14:37 +010060
61
62#define INIT_RESP(type, resp, req) { \
63 memset((resp), 0, sizeof(*(resp))); \
64 (resp)->command_length = 0; \
65 (resp)->command_id = type; \
66 (resp)->command_status = ESME_ROK; \
67 (resp)->sequence_number = (req)->sequence_number; \
68}
69
70#define PACK_AND_SEND(esme, ptr) pack_and_send(esme, (ptr)->command_id, ptr)
71static int pack_and_send(struct osmo_esme *esme, uint32_t type, void *ptr)
72{
73 struct msgb *msg = msgb_alloc(4096, "SMPP_Tx");
74 int rc, rlen;
75 if (!msg)
76 return -ENOMEM;
77
78 rc = smpp34_pack(type, msg->tail, msgb_tailroom(msg), &rlen, ptr);
79 if (rc != 0) {
80 LOGP(DSMPP, LOGL_ERROR, "Error during smpp34_pack(): %s\n",
81 smpp34_strerror);
82 msgb_free(msg);
83 return -EINVAL;
84 }
85 msgb_put(msg, rlen);
86
87 return osmo_wqueue_enqueue(&esme->wqueue, msg);
88}
89
90static int smpp_tx_gen_nack(struct osmo_esme *esme, uint32_t seq, uint32_t status)
91{
92 struct generic_nack_t nack;
93
94 nack.command_length = 0;
95 nack.command_id = GENERIC_NACK;
96 nack.sequence_number = seq;
97 nack.command_status = status;
98
99 return PACK_AND_SEND(esme, &nack);
100}
101
102static inline uint32_t smpp_msgb_cmdid(struct msgb *msg)
103{
104 uint8_t *tmp = msgb_data(msg) + 4;
105 return ntohl(*(uint32_t *)tmp);
106}
107
108static inline uint32_t smpp_msgb_seq(struct msgb *msg)
109{
110 uint8_t *tmp = msgb_data(msg);
111 return ntohl(*(uint32_t *)tmp);
112}
113
114
115static int smpp_handle_gen_nack(struct osmo_esme *esme, struct msgb *msg)
116{
117 struct generic_nack_t nack;
118 char buf[SMALL_BUFF];
119 int rc;
120
121 rc = smpp34_unpack(GENERIC_NACK, &nack, msgb_data(msg),
122 msgb_length(msg));
123 if (rc < 0)
124 return rc;
125
126 LOGP(DSMPP, LOGL_ERROR, "%s: GENERIC NACK: %s\n", esme->system_id,
127 str_command_status(nack.command_status, buf));
128
129 return 0;
130}
131
132static int smpp_handle_bind_rx(struct osmo_esme *esme, struct msgb *msg)
133{
134 struct bind_receiver_t bind;
135 struct bind_receiver_resp_t bind_r;
136 int rc;
137
138 rc = smpp34_unpack(BIND_RECEIVER, &bind, msgb_data(msg),
139 msgb_length(msg));
140 if (rc < 0)
141 return rc;
142
143 INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind);
144
145 LOGP(DSMPP, LOGL_INFO, "%s: BIND Rx from (Version %02x)\n",
146 bind.system_id, bind.interface_version);
147
148 if (bind.interface_version != SMPP_VERSION) {
149 bind_r.command_status = ESME_RSYSERR;
150 goto err;
151 }
152
153 if (esme->bind_flags) {
154 bind_r.command_status = ESME_RALYBND;
155 goto err;
156 }
157
158 esme->smpp_version = bind.interface_version;
159 snprintf(esme->system_id, sizeof(esme->system_id), "%s",
160 bind.system_id);
161 esme->bind_flags = ESME_BIND_RX;
162
163 /* FIXME */
164err:
165 return 0;
166}
167
168static int smpp_handle_bind_tx(struct osmo_esme *esme, struct msgb *msg)
169{
170 struct bind_transmitter_t bind;
171 struct bind_transmitter_resp_t bind_r;
172 struct tlv_t tlv;
173 int rc;
174
175 rc = smpp34_unpack(BIND_TRANSMITTER, &bind, msgb_data(msg),
176 msgb_length(msg));
177 if (rc < 0) {
178 printf("error during unpack: %s\n", smpp34_strerror);
179 return rc;
180 }
181
182 INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind);
183
184 LOGP(DSMPP, LOGL_INFO, "%s: BIND Tx (Version %02x)\n",
185 bind.system_id, bind.interface_version);
186
187 if (bind.interface_version != SMPP_VERSION) {
188 bind_r.command_status = ESME_RSYSERR;
189 goto err;
190 }
191
192 if (esme->bind_flags) {
193 bind_r.command_status = ESME_RALYBND;
194 goto err;
195 }
196
197 esme->smpp_version = bind.interface_version;
198 snprintf(esme->system_id, sizeof(esme->system_id), "%s", bind.system_id);
199 esme->bind_flags = ESME_BIND_TX;
200
201 /* build response */
202 snprintf((char *)bind_r.system_id, sizeof(bind_r.system_id), "%s",
203 esme->smsc->system_id);
204
205 /* add interface version TLV */
206 tlv.tag = TLVID_sc_interface_version;
207 tlv.length = sizeof(uint8_t);
208 tlv.value.val16 = esme->smpp_version;
209 build_tlv(&bind_r.tlv, &tlv);
210
211err:
212 return PACK_AND_SEND(esme, &bind_r);
213}
214
215static int smpp_handle_bind_trx(struct osmo_esme *esme, struct msgb *msg)
216{
217 struct bind_transceiver_t bind;
218 struct bind_transceiver_resp_t bind_r;
219 int rc;
220
221 rc = smpp34_unpack(BIND_TRANSCEIVER, &bind, msgb_data(msg),
222 msgb_length(msg));
223 if (rc < 0)
224 return rc;
225
226 INIT_RESP(BIND_TRANSMITTER_RESP, &bind_r, &bind);
227
228 LOGP(DSMPP, LOGL_INFO, "%s: BIND Trx (Version %02x)\n",
229 bind.system_id, bind.interface_version);
230
231 if (bind.interface_version != SMPP_VERSION) {
232 bind_r.command_status = ESME_RSYSERR;
233 goto err;
234 }
235
236 if (esme->bind_flags) {
237 bind_r.command_status = ESME_RALYBND;
238 goto err;
239 }
240
241 esme->smpp_version = bind.interface_version;
242 snprintf(esme->system_id, sizeof(esme->system_id), "%s", bind.system_id);
243 esme->bind_flags |= ESME_BIND_TX | ESME_BIND_RX;
244
245 /* FIXME */
246err:
247 return 0;
248}
249
250static int smpp_handle_unbind(struct osmo_esme *esme, struct msgb *msg)
251{
252 struct unbind_t unbind;
253 struct unbind_resp_t unbind_r;
254 int rc;
255
256 rc = smpp34_unpack(UNBIND, &unbind, msgb_data(msg),
257 msgb_length(msg));
258 if (rc < 0)
259 return rc;
260
261 INIT_RESP(UNBIND_RESP, &unbind_r, &unbind);
262
263 LOGP(DSMPP, LOGL_INFO, "%s: UNBIND\n", esme->system_id);
264
265 if (esme->bind_flags == 0) {
266 unbind_r.command_status = ESME_RINVBNDSTS;
267 goto err;
268 }
269
270 esme->bind_flags = 0;
271err:
272 return PACK_AND_SEND(esme, &unbind_r);
273}
274
275
276static int smpp_handle_enq_link(struct osmo_esme *esme, struct msgb *msg)
277{
278 struct enquire_link_t enq;
279 struct enquire_link_resp_t enq_r;
280 int rc;
281
282 rc = smpp34_unpack(ENQUIRE_LINK, &enq, msgb_data(msg),
283 msgb_length(msg));
284 if (rc < 0)
285 return rc;
286
287 LOGP(DSMPP, LOGL_DEBUG, "%s: Enquire Link\n", esme->system_id);
288
289 INIT_RESP(ENQUIRE_LINK_RESP, &enq_r, &enq);
290
291 return PACK_AND_SEND(esme, &enq_r);
292}
293
Harald Welted4bdee72012-11-08 19:44:08 +0100294int smpp_tx_submit_r(struct osmo_esme *esme, uint32_t sequence_nr,
295 uint32_t command_status, char *msg_id)
296{
297 struct submit_sm_resp_t submit_r;
298
299 memset(&submit_r, 0, sizeof(submit_r));
300 submit_r.command_length = 0;
301 submit_r.command_id = SUBMIT_SM_RESP;
302 submit_r.command_status = command_status;
303 submit_r.sequence_number= sequence_nr;
304 snprintf(submit_r.message_id, sizeof(submit_r.message_id), "%s", msg_id);
305
306 return PACK_AND_SEND(esme, &submit_r);
307}
308
Harald Weltef1033cc2012-11-08 16:14:37 +0100309static int smpp_handle_submit(struct osmo_esme *esme, struct msgb *msg)
310{
311 struct submit_sm_t submit;
312 struct submit_sm_resp_t submit_r;
313 int rc;
314
315 rc = smpp34_unpack(SUBMIT_SM, &submit, msgb_data(msg),
316 msgb_length(msg));
317 if (rc < 0)
318 return rc;
319
320 INIT_RESP(SUBMIT_SM_RESP, &submit_r, &submit);
321
322 if (!(esme->bind_flags & ESME_BIND_TX)) {
323 submit_r.command_status = ESME_RINVBNDSTS;
324 return PACK_AND_SEND(esme, &submit_r);
325 }
326
327 LOGP(DSMPP, LOGL_INFO, "%s: SUBMIT-SM(%s)\n", esme->system_id,
328 submit.service_type);
329
330 INIT_RESP(SUBMIT_SM_RESP, &submit_r, &submit);
331
332 rc = handle_smpp_submit(esme, &submit, &submit_r);
333 if (rc == 0)
334 return PACK_AND_SEND(esme, &submit_r);
335
336 return rc;
337}
338
339/* one complete SMPP PDU from the ESME has been received */
340static int smpp_pdu_rx(struct osmo_esme *esme, struct msgb *msg)
341{
342 uint32_t cmd_id = smpp_msgb_cmdid(msg);
343 int rc = 0;
344
345 LOGP(DSMPP, LOGL_DEBUG, "%s: smpp_pdu_rx(%s)\n", esme->system_id,
346 osmo_hexdump(msgb_data(msg), msgb_length(msg)));
347
348 switch (cmd_id) {
349 case GENERIC_NACK:
350 rc = smpp_handle_gen_nack(esme, msg);
351 break;
352 case BIND_RECEIVER:
353 rc = smpp_handle_bind_rx(esme, msg);
354 break;
355 case BIND_TRANSMITTER:
356 rc = smpp_handle_bind_tx(esme, msg);
357 break;
358 case BIND_TRANSCEIVER:
359 rc = smpp_handle_bind_trx(esme, msg);
360 break;
361 case UNBIND:
362 rc = smpp_handle_unbind(esme, msg);
363 break;
364 case ENQUIRE_LINK:
365 rc = smpp_handle_enq_link(esme, msg);
366 break;
367 case SUBMIT_SM:
368 rc = smpp_handle_submit(esme, msg);
369 break;
370 case DELIVER_SM:
371 break;
372 case DATA_SM:
373 break;
374 case CANCEL_SM:
375 case QUERY_SM:
376 case REPLACE_SM:
377 case SUBMIT_MULTI:
378 LOGP(DSMPP, LOGL_NOTICE, "%s: Unimplemented PDU Commmand "
379 "0x%08x\n", esme->system_id, cmd_id);
380 break;
381 default:
382 LOGP(DSMPP, LOGL_ERROR, "%s: Unknown PDU Command 0x%08x\n",
383 esme->system_id, cmd_id);
384 rc = smpp_tx_gen_nack(esme, smpp_msgb_seq(msg), ESME_RINVCMDID);
385 break;
386 }
387
388 return rc;
389}
390
391static void esme_destroy(struct osmo_esme *esme)
392{
393 osmo_wqueue_clear(&esme->wqueue);
394 osmo_fd_unregister(&esme->wqueue.bfd);
395 close(esme->wqueue.bfd.fd);
396 llist_del(&esme->list);
397 talloc_free(esme);
398}
399
400/* call-back when per-ESME TCP socket has some data to be read */
401static int esme_link_read_cb(struct osmo_fd *ofd)
402{
403 struct osmo_esme *esme = ofd->data;
404 uint32_t len;
405 uint8_t *lenptr = (uint8_t *) &len;
406 uint8_t *cur;
407 struct msgb *msg;
408 int rdlen;
409 int rc;
410
411 switch (esme->read_state) {
412 case READ_ST_IN_LEN:
413 rdlen = sizeof(uint32_t) - esme->read_idx;
414 rc = read(ofd->fd, lenptr + esme->read_idx, rdlen);
415 if (rc < 0) {
416 LOGP(DSMPP, LOGL_ERROR, "read returned %d\n", rc);
417 } else if (rc == 0) {
418 goto dead_socket;
419 } else
420 esme->read_idx += rc;
421 if (esme->read_idx >= sizeof(uint32_t)) {
422 esme->read_len = ntohl(len);
423 msg = msgb_alloc(esme->read_len, "SMPP Rx");
424 if (!msg)
425 return -ENOMEM;
426 esme->read_msg = msg;
427 cur = msgb_put(msg, sizeof(uint32_t));
428 memcpy(cur, lenptr, sizeof(uint32_t));
429 esme->read_state = READ_ST_IN_MSG;
430 esme->read_idx = sizeof(uint32_t);
431 }
432 break;
433 case READ_ST_IN_MSG:
434 msg = esme->read_msg;
435 rdlen = esme->read_len - esme->read_idx;
436 rc = read(ofd->fd, msg->tail, OSMO_MIN(rdlen, msgb_tailroom(msg)));
437 if (rc < 0) {
438 LOGP(DSMPP, LOGL_ERROR, "read returned %d\n", rc);
439 } else if (rc == 0) {
440 goto dead_socket;
441 } else {
442 esme->read_idx += rc;
443 msgb_put(msg, rc);
444 }
445
446 if (esme->read_idx >= esme->read_len) {
447 rc = smpp_pdu_rx(esme, esme->read_msg);
448 esme->read_msg = NULL;
449 esme->read_idx = 0;
450 esme->read_len = 0;
451 esme->read_state = READ_ST_IN_LEN;
452 }
453 break;
454 }
455
456 return 0;
457dead_socket:
458 msgb_free(esme->read_msg);
459 esme_destroy(esme);
460
461 return 0;
462}
463
464/* call-back of write queue once it wishes to write a message to the socket */
465static void esme_link_write_cb(struct osmo_fd *ofd, struct msgb *msg)
466{
467 struct osmo_esme *esme = ofd->data;
468 int rc;
469
470 rc = write(ofd->fd, msgb_data(msg), msgb_length(msg));
471 if (rc == 0) {
472 esme_destroy(esme);
473 } else if (rc < msgb_length(msg)) {
474 LOGP(DSMPP, LOGL_ERROR, "%s: Short write\n", esme->system_id);
475 return;
476 }
477}
478
479/* callback for already-accepted new TCP socket */
480static int link_accept_cb(struct smsc *smsc, int fd,
481 struct sockaddr_storage *s, socklen_t s_len)
482{
483 struct osmo_esme *esme = talloc_zero(smsc, struct osmo_esme);
484 if (!esme)
485 return -ENOMEM;
486
487 esme->smsc = smsc;
488 osmo_wqueue_init(&esme->wqueue, 10);
489 esme->wqueue.bfd.fd = fd;
490 esme->wqueue.bfd.data = esme;
491 esme->wqueue.bfd.when = BSC_FD_READ;
492 osmo_fd_register(&esme->wqueue.bfd);
493
494 esme->wqueue.read_cb = esme_link_read_cb;
495 esme->wqueue.write_cb = esme_link_write_cb;
496
497 esme->sa_len = OSMO_MIN(sizeof(esme->sa), s_len);
498 memcpy(&esme->sa, s, esme->sa_len);
499
500 llist_add_tail(&esme->list, &smsc->esme_list);
501
502 return 0;
503}
504
505/* callback of listening TCP socket */
506static int smsc_fd_cb(struct osmo_fd *ofd, unsigned int what)
507{
508 int rc;
509 struct sockaddr_storage sa;
510 socklen_t sa_len = sizeof(sa);
511
512 rc = accept(ofd->fd, (struct sockaddr *)&sa, &sa_len);
513 if (rc < 0) {
514 LOGP(DSMPP, LOGL_ERROR, "Accept returns %d (%s)\n",
515 rc, strerror(errno));
516 return rc;
517 }
518 return link_accept_cb(ofd->data, rc, &sa, sa_len);
519}
520
521int smpp_smsc_init(struct smsc *smsc, uint16_t port)
522{
523 int rc;
524
525 INIT_LLIST_HEAD(&smsc->esme_list);
526 smsc->listen_ofd.data = smsc;
527 smsc->listen_ofd.cb = smsc_fd_cb;
528 rc = osmo_sock_init_ofd(&smsc->listen_ofd, AF_UNSPEC, SOCK_STREAM,
529 IPPROTO_TCP, NULL, port, OSMO_SOCK_F_BIND);
530
531 return rc;
532}