Harald Welte | e687be5 | 2016-05-03 18:49:27 +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 | |
| 20 | |
| 21 | #include <errno.h> |
| 22 | |
| 23 | #include <osmocom/core/linuxlist.h> |
| 24 | #include <osmocom/core/talloc.h> |
| 25 | |
Neels Hofmeyr | 2f75803 | 2019-11-20 00:37:07 +0100 | [diff] [blame] | 26 | #include <osmocom/hlr/logging.h> |
| 27 | #include <osmocom/hlr/gsup_server.h> |
| 28 | #include <osmocom/hlr/gsup_router.h> |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 29 | |
Oliver Smith | f7d3251 | 2019-04-08 15:00:43 +0200 | [diff] [blame] | 30 | /*! Find a route for the given address. |
| 31 | * \param[in] gs gsup server |
| 32 | * \param[in] addr IPA name of the client (SGSN, MSC/VLR). Although this is passed like a blob, together with the |
| 33 | * length, it must be nul-terminated! This is for legacy reasons, see the discussion here: |
| 34 | * https://gerrit.osmocom.org/#/c/osmo-hlr/+/13048/ |
| 35 | * \param[in] addrlen length of addr, *including the nul-byte* (strlen(addr) + 1). |
| 36 | */ |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 37 | struct osmo_gsup_conn *gsup_route_find(struct osmo_gsup_server *gs, |
| 38 | const uint8_t *addr, size_t addrlen) |
| 39 | { |
| 40 | struct gsup_route *gr; |
| 41 | |
| 42 | llist_for_each_entry(gr, &gs->routes, list) { |
| 43 | if (talloc_total_size(gr->addr) == addrlen && |
| 44 | !memcmp(gr->addr, addr, addrlen)) |
| 45 | return gr->conn; |
| 46 | } |
| 47 | return NULL; |
| 48 | } |
| 49 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 50 | struct osmo_gsup_conn *gsup_route_find_by_ipa_name(struct osmo_gsup_server *gs, const struct osmo_ipa_name *ipa_name) |
| 51 | { |
| 52 | return gsup_route_find(gs, ipa_name->val, ipa_name->len); |
| 53 | } |
| 54 | |
Oliver Smith | 95abc2b | 2019-04-04 12:00:24 +0200 | [diff] [blame] | 55 | /*! Find a GSUP connection's route (to read the IPA address from the route). |
| 56 | * \param[in] conn GSUP connection |
| 57 | * \return GSUP route |
| 58 | */ |
| 59 | struct gsup_route *gsup_route_find_by_conn(const struct osmo_gsup_conn *conn) |
| 60 | { |
| 61 | struct gsup_route *gr; |
| 62 | |
| 63 | llist_for_each_entry(gr, &conn->server->routes, list) { |
| 64 | if (gr->conn == conn) |
| 65 | return gr; |
| 66 | } |
| 67 | |
| 68 | return NULL; |
| 69 | } |
| 70 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 71 | /* add a new route for the given address to the given conn */ |
| 72 | int gsup_route_add(struct osmo_gsup_conn *conn, const uint8_t *addr, size_t addrlen) |
| 73 | { |
| 74 | struct gsup_route *gr; |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 75 | struct osmo_gsup_conn *exists_on_conn; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 76 | |
| 77 | /* Check if we already have a route for this address */ |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 78 | exists_on_conn = gsup_route_find(conn->server, addr, addrlen); |
| 79 | if (exists_on_conn) { |
| 80 | if (exists_on_conn != conn) |
| 81 | return -EEXIST; |
| 82 | return 0; |
| 83 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 84 | |
| 85 | /* allocate new route and populate it */ |
| 86 | gr = talloc_zero(conn->server, struct gsup_route); |
| 87 | if (!gr) |
| 88 | return -ENOMEM; |
| 89 | |
Max | 44a2180 | 2019-02-14 11:28:21 +0100 | [diff] [blame] | 90 | LOGP(DMAIN, LOGL_INFO, "Adding GSUP route for %s via %s:%u\n", addr, conn->conn->addr, conn->conn->port); |
Harald Welte | 21c14fc | 2018-07-23 17:54:41 +0200 | [diff] [blame] | 91 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 92 | gr->addr = talloc_memdup(gr, addr, addrlen); |
| 93 | gr->conn = conn; |
| 94 | llist_add_tail(&gr->list, &conn->server->routes); |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 99 | int gsup_route_add_ipa_name(struct osmo_gsup_conn *conn, const struct osmo_ipa_name *ipa_name) |
| 100 | { |
| 101 | return gsup_route_add(conn, ipa_name->val, ipa_name->len); |
| 102 | } |
| 103 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 104 | /* delete all routes for the given connection */ |
| 105 | int gsup_route_del_conn(struct osmo_gsup_conn *conn) |
| 106 | { |
| 107 | struct gsup_route *gr, *gr2; |
| 108 | unsigned int num_deleted = 0; |
| 109 | |
| 110 | llist_for_each_entry_safe(gr, gr2, &conn->server->routes, list) { |
| 111 | if (gr->conn == conn) { |
Harald Welte | 21c14fc | 2018-07-23 17:54:41 +0200 | [diff] [blame] | 112 | LOGP(DMAIN, LOGL_INFO, "Removing GSUP route for %s (GSUP disconnect)\n", |
Neels Hofmeyr | ad868e2 | 2019-11-20 02:36:45 +0100 | [diff] [blame] | 113 | osmo_quote_str_c(OTC_SELECT, (char*)gr->addr, talloc_total_size(gr->addr))); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 114 | llist_del(&gr->list); |
| 115 | talloc_free(gr); |
| 116 | num_deleted++; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return num_deleted; |
| 121 | } |