Index: gw/bb_smscconn.c
===================================================================
--- gw/bb_smscconn.c	(revision 5010)
+++ gw/bb_smscconn.c	(working copy)
@@ -93,6 +93,7 @@
 #include "smscconn_p.h"        /* to access counters */
 
 #include "smsc/smpp_pdu.h"     /* access smpp_pdu_init/smpp_pdu_shutdown */
+#include "meta_data.h"
 
 /* passed from bearerbox core */
 
@@ -270,6 +271,7 @@
 
 void bb_smscconn_sent(SMSCConn *conn, Msg *sms, Octstr *reply)
 {
+    Dict *smpp_tlvs;
     if (sms->sms.split_parts != NULL) {
         handle_split(conn, sms, SMSCCONN_SUCCESS);
         octstr_destroy(reply);
@@ -308,6 +310,16 @@
         dlrmsg = create_dlr_from_msg((conn->id?conn->id:conn->name), sms,
 	                reply, DLR_SMSC_SUCCESS);
         if (dlrmsg != NULL) {
+            if(sms->sms.meta_data != NULL) {
+                smpp_tlvs = meta_data_get_values(sms->sms.meta_data, "smpp");
+                if(smpp_tlvs != NULL) {
+                    if(dlrmsg->sms.meta_data == NULL) {
+                        dlrmsg->sms.meta_data = octstr_create("");
+                    }
+                    meta_data_set_values(dlrmsg->sms.meta_data, smpp_tlvs, "smpp", 0);
+                }
+                dict_destroy(smpp_tlvs);
+            }
             bb_smscconn_receive(conn, dlrmsg);
         }
     }
@@ -319,6 +331,7 @@
 
 void bb_smscconn_send_failed(SMSCConn *conn, Msg *sms, int reason, Octstr *reply)
 {
+    Dict *smpp_tlvs;
     if (sms->sms.split_parts != NULL) {
         handle_split(conn, sms, reason);
         octstr_destroy(reply);
@@ -381,6 +394,16 @@
             dlrmsg = create_dlr_from_msg((conn ? (conn->id?conn->id:conn->name) : NULL), sms,
 	                                 reply, DLR_SMSC_FAIL);
             if (dlrmsg != NULL) {
+                if(sms->sms.meta_data != NULL) {
+                    smpp_tlvs = meta_data_get_values(sms->sms.meta_data, "smpp");
+                    if(smpp_tlvs != NULL) {
+                        if(dlrmsg->sms.meta_data == NULL) {
+                            dlrmsg->sms.meta_data = octstr_create("");
+                        }
+                        meta_data_set_values(dlrmsg->sms.meta_data, smpp_tlvs, "smpp", 0);
+                    }
+                    dict_destroy(smpp_tlvs);
+                }
                 bb_smscconn_receive(conn, dlrmsg);
             }
         }
Index: gw/smsc/smpp_pdu.h
===================================================================
--- gw/smsc/smpp_pdu.h	(revision 5010)
+++ gw/smsc/smpp_pdu.h	(working copy)
@@ -226,6 +226,8 @@
     SMPP_ESME_RINVBCASTCHANIND = 0x00000112,
 };
 
+#define SMPP_ERROR_CODE "smpp_error_code"
+
 /* initialize SMPP PDU */
 int smpp_pdu_init(Cfg *cfg);
 /* shutdown SMPP PDU */
Index: gw/smsc/smsc_smpp.c
===================================================================
@@ -1532,7 +1540,7 @@
                        long *pending_submits)
 {
     SMPP_PDU *resp = NULL;
-    Octstr *os;
+    Octstr *os, *smpp_error_code;
     Msg *msg = NULL, *dlrmsg=NULL;
     struct smpp_msg *smpp_msg = NULL;
     long reason, cmd_stat;
@@ -1680,6 +1688,15 @@
             }
             msg = smpp_msg->msg;
             smpp_msg_destroy(smpp_msg, 0);
+
+            if(msg->sms.meta_data != NULL) {
+                octstr_destroy(msg->sms.meta_data);
+            }
+            msg->sms.meta_data = octstr_create("");
+            smpp_error_code = octstr_format("%lu", pdu->u.submit_sm_resp.command_status);
+            meta_data_set_value(msg->sms.meta_data, "smpp", octstr_imm(SMPP_ERROR_CODE), smpp_error_code, 1);
+            octstr_destroy(smpp_error_code);
+
             if (pdu->u.submit_sm_resp.command_status != 0) {
                 error(0, "SMPP[%s]: SMSC returned error code 0x%08lx (%s) "
                       "in response to submit_sm.",

