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