Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 1 | /* Mapper between RUA ContextID (24 bit, per HNB) and the SUA/SCCP |
| 2 | * Connection ID (32bit, per signalling link) */ |
| 3 | |
| 4 | /* (C) 2015 by Harald Welte <laforge@gnumonks.org> |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | /* an expired mapping is destroyed after 1..2 * EXPIRY_TIMER_SECS */ |
| 23 | #define EXPIRY_TIMER_SECS 23 |
| 24 | |
| 25 | #include <osmocom/core/timer.h> |
| 26 | |
| 27 | #include "hnbgw.h" |
| 28 | #include "context_map.h" |
| 29 | |
| 30 | /* is a given SCCP USER SAP Connection ID in use for a given CN link? */ |
| 31 | static int cn_id_in_use(struct hnbgw_cnlink *cn, uint32_t id) |
| 32 | { |
| 33 | struct hnbgw_context_map *map; |
| 34 | |
| 35 | llist_for_each_entry(map, &cn->map_list, cn_list) { |
| 36 | if (map->scu_conn_id == id) |
| 37 | return 1; |
| 38 | } |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | /* try to allocate a new SCCP User SAP Connection ID */ |
| 43 | static int alloc_cn_conn_id(struct hnbgw_cnlink *cn, uint32_t *id_out) |
| 44 | { |
| 45 | uint32_t i; |
| 46 | uint32_t id; |
| 47 | |
| 48 | for (i = 0; i < 0xffffffff; i++) { |
| 49 | id = cn->next_conn_id++; |
| 50 | if (!cn_id_in_use(cn, id)) { |
| 51 | *id_out = id; |
| 52 | return 1; |
| 53 | } |
| 54 | } |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | /* Map from a HNB + ContextID to the SCCP-side Connection ID */ |
| 59 | struct hnbgw_context_map * |
| 60 | context_map_alloc_by_hnb(struct hnb_context *hnb, uint32_t rua_ctx_id, |
| 61 | struct hnbgw_cnlink *cn_if_new) |
| 62 | { |
| 63 | struct hnbgw_context_map *map; |
| 64 | uint32_t new_scu_conn_id; |
| 65 | |
| 66 | llist_for_each_entry(map, &hnb->map_list, hnb_list) { |
| 67 | if (map->state != MAP_S_ACTIVE) |
| 68 | continue; |
| 69 | if (map->rua_ctx_id == rua_ctx_id) { |
| 70 | return map; |
| 71 | } |
| 72 | } |
| 73 | |
Harald Welte | 37166a2 | 2015-12-24 10:12:09 +0100 | [diff] [blame] | 74 | if (alloc_cn_conn_id(cn_if_new, &new_scu_conn_id) < 0) { |
| 75 | LOGP(DMAIN, LOGL_ERROR, "Unable to allocate CN connection ID\n"); |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 76 | return NULL; |
Harald Welte | 37166a2 | 2015-12-24 10:12:09 +0100 | [diff] [blame] | 77 | } |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 78 | |
| 79 | /* alloate a new map entry */ |
| 80 | map = talloc_zero(hnb, struct hnbgw_context_map); |
| 81 | map->state = MAP_S_NULL; |
| 82 | map->cn_link = cn_if_new; |
| 83 | map->hnb_ctx = hnb; |
| 84 | map->rua_ctx_id = rua_ctx_id; |
| 85 | |
| 86 | /* put it into both lists */ |
| 87 | llist_add_tail(&map->hnb_list, &hnb->map_list); |
| 88 | llist_add_tail(&map->cn_list, &cn_if_new->map_list); |
| 89 | map->state = MAP_S_ACTIVE; |
| 90 | |
| 91 | return map; |
| 92 | } |
| 93 | |
| 94 | /* Map from a CN + Connection ID to HNB + Context ID */ |
| 95 | struct hnbgw_context_map * |
| 96 | context_map_by_cn(struct hnbgw_cnlink *cn, uint32_t scu_conn_id) |
| 97 | { |
| 98 | struct hnbgw_context_map *map; |
| 99 | |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 100 | llist_for_each_entry(map, &cn->map_list, cn_list) { |
| 101 | if (map->state != MAP_S_ACTIVE) |
| 102 | continue; |
| 103 | if (map->scu_conn_id == scu_conn_id) { |
| 104 | return map; |
| 105 | } |
| 106 | } |
| 107 | /* we don't allocate new mappings in the CN->HNB |
| 108 | * direction, as the RUA=SCCP=SUA connections are always |
Harald Welte | 37166a2 | 2015-12-24 10:12:09 +0100 | [diff] [blame] | 109 | * established from HNB towards CN. */ |
| 110 | LOGP(DMAIN, LOGL_NOTICE, "Unable to resolve map for CN " |
| 111 | "connection ID %u\n", scu_conn_id); |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | void context_map_deactivate(struct hnbgw_context_map *map) |
| 116 | { |
| 117 | /* set the state to reserved. We still show up in the list and |
| 118 | * avoid re-allocation of the context-id until we are cleaned up |
| 119 | * by the context_map garbage collector timer */ |
| 120 | |
| 121 | if (map->state != MAP_S_RESERVED2) |
| 122 | map->state = MAP_S_RESERVED1; |
| 123 | } |
| 124 | |
| 125 | static struct osmo_timer_list context_map_tmr; |
| 126 | |
| 127 | static void context_map_tmr_cb(void *data) |
| 128 | { |
| 129 | struct hnb_gw *gw = data; |
| 130 | struct hnbgw_cnlink *cn; |
| 131 | |
Harald Welte | 37166a2 | 2015-12-24 10:12:09 +0100 | [diff] [blame] | 132 | DEBUGP(DMAIN, "Running context mapper garbage collection\n"); |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 133 | /* iterate over list of core network (links) */ |
| 134 | llist_for_each_entry(cn, &gw->cn_list, list) { |
| 135 | struct hnbgw_context_map *map; |
| 136 | |
| 137 | llist_for_each_entry(map, &cn->map_list, cn_list) { |
| 138 | switch (map->state) { |
| 139 | case MAP_S_RESERVED1: |
| 140 | /* first time we see this reserved |
| 141 | * entry: mark it for stage 2 */ |
| 142 | map->state = MAP_S_RESERVED2; |
| 143 | break; |
| 144 | case MAP_S_RESERVED2: |
| 145 | /* first time we see this reserved |
| 146 | * entry: remove it */ |
| 147 | map->state = MAP_S_NULL; |
| 148 | llist_del(&map->cn_list); |
| 149 | llist_del(&map->hnb_list); |
| 150 | talloc_free(map); |
| 151 | break; |
| 152 | default: |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | /* re-schedule this timer */ |
| 158 | osmo_timer_schedule(&context_map_tmr, EXPIRY_TIMER_SECS, 0); |
| 159 | } |
| 160 | |
| 161 | int context_map_init(struct hnb_gw *gw) |
| 162 | { |
| 163 | context_map_tmr.cb = context_map_tmr_cb; |
| 164 | context_map_tmr.data = gw; |
| 165 | osmo_timer_schedule(&context_map_tmr, EXPIRY_TIMER_SECS, 0); |
| 166 | } |