Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 1 | /* SCCP User SAP helper functions (move to libosmo-sigtran?) */ |
| 2 | |
| 3 | /* (C) 2015 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <osmocom/sigtran/sccp_sap.h> |
| 24 | #include <osmocom/sigtran/sua.h> |
| 25 | |
| 26 | #include "sccp_helpers.h" |
| 27 | |
| 28 | int sccp_tx_unitdata(struct osmo_sua_link *link, |
| 29 | const struct osmo_sccp_addr *calling_addr, |
| 30 | const struct osmo_sccp_addr *called_addr, |
| 31 | uint8_t *data, unsigned int len) |
| 32 | { |
| 33 | struct msgb *msg = msgb_alloc(1024, "sccp_tx_unitdata"); |
| 34 | struct osmo_scu_prim *prim; |
| 35 | struct osmo_scu_unitdata_param *param; |
| 36 | |
| 37 | prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); |
| 38 | param = &prim->u.unitdata; |
| 39 | param->calling_addr.presence = OSMO_SCCP_ADDR_T_SSN; |
| 40 | param->called_addr.presence = OSMO_SCCP_ADDR_T_SSN; |
| 41 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_REQUEST, msg); |
| 42 | |
| 43 | msg->l2h = msgb_put(msg, len); |
| 44 | memcpy(msg->l2h, data, len); |
| 45 | |
| 46 | return osmo_sua_user_link_down(link, &prim->oph); |
| 47 | } |
| 48 | |
| 49 | int sccp_tx_unitdata_msg(struct osmo_sua_link *link, |
| 50 | const struct osmo_sccp_addr *calling_addr, |
| 51 | const struct osmo_sccp_addr *called_addr, |
| 52 | struct msgb *msg) |
| 53 | { |
| 54 | int rc; |
| 55 | |
| 56 | rc = sccp_tx_unitdata(link, calling_addr, called_addr, |
| 57 | msg->data, msgb_length(msg)); |
| 58 | msgb_free(msg); |
| 59 | |
| 60 | return rc; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | #define SSN_RANAP 142 |
| 65 | void sccp_make_addr_pc_ssn(struct osmo_sccp_addr *addr, uint32_t pc, uint32_t ssn) |
| 66 | { |
| 67 | addr->presence = OSMO_SCCP_ADDR_T_SSN | OSMO_SCCP_ADDR_T_PC; |
| 68 | addr->ssn = ssn; |
| 69 | addr->pc = pc; |
| 70 | } |
| 71 | |
| 72 | int sccp_tx_conn_req(struct osmo_sua_link *link, uint32_t conn_id, |
| 73 | const struct osmo_sccp_addr *calling_addr, |
| 74 | const struct osmo_sccp_addr *called_addr, |
| 75 | uint8_t *data, unsigned int len) |
| 76 | { |
| 77 | struct msgb *msg = msgb_alloc(1024, "sccp_tx_conn_req"); |
| 78 | struct osmo_scu_prim *prim; |
| 79 | |
| 80 | prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); |
| 81 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 82 | OSMO_SCU_PRIM_N_CONNECT, |
| 83 | PRIM_OP_REQUEST, msg); |
| 84 | sccp_make_addr_pc_ssn(&prim->u.connect.calling_addr, 1, SSN_RANAP); |
| 85 | prim->u.connect.sccp_class = 2; |
| 86 | prim->u.connect.conn_id = conn_id; |
| 87 | |
| 88 | if (data && len) { |
| 89 | msg->l2h = msgb_put(msg, len); |
| 90 | memcpy(msg->l2h, data, len); |
| 91 | } |
| 92 | |
| 93 | return osmo_sua_user_link_down(link, &prim->oph); |
| 94 | } |
| 95 | |
| 96 | int sccp_tx_conn_req_msg(struct osmo_sua_link *link, uint32_t conn_id, |
| 97 | const struct osmo_sccp_addr *calling_addr, |
| 98 | const struct osmo_sccp_addr *called_addr, |
| 99 | struct msgb *msg) |
| 100 | { |
| 101 | int rc; |
| 102 | |
| 103 | rc = sccp_tx_conn_req(link, conn_id, calling_addr, called_addr, |
| 104 | msg->data, msgb_length(msg)); |
| 105 | msgb_free(msg); |
| 106 | |
| 107 | return rc; |
| 108 | } |
| 109 | |
| 110 | int sccp_tx_data(struct osmo_sua_link *link, uint32_t conn_id, |
| 111 | uint8_t *data, unsigned int len) |
| 112 | { |
| 113 | struct msgb *msg = msgb_alloc(1024, "sccp_tx_data"); |
| 114 | struct osmo_scu_prim *prim; |
| 115 | |
| 116 | prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); |
| 117 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 118 | OSMO_SCU_PRIM_N_DATA, |
| 119 | PRIM_OP_REQUEST, msg); |
| 120 | prim->u.data.conn_id = conn_id; |
| 121 | |
| 122 | msg->l2h = msgb_put(msg, len); |
| 123 | memcpy(msg->l2h, data, len); |
| 124 | |
| 125 | return osmo_sua_user_link_down(link, &prim->oph); |
| 126 | } |
| 127 | |
| 128 | int sccp_tx_data_msg(struct osmo_sua_link *link, uint32_t conn_id, |
| 129 | struct msgb *msg) |
| 130 | { |
| 131 | int rc; |
| 132 | |
| 133 | rc = sccp_tx_data(link, conn_id, msg->data, msgb_length(msg)); |
| 134 | msgb_free(msg); |
| 135 | |
| 136 | return rc; |
| 137 | } |