blob: 24ba73807f675e9df89fdafd3bfba248ddf1ba55 [file] [log] [blame]
Harald Welte936f6722016-05-03 18:51:18 +02001/* (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 Weltee72cf552016-04-28 07:18:49 +020020#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>
Stefan Sperling93c5b102018-04-10 19:26:14 +020027#include <osmocom/gsm/apn.h>
Harald Weltee72cf552016-04-28 07:18:49 +020028
29#include "gsup_server.h"
Neels Hofmeyr6eed3222016-12-11 01:21:49 +010030#include "gsup_router.h"
Harald Weltee72cf552016-04-28 07:18:49 +020031
32static void osmo_gsup_server_send(struct osmo_gsup_conn *conn,
33 int proto_ext, struct msgb *msg_tx)
34{
35 ipa_prepend_header_ext(msg_tx, proto_ext);
36 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
37 ipa_server_conn_send(conn->conn, msg_tx);
38}
39
40int osmo_gsup_conn_send(struct osmo_gsup_conn *conn, struct msgb *msg)
41{
42 if (!conn) {
43 msgb_free(msg);
44 return -ENOTCONN;
45 }
46
47 osmo_gsup_server_send(conn, IPAC_PROTO_EXT_GSUP, msg);
48
49 return 0;
50}
51
52static int osmo_gsup_conn_oap_handle(struct osmo_gsup_conn *conn,
53 struct msgb *msg_rx)
54{
Neels Hofmeyrec1b9592016-12-11 01:22:23 +010055#if 0
Harald Weltee72cf552016-04-28 07:18:49 +020056 int rc;
57 struct msgb *msg_tx;
Harald Weltee72cf552016-04-28 07:18:49 +020058 rc = oap_handle(&conn->oap_state, msg_rx, &msg_tx);
59 msgb_free(msg_rx);
60 if (rc < 0)
61 return rc;
62
63 if (msg_tx)
64 osmo_gsup_conn_send(conn, IPAC_PROTO_EXT_OAP, msg_tx);
65#endif
66 return 0;
67}
68
Harald Weltee72cf552016-04-28 07:18:49 +020069/* Data from a given client has arrived over the socket */
70static int osmo_gsup_server_read_cb(struct ipa_server_conn *conn,
71 struct msgb *msg)
72{
73 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
74 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
75 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
76 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +020077
78 msg->l2h = &hh->data[0];
79
Harald Welte173afdb2016-04-28 08:53:25 +020080 if (hh->proto == IPAC_PROTO_IPACCESS) {
81 rc = ipa_server_conn_ccm(conn, msg);
82 if (rc < 0) {
83 /* conn is already invalid here! */
Harald Welte173afdb2016-04-28 08:53:25 +020084 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +020085 }
Harald Welte5341b5d2016-04-28 12:48:39 +020086 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +020087 return 0;
88 }
Harald Weltee72cf552016-04-28 07:18:49 +020089
Harald Weltef2d96da2016-04-28 11:13:15 +020090 if (hh->proto != IPAC_PROTO_OSMO) {
91 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA stream ID 0x%02x\n",
92 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +020093 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +020094 }
Harald Weltee72cf552016-04-28 07:18:49 +020095
Harald Weltef2d96da2016-04-28 11:13:15 +020096 if (!he || msgb_l2len(msg) < sizeof(*he)) {
97 LOGP(DLGSUP, LOGL_NOTICE, "short IPA message\n");
Harald Weltee72cf552016-04-28 07:18:49 +020098 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +020099 }
Harald Weltee72cf552016-04-28 07:18:49 +0200100
101 msg->l2h = &he->data[0];
102
103 if (he->proto == IPAC_PROTO_EXT_GSUP) {
104 OSMO_ASSERT(clnt->server->read_cb != NULL);
105 clnt->server->read_cb(clnt, msg);
106 /* expecting read_cb() to free msg */
107 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
108 return osmo_gsup_conn_oap_handle(clnt, msg);
109 /* osmo_gsup_client_oap_handle frees msg */
Harald Weltef2d96da2016-04-28 11:13:15 +0200110 } else {
111 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA Osmo Proto 0x%02x\n",
112 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +0200113 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200114 }
Harald Weltee72cf552016-04-28 07:18:49 +0200115
116 return 0;
117
118invalid:
119 LOGP(DLGSUP, LOGL_NOTICE,
Harald Weltef2d96da2016-04-28 11:13:15 +0200120 "GSUP received an invalid IPA message from %s:%d: %s\n",
121 conn->addr, conn->port, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Weltee72cf552016-04-28 07:18:49 +0200122 msgb_free(msg);
123 return -1;
124
125}
126
Harald Weltee687be52016-05-03 18:49:27 +0200127static void osmo_tlvp_dump(const struct tlv_parsed *tlvp,
128 int subsys, int level)
129{
130 unsigned int i;
131
132 for (i = 0; i < ARRAY_SIZE(tlvp->lv); i++) {
133 if (!TLVP_PRESENT(tlvp, i))
134 continue;
135
136 LOGP(subsys, level, "%u: %s\n", i,
137 TLVP_VAL(tlvp, i));
138 LOGP(subsys, level, "%u: %s\n", i,
139 osmo_hexdump(TLVP_VAL(tlvp, i),
140 TLVP_LEN(tlvp, i)));
141 }
142}
143
144/* FIXME: should this be parrt of ipas_server handling, not GSUP? */
145static void tlvp_copy(void *ctx, struct tlv_parsed *out, const struct tlv_parsed *in)
146{
147 unsigned int i;
148
149 for (i = 0; i < ARRAY_SIZE(out->lv); i++) {
150 if (!TLVP_PRESENT(in, i)) {
151 if (TLVP_PRESENT(out, i)) {
152 talloc_free((void *) out->lv[i].val);
153 out->lv[i].val = NULL;
154 out->lv[i].len = 0;
155 }
156 continue;
157 }
158 out->lv[i].val = talloc_memdup(ctx, in->lv[i].val, in->lv[i].len);
159 out->lv[i].len = in->lv[i].len;
160 }
161}
162
163int osmo_gsup_conn_ccm_get(const struct osmo_gsup_conn *clnt, uint8_t **addr,
164 uint8_t tag)
165{
166 if (!TLVP_PRESENT(&clnt->ccm, tag))
167 return -ENODEV;
168 *addr = (uint8_t *) TLVP_VAL(&clnt->ccm, tag);
169
170 return TLVP_LEN(&clnt->ccm, tag);
171}
172
Harald Welte173afdb2016-04-28 08:53:25 +0200173static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn,
174 struct msgb *msg, struct tlv_parsed *tlvp,
175 struct ipaccess_unit *unit)
176{
Harald Weltee687be52016-05-03 18:49:27 +0200177 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100178 uint8_t *addr = NULL;
Harald Weltee687be52016-05-03 18:49:27 +0200179 size_t addr_len;
180
Harald Welte173afdb2016-04-28 08:53:25 +0200181 LOGP(DLGSUP, LOGL_INFO, "CCM Callback\n");
Harald Weltee687be52016-05-03 18:49:27 +0200182
183 /* FIXME: should this be parrt of ipas_server handling, not
184 * GSUP? */
185 tlvp_copy(clnt, &clnt->ccm, tlvp);
186 osmo_tlvp_dump(tlvp, DLGSUP, LOGL_INFO);
187
188 addr_len = osmo_gsup_conn_ccm_get(clnt, &addr, IPAC_IDTAG_SERNR);
Neels Hofmeyr5ecdc562017-02-24 06:38:04 +0100189 if (addr_len <= 0) {
190 LOGP(DLGSUP, LOGL_ERROR, "GSUP client %s:%u has no %s IE and"
191 " cannot be routed\n",
192 conn->addr, conn->port,
193 ipa_ccm_idtag_name(IPAC_IDTAG_SERNR));
194 return -EINVAL;
195 }
Harald Weltee687be52016-05-03 18:49:27 +0200196
Neels Hofmeyr5ecdc562017-02-24 06:38:04 +0100197 gsup_route_add(clnt, addr, addr_len);
Harald Welte173afdb2016-04-28 08:53:25 +0200198 return 0;
199}
200
Harald Weltee72cf552016-04-28 07:18:49 +0200201static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn)
202{
203 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
204
205 LOGP(DLGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n",
206 conn->addr, conn->port);
207
Harald Weltee687be52016-05-03 18:49:27 +0200208 gsup_route_del_conn(clnt);
Harald Weltee72cf552016-04-28 07:18:49 +0200209 llist_del(&clnt->list);
Harald Weltea7617e92016-04-28 12:57:10 +0200210 talloc_free(clnt);
Harald Weltee72cf552016-04-28 07:18:49 +0200211
212 return 0;
213}
214
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100215/* Add conn to the clients list in a way that conn->auc_3g_ind takes the lowest
216 * unused integer and the list of clients remains sorted by auc_3g_ind.
217 * Keep this function non-static to allow linking in a unit test. */
218void osmo_gsup_server_add_conn(struct llist_head *clients,
219 struct osmo_gsup_conn *conn)
220{
221 struct osmo_gsup_conn *c;
222 struct osmo_gsup_conn *prev_conn;
223
224 c = llist_first_entry_or_null(clients, struct osmo_gsup_conn, list);
225
226 /* Is the first index, 0, unused? */
227 if (!c || c->auc_3g_ind > 0) {
228 conn->auc_3g_ind = 0;
229 llist_add(&conn->list, clients);
230 return;
231 }
232
233 /* Look for a gap later on */
234 prev_conn = NULL;
235 llist_for_each_entry(c, clients, list) {
236 /* skip first item, we know it has auc_3g_ind == 0. */
237 if (!prev_conn) {
238 prev_conn = c;
239 continue;
240 }
241 if (c->auc_3g_ind > prev_conn->auc_3g_ind + 1)
242 break;
243 prev_conn = c;
244 }
245
246 OSMO_ASSERT(prev_conn);
247
248 conn->auc_3g_ind = prev_conn->auc_3g_ind + 1;
249 llist_add(&conn->list, &prev_conn->list);
250}
251
Harald Weltee72cf552016-04-28 07:18:49 +0200252/* a client has connected to the server socket and we have accept()ed it */
253static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd)
254{
255 struct osmo_gsup_conn *conn;
256 struct osmo_gsup_server *gsups =
257 (struct osmo_gsup_server *) link->data;
Harald Welte173afdb2016-04-28 08:53:25 +0200258 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200259
Harald Weltea7617e92016-04-28 12:57:10 +0200260 conn = talloc_zero(gsups, struct osmo_gsup_conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200261 OSMO_ASSERT(conn);
262
Harald Weltea7617e92016-04-28 12:57:10 +0200263 conn->conn = ipa_server_conn_create(gsups, link, fd,
Harald Weltee72cf552016-04-28 07:18:49 +0200264 osmo_gsup_server_read_cb,
265 osmo_gsup_server_closed_cb, conn);
266 OSMO_ASSERT(conn->conn);
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100267 conn->conn->ccm_cb = osmo_gsup_server_ccm_cb;
Harald Weltee72cf552016-04-28 07:18:49 +0200268
269 /* link data structure with server structure */
270 conn->server = gsups;
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100271 osmo_gsup_server_add_conn(&gsups->clients, conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200272
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100273 LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d (IND=%u)\n",
274 conn->conn->addr, conn->conn->port, conn->auc_3g_ind);
Harald Welte173afdb2016-04-28 08:53:25 +0200275
276 /* request the identity of the client */
277 rc = ipa_ccm_send_id_req(fd);
278 if (rc < 0)
279 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200280#if 0
281 rc = oap_init(&gsups->oap_config, &conn->oap_state);
282 if (rc != 0)
283 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200284#endif
285 return 0;
Harald Welte173afdb2016-04-28 08:53:25 +0200286failed:
287 ipa_server_conn_destroy(conn->conn);
288 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200289}
290
291struct osmo_gsup_server *
Max9cacb6f2017-02-20 17:22:56 +0100292osmo_gsup_server_create(void *ctx, const char *ip_addr, uint16_t tcp_port,
293 osmo_gsup_read_cb_t read_cb,
294 struct llist_head *lu_op_lst)
Harald Weltee72cf552016-04-28 07:18:49 +0200295{
296 struct osmo_gsup_server *gsups;
297 int rc;
298
299 gsups = talloc_zero(ctx, struct osmo_gsup_server);
300 OSMO_ASSERT(gsups);
301
302 INIT_LLIST_HEAD(&gsups->clients);
Harald Weltee687be52016-05-03 18:49:27 +0200303 INIT_LLIST_HEAD(&gsups->routes);
Harald Weltee72cf552016-04-28 07:18:49 +0200304
305 gsups->link = ipa_server_link_create(gsups,
306 /* no e1inp */ NULL,
307 ip_addr, tcp_port,
308 osmo_gsup_server_accept_cb,
309 gsups);
310 if (!gsups->link)
311 goto failed;
312
313 gsups->read_cb = read_cb;
314
315 rc = ipa_server_link_open(gsups->link);
316 if (rc < 0)
317 goto failed;
318
Max9cacb6f2017-02-20 17:22:56 +0100319 gsups->luop = lu_op_lst;
320
Harald Weltee72cf552016-04-28 07:18:49 +0200321 return gsups;
322
323failed:
324 osmo_gsup_server_destroy(gsups);
325 return NULL;
326}
327
328void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups)
329{
330 if (gsups->link) {
331 ipa_server_link_close(gsups->link);
332 ipa_server_link_destroy(gsups->link);
333 gsups->link = NULL;
334 }
335 talloc_free(gsups);
336}
Stefan Sperling93c5b102018-04-10 19:26:14 +0200337
338void osmo_gsup_configure_wildcard_apn(struct osmo_gsup_message *gsup)
339{
340 int l;
341 uint8_t apn[APN_MAXLEN];
342
343 l = osmo_apn_from_str(apn, sizeof(apn), "*");
344 if (l <= 0)
345 return;
346
347 gsup->pdp_infos[0].apn_enc = apn;
348 gsup->pdp_infos[0].apn_enc_len = l;
349 gsup->pdp_infos[0].have_info = 1;
350 gsup->num_pdp_infos = 1;
351 /* FIXME: use real value: */
352 gsup->pdp_infos[0].context_id = 1;
353}