Harald Welte | 936f672 | 2016-05-03 18:51:18 +0200 | [diff] [blame] | 1 | /* (C) 2016 by Harald Welte <laforge@gnumonks.org> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU Affero General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | |
| 22 | #include <osmocom/core/msgb.h> |
| 23 | #include <osmocom/core/logging.h> |
| 24 | #include <osmocom/core/linuxlist.h> |
| 25 | #include <osmocom/abis/ipa.h> |
| 26 | #include <osmocom/abis/ipaccess.h> |
| 27 | |
| 28 | #include "gsup_server.h" |
| 29 | |
| 30 | static void osmo_gsup_server_send(struct osmo_gsup_conn *conn, |
| 31 | int proto_ext, struct msgb *msg_tx) |
| 32 | { |
| 33 | ipa_prepend_header_ext(msg_tx, proto_ext); |
| 34 | ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO); |
| 35 | ipa_server_conn_send(conn->conn, msg_tx); |
| 36 | } |
| 37 | |
| 38 | int osmo_gsup_conn_send(struct osmo_gsup_conn *conn, struct msgb *msg) |
| 39 | { |
| 40 | if (!conn) { |
| 41 | msgb_free(msg); |
| 42 | return -ENOTCONN; |
| 43 | } |
| 44 | |
| 45 | osmo_gsup_server_send(conn, IPAC_PROTO_EXT_GSUP, msg); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int osmo_gsup_conn_oap_handle(struct osmo_gsup_conn *conn, |
| 51 | struct msgb *msg_rx) |
| 52 | { |
| 53 | int rc; |
| 54 | struct msgb *msg_tx; |
| 55 | #if 0 |
| 56 | rc = oap_handle(&conn->oap_state, msg_rx, &msg_tx); |
| 57 | msgb_free(msg_rx); |
| 58 | if (rc < 0) |
| 59 | return rc; |
| 60 | |
| 61 | if (msg_tx) |
| 62 | osmo_gsup_conn_send(conn, IPAC_PROTO_EXT_OAP, msg_tx); |
| 63 | #endif |
| 64 | return 0; |
| 65 | } |
| 66 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 67 | /* Data from a given client has arrived over the socket */ |
| 68 | static int osmo_gsup_server_read_cb(struct ipa_server_conn *conn, |
| 69 | struct msgb *msg) |
| 70 | { |
| 71 | struct ipaccess_head *hh = (struct ipaccess_head *) msg->data; |
| 72 | struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg); |
| 73 | struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data; |
| 74 | int rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 75 | |
| 76 | msg->l2h = &hh->data[0]; |
| 77 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 78 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 79 | rc = ipa_server_conn_ccm(conn, msg); |
| 80 | if (rc < 0) { |
| 81 | /* conn is already invalid here! */ |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 82 | return -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 83 | } |
Harald Welte | 5341b5d | 2016-04-28 12:48:39 +0200 | [diff] [blame] | 84 | msgb_free(msg); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 85 | return 0; |
| 86 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 87 | |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 88 | if (hh->proto != IPAC_PROTO_OSMO) { |
| 89 | LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA stream ID 0x%02x\n", |
| 90 | hh->proto); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 91 | goto invalid; |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 92 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 93 | |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 94 | if (!he || msgb_l2len(msg) < sizeof(*he)) { |
| 95 | LOGP(DLGSUP, LOGL_NOTICE, "short IPA message\n"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 96 | goto invalid; |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 97 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 98 | |
| 99 | msg->l2h = &he->data[0]; |
| 100 | |
| 101 | if (he->proto == IPAC_PROTO_EXT_GSUP) { |
| 102 | OSMO_ASSERT(clnt->server->read_cb != NULL); |
| 103 | clnt->server->read_cb(clnt, msg); |
| 104 | /* expecting read_cb() to free msg */ |
| 105 | } else if (he->proto == IPAC_PROTO_EXT_OAP) { |
| 106 | return osmo_gsup_conn_oap_handle(clnt, msg); |
| 107 | /* osmo_gsup_client_oap_handle frees msg */ |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 108 | } else { |
| 109 | LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA Osmo Proto 0x%02x\n", |
| 110 | hh->proto); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 111 | goto invalid; |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 112 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | |
| 116 | invalid: |
| 117 | LOGP(DLGSUP, LOGL_NOTICE, |
Harald Welte | f2d96da | 2016-04-28 11:13:15 +0200 | [diff] [blame] | 118 | "GSUP received an invalid IPA message from %s:%d: %s\n", |
| 119 | conn->addr, conn->port, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg))); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 120 | msgb_free(msg); |
| 121 | return -1; |
| 122 | |
| 123 | } |
| 124 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 125 | static void osmo_tlvp_dump(const struct tlv_parsed *tlvp, |
| 126 | int subsys, int level) |
| 127 | { |
| 128 | unsigned int i; |
| 129 | |
| 130 | for (i = 0; i < ARRAY_SIZE(tlvp->lv); i++) { |
| 131 | if (!TLVP_PRESENT(tlvp, i)) |
| 132 | continue; |
| 133 | |
| 134 | LOGP(subsys, level, "%u: %s\n", i, |
| 135 | TLVP_VAL(tlvp, i)); |
| 136 | LOGP(subsys, level, "%u: %s\n", i, |
| 137 | osmo_hexdump(TLVP_VAL(tlvp, i), |
| 138 | TLVP_LEN(tlvp, i))); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /* FIXME: should this be parrt of ipas_server handling, not GSUP? */ |
| 143 | static void tlvp_copy(void *ctx, struct tlv_parsed *out, const struct tlv_parsed *in) |
| 144 | { |
| 145 | unsigned int i; |
| 146 | |
| 147 | for (i = 0; i < ARRAY_SIZE(out->lv); i++) { |
| 148 | if (!TLVP_PRESENT(in, i)) { |
| 149 | if (TLVP_PRESENT(out, i)) { |
| 150 | talloc_free((void *) out->lv[i].val); |
| 151 | out->lv[i].val = NULL; |
| 152 | out->lv[i].len = 0; |
| 153 | } |
| 154 | continue; |
| 155 | } |
| 156 | out->lv[i].val = talloc_memdup(ctx, in->lv[i].val, in->lv[i].len); |
| 157 | out->lv[i].len = in->lv[i].len; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | int osmo_gsup_conn_ccm_get(const struct osmo_gsup_conn *clnt, uint8_t **addr, |
| 162 | uint8_t tag) |
| 163 | { |
| 164 | if (!TLVP_PRESENT(&clnt->ccm, tag)) |
| 165 | return -ENODEV; |
| 166 | *addr = (uint8_t *) TLVP_VAL(&clnt->ccm, tag); |
| 167 | |
| 168 | return TLVP_LEN(&clnt->ccm, tag); |
| 169 | } |
| 170 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 171 | static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn, |
| 172 | struct msgb *msg, struct tlv_parsed *tlvp, |
| 173 | struct ipaccess_unit *unit) |
| 174 | { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 175 | struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data; |
| 176 | uint8_t *addr; |
| 177 | size_t addr_len; |
| 178 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 179 | LOGP(DLGSUP, LOGL_INFO, "CCM Callback\n"); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 180 | |
| 181 | /* FIXME: should this be parrt of ipas_server handling, not |
| 182 | * GSUP? */ |
| 183 | tlvp_copy(clnt, &clnt->ccm, tlvp); |
| 184 | osmo_tlvp_dump(tlvp, DLGSUP, LOGL_INFO); |
| 185 | |
| 186 | addr_len = osmo_gsup_conn_ccm_get(clnt, &addr, IPAC_IDTAG_SERNR); |
| 187 | if (addr_len) |
| 188 | gsup_route_add(clnt, addr, addr_len); |
| 189 | |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 193 | static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn) |
| 194 | { |
| 195 | struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data; |
| 196 | |
| 197 | LOGP(DLGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n", |
| 198 | conn->addr, conn->port); |
| 199 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 200 | gsup_route_del_conn(clnt); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 201 | llist_del(&clnt->list); |
Harald Welte | a7617e9 | 2016-04-28 12:57:10 +0200 | [diff] [blame] | 202 | talloc_free(clnt); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /* a client has connected to the server socket and we have accept()ed it */ |
| 208 | static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd) |
| 209 | { |
| 210 | struct osmo_gsup_conn *conn; |
| 211 | struct osmo_gsup_server *gsups = |
| 212 | (struct osmo_gsup_server *) link->data; |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 213 | int rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 214 | |
Harald Welte | a7617e9 | 2016-04-28 12:57:10 +0200 | [diff] [blame] | 215 | conn = talloc_zero(gsups, struct osmo_gsup_conn); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 216 | OSMO_ASSERT(conn); |
| 217 | |
Harald Welte | a7617e9 | 2016-04-28 12:57:10 +0200 | [diff] [blame] | 218 | conn->conn = ipa_server_conn_create(gsups, link, fd, |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 219 | osmo_gsup_server_read_cb, |
| 220 | osmo_gsup_server_closed_cb, conn); |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 221 | conn->conn->ccm_cb = osmo_gsup_server_ccm_cb; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 222 | OSMO_ASSERT(conn->conn); |
| 223 | |
| 224 | /* link data structure with server structure */ |
| 225 | conn->server = gsups; |
| 226 | llist_add_tail(&conn->list, &gsups->clients); |
| 227 | |
| 228 | LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d\n", |
| 229 | conn->conn->addr, conn->conn->port); |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 230 | |
| 231 | /* request the identity of the client */ |
| 232 | rc = ipa_ccm_send_id_req(fd); |
| 233 | if (rc < 0) |
| 234 | goto failed; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 235 | #if 0 |
| 236 | rc = oap_init(&gsups->oap_config, &conn->oap_state); |
| 237 | if (rc != 0) |
| 238 | goto failed; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 239 | #endif |
| 240 | return 0; |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 241 | failed: |
| 242 | ipa_server_conn_destroy(conn->conn); |
| 243 | return -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | struct osmo_gsup_server * |
| 247 | osmo_gsup_server_create(void *ctx, const char *ip_addr, |
| 248 | uint16_t tcp_port, |
| 249 | osmo_gsup_read_cb_t read_cb) |
| 250 | { |
| 251 | struct osmo_gsup_server *gsups; |
| 252 | int rc; |
| 253 | |
| 254 | gsups = talloc_zero(ctx, struct osmo_gsup_server); |
| 255 | OSMO_ASSERT(gsups); |
| 256 | |
| 257 | INIT_LLIST_HEAD(&gsups->clients); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 258 | INIT_LLIST_HEAD(&gsups->routes); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 259 | |
| 260 | gsups->link = ipa_server_link_create(gsups, |
| 261 | /* no e1inp */ NULL, |
| 262 | ip_addr, tcp_port, |
| 263 | osmo_gsup_server_accept_cb, |
| 264 | gsups); |
| 265 | if (!gsups->link) |
| 266 | goto failed; |
| 267 | |
| 268 | gsups->read_cb = read_cb; |
| 269 | |
| 270 | rc = ipa_server_link_open(gsups->link); |
| 271 | if (rc < 0) |
| 272 | goto failed; |
| 273 | |
| 274 | return gsups; |
| 275 | |
| 276 | failed: |
| 277 | osmo_gsup_server_destroy(gsups); |
| 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups) |
| 282 | { |
| 283 | if (gsups->link) { |
| 284 | ipa_server_link_close(gsups->link); |
| 285 | ipa_server_link_destroy(gsups->link); |
| 286 | gsups->link = NULL; |
| 287 | } |
| 288 | talloc_free(gsups); |
| 289 | } |