blob: e75bbd78d9666c15bfd00cb9c7e72d4bc532ab21 [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 Sperlingf83432c2018-05-03 14:26:59 +020027#include <osmocom/gsm/gsm48_ie.h>
Stefan Sperling93c5b102018-04-10 19:26:14 +020028#include <osmocom/gsm/apn.h>
Harald Weltee72cf552016-04-28 07:18:49 +020029
30#include "gsup_server.h"
Neels Hofmeyr6eed3222016-12-11 01:21:49 +010031#include "gsup_router.h"
Harald Weltee72cf552016-04-28 07:18:49 +020032
33static void osmo_gsup_server_send(struct osmo_gsup_conn *conn,
34 int proto_ext, struct msgb *msg_tx)
35{
36 ipa_prepend_header_ext(msg_tx, proto_ext);
37 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
38 ipa_server_conn_send(conn->conn, msg_tx);
39}
40
41int osmo_gsup_conn_send(struct osmo_gsup_conn *conn, struct msgb *msg)
42{
43 if (!conn) {
44 msgb_free(msg);
45 return -ENOTCONN;
46 }
47
48 osmo_gsup_server_send(conn, IPAC_PROTO_EXT_GSUP, msg);
49
50 return 0;
51}
52
53static int osmo_gsup_conn_oap_handle(struct osmo_gsup_conn *conn,
54 struct msgb *msg_rx)
55{
Neels Hofmeyrec1b9592016-12-11 01:22:23 +010056#if 0
Harald Weltee72cf552016-04-28 07:18:49 +020057 int rc;
58 struct msgb *msg_tx;
Harald Weltee72cf552016-04-28 07:18:49 +020059 rc = oap_handle(&conn->oap_state, msg_rx, &msg_tx);
60 msgb_free(msg_rx);
61 if (rc < 0)
62 return rc;
63
64 if (msg_tx)
65 osmo_gsup_conn_send(conn, IPAC_PROTO_EXT_OAP, msg_tx);
66#endif
67 return 0;
68}
69
Harald Weltee72cf552016-04-28 07:18:49 +020070/* Data from a given client has arrived over the socket */
71static int osmo_gsup_server_read_cb(struct ipa_server_conn *conn,
72 struct msgb *msg)
73{
74 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
75 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
76 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
77 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +020078
79 msg->l2h = &hh->data[0];
80
Harald Welte173afdb2016-04-28 08:53:25 +020081 if (hh->proto == IPAC_PROTO_IPACCESS) {
82 rc = ipa_server_conn_ccm(conn, msg);
83 if (rc < 0) {
84 /* conn is already invalid here! */
Harald Welte173afdb2016-04-28 08:53:25 +020085 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +020086 }
Harald Welte5341b5d2016-04-28 12:48:39 +020087 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +020088 return 0;
89 }
Harald Weltee72cf552016-04-28 07:18:49 +020090
Harald Weltef2d96da2016-04-28 11:13:15 +020091 if (hh->proto != IPAC_PROTO_OSMO) {
92 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA stream ID 0x%02x\n",
93 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +020094 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +020095 }
Harald Weltee72cf552016-04-28 07:18:49 +020096
Harald Weltef2d96da2016-04-28 11:13:15 +020097 if (!he || msgb_l2len(msg) < sizeof(*he)) {
98 LOGP(DLGSUP, LOGL_NOTICE, "short IPA message\n");
Harald Weltee72cf552016-04-28 07:18:49 +020099 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200100 }
Harald Weltee72cf552016-04-28 07:18:49 +0200101
102 msg->l2h = &he->data[0];
103
104 if (he->proto == IPAC_PROTO_EXT_GSUP) {
105 OSMO_ASSERT(clnt->server->read_cb != NULL);
106 clnt->server->read_cb(clnt, msg);
107 /* expecting read_cb() to free msg */
108 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
109 return osmo_gsup_conn_oap_handle(clnt, msg);
110 /* osmo_gsup_client_oap_handle frees msg */
Harald Weltef2d96da2016-04-28 11:13:15 +0200111 } else {
112 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA Osmo Proto 0x%02x\n",
113 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +0200114 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200115 }
Harald Weltee72cf552016-04-28 07:18:49 +0200116
117 return 0;
118
119invalid:
120 LOGP(DLGSUP, LOGL_NOTICE,
Harald Weltef2d96da2016-04-28 11:13:15 +0200121 "GSUP received an invalid IPA message from %s:%d: %s\n",
122 conn->addr, conn->port, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Weltee72cf552016-04-28 07:18:49 +0200123 msgb_free(msg);
124 return -1;
125
126}
127
Harald Weltee687be52016-05-03 18:49:27 +0200128static void osmo_tlvp_dump(const struct tlv_parsed *tlvp,
129 int subsys, int level)
130{
131 unsigned int i;
132
133 for (i = 0; i < ARRAY_SIZE(tlvp->lv); i++) {
134 if (!TLVP_PRESENT(tlvp, i))
135 continue;
136
137 LOGP(subsys, level, "%u: %s\n", i,
138 TLVP_VAL(tlvp, i));
139 LOGP(subsys, level, "%u: %s\n", i,
140 osmo_hexdump(TLVP_VAL(tlvp, i),
141 TLVP_LEN(tlvp, i)));
142 }
143}
144
145/* FIXME: should this be parrt of ipas_server handling, not GSUP? */
146static void tlvp_copy(void *ctx, struct tlv_parsed *out, const struct tlv_parsed *in)
147{
148 unsigned int i;
149
150 for (i = 0; i < ARRAY_SIZE(out->lv); i++) {
151 if (!TLVP_PRESENT(in, i)) {
152 if (TLVP_PRESENT(out, i)) {
153 talloc_free((void *) out->lv[i].val);
154 out->lv[i].val = NULL;
155 out->lv[i].len = 0;
156 }
157 continue;
158 }
159 out->lv[i].val = talloc_memdup(ctx, in->lv[i].val, in->lv[i].len);
160 out->lv[i].len = in->lv[i].len;
161 }
162}
163
164int osmo_gsup_conn_ccm_get(const struct osmo_gsup_conn *clnt, uint8_t **addr,
165 uint8_t tag)
166{
167 if (!TLVP_PRESENT(&clnt->ccm, tag))
168 return -ENODEV;
169 *addr = (uint8_t *) TLVP_VAL(&clnt->ccm, tag);
170
171 return TLVP_LEN(&clnt->ccm, tag);
172}
173
Harald Welte173afdb2016-04-28 08:53:25 +0200174static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn,
175 struct msgb *msg, struct tlv_parsed *tlvp,
176 struct ipaccess_unit *unit)
177{
Harald Weltee687be52016-05-03 18:49:27 +0200178 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100179 uint8_t *addr = NULL;
Harald Weltee687be52016-05-03 18:49:27 +0200180 size_t addr_len;
181
Harald Welte173afdb2016-04-28 08:53:25 +0200182 LOGP(DLGSUP, LOGL_INFO, "CCM Callback\n");
Harald Weltee687be52016-05-03 18:49:27 +0200183
184 /* FIXME: should this be parrt of ipas_server handling, not
185 * GSUP? */
186 tlvp_copy(clnt, &clnt->ccm, tlvp);
187 osmo_tlvp_dump(tlvp, DLGSUP, LOGL_INFO);
188
189 addr_len = osmo_gsup_conn_ccm_get(clnt, &addr, IPAC_IDTAG_SERNR);
Neels Hofmeyr5ecdc562017-02-24 06:38:04 +0100190 if (addr_len <= 0) {
191 LOGP(DLGSUP, LOGL_ERROR, "GSUP client %s:%u has no %s IE and"
192 " cannot be routed\n",
193 conn->addr, conn->port,
194 ipa_ccm_idtag_name(IPAC_IDTAG_SERNR));
195 return -EINVAL;
196 }
Harald Weltee687be52016-05-03 18:49:27 +0200197
Neels Hofmeyr5ecdc562017-02-24 06:38:04 +0100198 gsup_route_add(clnt, addr, addr_len);
Harald Welte173afdb2016-04-28 08:53:25 +0200199 return 0;
200}
201
Harald Weltee72cf552016-04-28 07:18:49 +0200202static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn)
203{
204 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
205
206 LOGP(DLGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n",
207 conn->addr, conn->port);
208
Harald Weltee687be52016-05-03 18:49:27 +0200209 gsup_route_del_conn(clnt);
Harald Weltee72cf552016-04-28 07:18:49 +0200210 llist_del(&clnt->list);
Harald Weltea7617e92016-04-28 12:57:10 +0200211 talloc_free(clnt);
Harald Weltee72cf552016-04-28 07:18:49 +0200212
213 return 0;
214}
215
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100216/* Add conn to the clients list in a way that conn->auc_3g_ind takes the lowest
217 * unused integer and the list of clients remains sorted by auc_3g_ind.
218 * Keep this function non-static to allow linking in a unit test. */
219void osmo_gsup_server_add_conn(struct llist_head *clients,
220 struct osmo_gsup_conn *conn)
221{
222 struct osmo_gsup_conn *c;
223 struct osmo_gsup_conn *prev_conn;
224
225 c = llist_first_entry_or_null(clients, struct osmo_gsup_conn, list);
226
227 /* Is the first index, 0, unused? */
228 if (!c || c->auc_3g_ind > 0) {
229 conn->auc_3g_ind = 0;
230 llist_add(&conn->list, clients);
231 return;
232 }
233
234 /* Look for a gap later on */
235 prev_conn = NULL;
236 llist_for_each_entry(c, clients, list) {
237 /* skip first item, we know it has auc_3g_ind == 0. */
238 if (!prev_conn) {
239 prev_conn = c;
240 continue;
241 }
242 if (c->auc_3g_ind > prev_conn->auc_3g_ind + 1)
243 break;
244 prev_conn = c;
245 }
246
247 OSMO_ASSERT(prev_conn);
248
249 conn->auc_3g_ind = prev_conn->auc_3g_ind + 1;
250 llist_add(&conn->list, &prev_conn->list);
251}
252
Harald Weltee72cf552016-04-28 07:18:49 +0200253/* a client has connected to the server socket and we have accept()ed it */
254static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd)
255{
256 struct osmo_gsup_conn *conn;
257 struct osmo_gsup_server *gsups =
258 (struct osmo_gsup_server *) link->data;
Harald Welte173afdb2016-04-28 08:53:25 +0200259 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200260
Harald Weltea7617e92016-04-28 12:57:10 +0200261 conn = talloc_zero(gsups, struct osmo_gsup_conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200262 OSMO_ASSERT(conn);
263
Harald Weltea7617e92016-04-28 12:57:10 +0200264 conn->conn = ipa_server_conn_create(gsups, link, fd,
Harald Weltee72cf552016-04-28 07:18:49 +0200265 osmo_gsup_server_read_cb,
266 osmo_gsup_server_closed_cb, conn);
267 OSMO_ASSERT(conn->conn);
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100268 conn->conn->ccm_cb = osmo_gsup_server_ccm_cb;
Harald Weltee72cf552016-04-28 07:18:49 +0200269
270 /* link data structure with server structure */
271 conn->server = gsups;
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100272 osmo_gsup_server_add_conn(&gsups->clients, conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200273
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100274 LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d (IND=%u)\n",
275 conn->conn->addr, conn->conn->port, conn->auc_3g_ind);
Harald Welte173afdb2016-04-28 08:53:25 +0200276
277 /* request the identity of the client */
278 rc = ipa_ccm_send_id_req(fd);
279 if (rc < 0)
280 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200281#if 0
282 rc = oap_init(&gsups->oap_config, &conn->oap_state);
283 if (rc != 0)
284 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200285#endif
286 return 0;
Harald Welte173afdb2016-04-28 08:53:25 +0200287failed:
288 ipa_server_conn_destroy(conn->conn);
289 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200290}
291
292struct osmo_gsup_server *
Max9cacb6f2017-02-20 17:22:56 +0100293osmo_gsup_server_create(void *ctx, const char *ip_addr, uint16_t tcp_port,
294 osmo_gsup_read_cb_t read_cb,
Harald Welte32acace2018-06-16 17:07:28 +0200295 struct llist_head *lu_op_lst, void *priv)
Harald Weltee72cf552016-04-28 07:18:49 +0200296{
297 struct osmo_gsup_server *gsups;
298 int rc;
299
300 gsups = talloc_zero(ctx, struct osmo_gsup_server);
301 OSMO_ASSERT(gsups);
302
303 INIT_LLIST_HEAD(&gsups->clients);
Harald Weltee687be52016-05-03 18:49:27 +0200304 INIT_LLIST_HEAD(&gsups->routes);
Harald Weltee72cf552016-04-28 07:18:49 +0200305
306 gsups->link = ipa_server_link_create(gsups,
307 /* no e1inp */ NULL,
308 ip_addr, tcp_port,
309 osmo_gsup_server_accept_cb,
310 gsups);
311 if (!gsups->link)
312 goto failed;
313
314 gsups->read_cb = read_cb;
Harald Welte32acace2018-06-16 17:07:28 +0200315 gsups->priv = priv;
Harald Weltee72cf552016-04-28 07:18:49 +0200316
317 rc = ipa_server_link_open(gsups->link);
318 if (rc < 0)
319 goto failed;
320
Max9cacb6f2017-02-20 17:22:56 +0100321 gsups->luop = lu_op_lst;
322
Harald Weltee72cf552016-04-28 07:18:49 +0200323 return gsups;
324
325failed:
326 osmo_gsup_server_destroy(gsups);
327 return NULL;
328}
329
330void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups)
331{
332 if (gsups->link) {
333 ipa_server_link_close(gsups->link);
334 ipa_server_link_destroy(gsups->link);
335 gsups->link = NULL;
336 }
337 talloc_free(gsups);
338}
Stefan Sperling93c5b102018-04-10 19:26:14 +0200339
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200340/* Set GSUP message's pdp_infos[0] to a wildcard APN.
341 * Use the provided apn_buf to store the produced APN data. This must remain valid until
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200342 * osmo_gsup_encode() is done. Return 0 if an entry was added, -ENOMEM if the provided buffer is too
343 * small. */
344int osmo_gsup_configure_wildcard_apn(struct osmo_gsup_message *gsup,
345 uint8_t *apn_buf, size_t apn_buf_size)
Stefan Sperling93c5b102018-04-10 19:26:14 +0200346{
347 int l;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200348
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200349 l = osmo_apn_from_str(apn_buf, apn_buf_size, "*");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200350 if (l <= 0)
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200351 return -ENOMEM;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200352
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200353 gsup->pdp_infos[0].apn_enc = apn_buf;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200354 gsup->pdp_infos[0].apn_enc_len = l;
355 gsup->pdp_infos[0].have_info = 1;
356 gsup->num_pdp_infos = 1;
357 /* FIXME: use real value: */
358 gsup->pdp_infos[0].context_id = 1;
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200359
360 return 0;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200361}
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200362
363/**
364 * Populate a gsup message structure with an Insert Subscriber Data Message.
365 * All required memory buffers for data pointed to by pointers in struct omso_gsup_message
366 * must be allocated by the caller and should have the same lifetime as the gsup parameter.
367 *
368 * \param[out] gsup The gsup message to populate.
369 * \param[in] imsi The subscriber's IMSI.
370 * \param[in] msisdn The subscriber's MSISDN.
371 * \param[out] msisdn_enc A buffer large enough to store the MSISDN in encoded form.
372 * \param[in] msisdn_enc_size Size of the buffer (must be >= OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN).
373 * \param[out] apn_buf A buffer large enough to store an APN (required if cn_domain is OSMO_GSUP_CN_DOMAIN_PS).
374 * \param[in] apn_buf_size Size of APN buffer (must be >= APN_MAXLEN).
375 * \param[in] cn_domain The CN Domain of the subscriber connection.
376 * \returns 0 on success, and negative on error.
377 */
378int osmo_gsup_create_insert_subscriber_data_msg(struct osmo_gsup_message *gsup, const char *imsi, const char *msisdn,
379 uint8_t *msisdn_enc, size_t msisdn_enc_size,
380 uint8_t *apn_buf, size_t apn_buf_size,
381 enum osmo_gsup_cn_domain cn_domain)
382{
383 int len;
384
385 OSMO_ASSERT(gsup);
386
387 gsup->message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST;
388 osmo_strlcpy(gsup->imsi, imsi, sizeof(gsup->imsi));
389
390 if (msisdn_enc_size < OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN)
391 return -ENOSPC;
392
393 OSMO_ASSERT(msisdn_enc);
394 len = gsm48_encode_bcd_number(msisdn_enc, msisdn_enc_size, 0, msisdn);
395 if (len < 1) {
396 LOGP(DLGSUP, LOGL_ERROR, "%s: Error: cannot encode MSISDN '%s'\n", imsi, msisdn);
397 return -ENOSPC;
398 }
399 gsup->msisdn_enc = msisdn_enc;
400 gsup->msisdn_enc_len = len;
401
402 #pragma message "FIXME: deal with encoding the following data: gsup.hlr_enc"
403
404 gsup->cn_domain = cn_domain;
405 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) {
406 OSMO_ASSERT(apn_buf_size >= APN_MAXLEN);
407 OSMO_ASSERT(apn_buf);
408 /* FIXME: PDP infos - use more fine-grained access control
409 instead of wildcard APN */
410 osmo_gsup_configure_wildcard_apn(gsup, apn_buf, apn_buf_size);
411 }
412
413 return 0;
414}