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! */ |
| 63 | msgb_free(msg); |
| 64 | return -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 65 | } |
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 | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 106 | static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn, |
| 107 | struct msgb *msg, struct tlv_parsed *tlvp, |
| 108 | struct ipaccess_unit *unit) |
| 109 | { |
| 110 | LOGP(DLGSUP, LOGL_INFO, "CCM Callback\n"); |
| 111 | /* TODO: ? */ |
| 112 | return 0; |
| 113 | } |
| 114 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 115 | static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn) |
| 116 | { |
| 117 | struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data; |
| 118 | |
| 119 | LOGP(DLGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n", |
| 120 | conn->addr, conn->port); |
| 121 | |
| 122 | llist_del(&clnt->list); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /* a client has connected to the server socket and we have accept()ed it */ |
| 128 | static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd) |
| 129 | { |
| 130 | struct osmo_gsup_conn *conn; |
| 131 | struct osmo_gsup_server *gsups = |
| 132 | (struct osmo_gsup_server *) link->data; |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 133 | int rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 134 | |
| 135 | conn = talloc_zero(link->data, struct osmo_gsup_conn); |
| 136 | OSMO_ASSERT(conn); |
| 137 | |
| 138 | conn->conn = ipa_server_conn_create(conn, link, fd, |
| 139 | osmo_gsup_server_read_cb, |
| 140 | osmo_gsup_server_closed_cb, conn); |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 141 | conn->conn->ccm_cb = osmo_gsup_server_ccm_cb; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 142 | OSMO_ASSERT(conn->conn); |
| 143 | |
| 144 | /* link data structure with server structure */ |
| 145 | conn->server = gsups; |
| 146 | llist_add_tail(&conn->list, &gsups->clients); |
| 147 | |
| 148 | LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d\n", |
| 149 | conn->conn->addr, conn->conn->port); |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 150 | |
| 151 | /* request the identity of the client */ |
| 152 | rc = ipa_ccm_send_id_req(fd); |
| 153 | if (rc < 0) |
| 154 | goto failed; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 155 | #if 0 |
| 156 | rc = oap_init(&gsups->oap_config, &conn->oap_state); |
| 157 | if (rc != 0) |
| 158 | goto failed; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 159 | #endif |
| 160 | return 0; |
Harald Welte | 173afdb | 2016-04-28 08:53:25 +0200 | [diff] [blame] | 161 | failed: |
| 162 | ipa_server_conn_destroy(conn->conn); |
| 163 | return -1; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | struct osmo_gsup_server * |
| 167 | osmo_gsup_server_create(void *ctx, const char *ip_addr, |
| 168 | uint16_t tcp_port, |
| 169 | osmo_gsup_read_cb_t read_cb) |
| 170 | { |
| 171 | struct osmo_gsup_server *gsups; |
| 172 | int rc; |
| 173 | |
| 174 | gsups = talloc_zero(ctx, struct osmo_gsup_server); |
| 175 | OSMO_ASSERT(gsups); |
| 176 | |
| 177 | INIT_LLIST_HEAD(&gsups->clients); |
| 178 | |
| 179 | gsups->link = ipa_server_link_create(gsups, |
| 180 | /* no e1inp */ NULL, |
| 181 | ip_addr, tcp_port, |
| 182 | osmo_gsup_server_accept_cb, |
| 183 | gsups); |
| 184 | if (!gsups->link) |
| 185 | goto failed; |
| 186 | |
| 187 | gsups->read_cb = read_cb; |
| 188 | |
| 189 | rc = ipa_server_link_open(gsups->link); |
| 190 | if (rc < 0) |
| 191 | goto failed; |
| 192 | |
| 193 | return gsups; |
| 194 | |
| 195 | failed: |
| 196 | osmo_gsup_server_destroy(gsups); |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups) |
| 201 | { |
| 202 | if (gsups->link) { |
| 203 | ipa_server_link_close(gsups->link); |
| 204 | ipa_server_link_destroy(gsups->link); |
| 205 | gsups->link = NULL; |
| 206 | } |
| 207 | talloc_free(gsups); |
| 208 | } |