blob: 0960cb95d56eeeefebc72004081ddf58fdbe9a31 [file] [log] [blame]
Harald Welte90256ba2015-12-23 20:16:36 +01001/* 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
Neels Hofmeyrdf63de22016-08-18 13:13:55 +020027#include <osmocom/iuh/hnbgw.h>
28#include <osmocom/iuh/context_map.h>
Harald Welte90256ba2015-12-23 20:16:36 +010029
30/* is a given SCCP USER SAP Connection ID in use for a given CN link? */
31static 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 */
43static 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 */
59struct hnbgw_context_map *
60context_map_alloc_by_hnb(struct hnb_context *hnb, uint32_t rua_ctx_id,
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020061 bool is_ps,
Harald Welte90256ba2015-12-23 20:16:36 +010062 struct hnbgw_cnlink *cn_if_new)
63{
64 struct hnbgw_context_map *map;
65 uint32_t new_scu_conn_id;
66
67 llist_for_each_entry(map, &hnb->map_list, hnb_list) {
68 if (map->state != MAP_S_ACTIVE)
69 continue;
Daniel Willmanndefd2992016-01-14 15:10:05 +010070 if (map->cn_link != cn_if_new) {
71 continue;
72 }
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020073 if (map->rua_ctx_id == rua_ctx_id
74 && map->is_ps == is_ps) {
Harald Welte90256ba2015-12-23 20:16:36 +010075 return map;
76 }
77 }
78
Harald Welte37166a22015-12-24 10:12:09 +010079 if (alloc_cn_conn_id(cn_if_new, &new_scu_conn_id) < 0) {
80 LOGP(DMAIN, LOGL_ERROR, "Unable to allocate CN connection ID\n");
Harald Welte90256ba2015-12-23 20:16:36 +010081 return NULL;
Harald Welte37166a22015-12-24 10:12:09 +010082 }
Harald Welte90256ba2015-12-23 20:16:36 +010083
Harald Welte49287972015-12-29 19:00:35 +010084 LOGP(DMAIN, LOGL_INFO, "Creating new Mapping RUA CTX %p/%u <-> SCU Conn ID %p/%u\n",
85 hnb, rua_ctx_id, cn_if_new, new_scu_conn_id);
86
Harald Welte90256ba2015-12-23 20:16:36 +010087 /* alloate a new map entry */
88 map = talloc_zero(hnb, struct hnbgw_context_map);
89 map->state = MAP_S_NULL;
90 map->cn_link = cn_if_new;
91 map->hnb_ctx = hnb;
92 map->rua_ctx_id = rua_ctx_id;
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020093 map->is_ps = is_ps;
Harald Welte4009b362015-12-26 23:39:05 +010094 map->scu_conn_id = new_scu_conn_id;
Harald Welte90256ba2015-12-23 20:16:36 +010095
96 /* put it into both lists */
97 llist_add_tail(&map->hnb_list, &hnb->map_list);
98 llist_add_tail(&map->cn_list, &cn_if_new->map_list);
99 map->state = MAP_S_ACTIVE;
100
101 return map;
102}
103
104/* Map from a CN + Connection ID to HNB + Context ID */
105struct hnbgw_context_map *
106context_map_by_cn(struct hnbgw_cnlink *cn, uint32_t scu_conn_id)
107{
108 struct hnbgw_context_map *map;
109
Harald Welte90256ba2015-12-23 20:16:36 +0100110 llist_for_each_entry(map, &cn->map_list, cn_list) {
111 if (map->state != MAP_S_ACTIVE)
112 continue;
113 if (map->scu_conn_id == scu_conn_id) {
114 return map;
115 }
116 }
117 /* we don't allocate new mappings in the CN->HNB
118 * direction, as the RUA=SCCP=SUA connections are always
Harald Welte37166a22015-12-24 10:12:09 +0100119 * established from HNB towards CN. */
120 LOGP(DMAIN, LOGL_NOTICE, "Unable to resolve map for CN "
Harald Welte49287972015-12-29 19:00:35 +0100121 "connection ID %p/%u\n", cn, scu_conn_id);
Harald Welte90256ba2015-12-23 20:16:36 +0100122 return NULL;
123}
124
125void context_map_deactivate(struct hnbgw_context_map *map)
126{
127 /* set the state to reserved. We still show up in the list and
128 * avoid re-allocation of the context-id until we are cleaned up
129 * by the context_map garbage collector timer */
130
131 if (map->state != MAP_S_RESERVED2)
132 map->state = MAP_S_RESERVED1;
133}
134
135static struct osmo_timer_list context_map_tmr;
136
137static void context_map_tmr_cb(void *data)
138{
139 struct hnb_gw *gw = data;
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200140 struct hnbgw_cnlink *cn = gw->sccp.cnlink;
141 struct hnbgw_context_map *map;
Harald Welte90256ba2015-12-23 20:16:36 +0100142
Harald Welte37166a22015-12-24 10:12:09 +0100143 DEBUGP(DMAIN, "Running context mapper garbage collection\n");
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200144 llist_for_each_entry(map, &cn->map_list, cn_list) {
145 switch (map->state) {
146 case MAP_S_RESERVED1:
147 /* first time we see this reserved
148 * entry: mark it for stage 2 */
149 map->state = MAP_S_RESERVED2;
150 break;
151 case MAP_S_RESERVED2:
152 /* first time we see this reserved
153 * entry: remove it */
154 map->state = MAP_S_NULL;
155 llist_del(&map->cn_list);
156 llist_del(&map->hnb_list);
157 talloc_free(map);
158 break;
159 default:
160 break;
Harald Welte90256ba2015-12-23 20:16:36 +0100161 }
162 }
163 /* re-schedule this timer */
164 osmo_timer_schedule(&context_map_tmr, EXPIRY_TIMER_SECS, 0);
165}
166
167int context_map_init(struct hnb_gw *gw)
168{
169 context_map_tmr.cb = context_map_tmr_cb;
170 context_map_tmr.data = gw;
171 osmo_timer_schedule(&context_map_tmr, EXPIRY_TIMER_SECS, 0);
172}