blob: bb886ec7246c9c391f796edd2ac4b308b80d7d30 [file] [log] [blame]
Holger Hans Peter Freyther68c6f882014-09-30 09:10:25 +02001/* Test the SGSN */
2/*
3 * (C) 2014 by Holger Hans Peter Freyther
4 * (C) 2014 by sysmocom s.f.m.c. GmbH
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
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +020022#include <openbsc/gprs_llc.h>
23#include <openbsc/sgsn.h>
Holger Hans Peter Freytherfe921332014-10-02 22:24:47 +020024#include <openbsc/gprs_gmm.h>
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +020025#include <openbsc/debug.h>
26
27#include <osmocom/gsm/gsm_utils.h>
28
29#include <osmocom/core/application.h>
30#include <osmocom/core/msgb.h>
31
Holger Hans Peter Freyther68c6f882014-09-30 09:10:25 +020032#include <stdio.h>
33
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +020034extern void *tall_msgb_ctx;
35
36void *tall_bsc_ctx;
37static struct sgsn_instance sgsn_inst = {
38 .config_file = "osmo_sgsn.cfg",
39 .cfg = {
40 .gtp_statedir = "./",
41 .acl_enabled = 1,
42 },
43};
44struct sgsn_instance *sgsn = &sgsn_inst;
45
46static int count(struct llist_head *head)
47{
48 struct llist_head *cur;
49 int count = 0;
50
51 llist_for_each(cur, head)
52 count += 1;
53
54 return count;
55}
56
Holger Hans Peter Freytherfe921332014-10-02 22:24:47 +020057static struct msgb *create_msg(const uint8_t *data, size_t len)
58{
59 struct msgb *msg = msgb_alloc(len + 8, "test message");
60 msg->l1h = msgb_put(msg, 8);
61 msg->l2h = msgb_put(msg, len);
62 memcpy(msg->l2h, data, len);
63
64 msgb_bcid(msg) = msg->l1h;
65 msgb_gmmh(msg) = msg->l2h;
66 return msg;
67}
68
Jacob Erlbeckabc16a52014-10-27 13:23:49 +010069/*
70 * Create a context and search for it
71 */
72static struct sgsn_mm_ctx *alloc_mm_ctx(uint32_t tlli, struct gprs_ra_id *raid)
73{
74 struct sgsn_mm_ctx *ctx, *ictx;
75 struct gprs_llc_lle *lle;
76 int old_count = count(gprs_llme_list());
77
78 lle = gprs_lle_get_or_create(tlli, 3);
79 ctx = sgsn_mm_ctx_alloc(tlli, raid);
80 ctx->mm_state = GMM_REGISTERED_NORMAL;
81 ctx->llme = lle->llme;
82
83 ictx = sgsn_mm_ctx_by_tlli(tlli, raid);
84 OSMO_ASSERT(ictx == ctx);
85
86 OSMO_ASSERT(count(gprs_llme_list()) == old_count + 1);
87
88 return ctx;
89}
90
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +020091static void test_llme(void)
92{
93 struct gprs_llc_lle *lle, *lle_copy;
94 uint32_t local_tlli;
95 uint32_t foreign_tlli;
96
97 printf("Testing LLME allocations\n");
98 local_tlli = gprs_tmsi2tlli(0x234, TLLI_LOCAL);
99 foreign_tlli = gprs_tmsi2tlli(0x234, TLLI_FOREIGN);
100
101 /* initial state */
102 OSMO_ASSERT(count(gprs_llme_list()) == 0);
103
104 /* Create a new entry */
105 lle = gprs_lle_get_or_create(local_tlli, 3);
106 OSMO_ASSERT(lle);
107 OSMO_ASSERT(count(gprs_llme_list()) == 1);
108
109 /* No new entry is created */
110 lle_copy = gprs_lle_get_or_create(local_tlli, 3);
111 OSMO_ASSERT(lle == lle_copy);
112 OSMO_ASSERT(count(gprs_llme_list()) == 1);
113 lle_copy = gprs_lle_get_or_create(foreign_tlli, 3);
114 OSMO_ASSERT(lle == lle_copy);
115 OSMO_ASSERT(count(gprs_llme_list()) == 1);
116
117 /* unassign which should delete it*/
118 gprs_llgmm_assign(lle->llme, lle->llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
119
120 /* Check that everything was cleaned up */
121 OSMO_ASSERT(count(gprs_llme_list()) == 0);
122}
123
Holger Hans Peter Freytherfe921332014-10-02 22:24:47 +0200124/*
125 * Test that a GMM Detach will remove the MMCTX and the
126 * associated LLME.
127 */
128static void test_gmm_detach(void)
129{
130 struct gprs_ra_id raid = { 0, };
131 struct sgsn_mm_ctx *ctx, *ictx;
Holger Hans Peter Freytherfe921332014-10-02 22:24:47 +0200132 uint32_t local_tlli;
133 struct msgb *msg;
134
135 printf("Testing GMM detach\n");
136
137 /* DTAP - Detach Request (MO) */
138 /* normal detach, power_off = 0 */
139 static const unsigned char detach_req[] = {
140 0x08, 0x05, 0x01, 0x18, 0x05, 0xf4, 0xef, 0xe2,
141 0xb7, 0x00, 0x19, 0x03, 0xb9, 0x97, 0xcb
142 };
143
Holger Hans Peter Freytherfe921332014-10-02 22:24:47 +0200144 local_tlli = gprs_tmsi2tlli(0x23, TLLI_LOCAL);
Holger Hans Peter Freytherfe921332014-10-02 22:24:47 +0200145
Jacob Erlbeckabc16a52014-10-27 13:23:49 +0100146 /* Create a context */
147 OSMO_ASSERT(count(gprs_llme_list()) == 0);
148 ctx = alloc_mm_ctx(local_tlli, &raid);
Holger Hans Peter Freytherfe921332014-10-02 22:24:47 +0200149
150 /* inject the detach */
151 msg = create_msg(detach_req, ARRAY_SIZE(detach_req));
152 msgb_tlli(msg) = local_tlli;
153 gsm0408_gprs_rcvmsg(msg, ctx->llme);
154 msgb_free(msg);
155
156 /* verify that things are gone */
157 OSMO_ASSERT(count(gprs_llme_list()) == 0);
158 ictx = sgsn_mm_ctx_by_tlli(local_tlli, &raid);
Jacob Erlbeck258ce3d2014-09-30 13:51:45 +0200159 OSMO_ASSERT(!ictx);
Holger Hans Peter Freytherfe921332014-10-02 22:24:47 +0200160}
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200161
162static struct log_info_cat gprs_categories[] = {
163 [DMM] = {
164 .name = "DMM",
165 .description = "Layer3 Mobility Management (MM)",
166 .color = "\033[1;33m",
167 .enabled = 1, .loglevel = LOGL_NOTICE,
168 },
169 [DPAG] = {
170 .name = "DPAG",
171 .description = "Paging Subsystem",
172 .color = "\033[1;38m",
173 .enabled = 1, .loglevel = LOGL_NOTICE,
174 },
175 [DMEAS] = {
176 .name = "DMEAS",
177 .description = "Radio Measurement Processing",
178 .enabled = 0, .loglevel = LOGL_NOTICE,
179 },
180 [DREF] = {
181 .name = "DREF",
182 .description = "Reference Counting",
183 .enabled = 0, .loglevel = LOGL_NOTICE,
184 },
185 [DGPRS] = {
186 .name = "DGPRS",
187 .description = "GPRS Packet Service",
188 .enabled = 1, .loglevel = LOGL_DEBUG,
189 },
190 [DNS] = {
191 .name = "DNS",
192 .description = "GPRS Network Service (NS)",
193 .enabled = 1, .loglevel = LOGL_INFO,
194 },
195 [DBSSGP] = {
196 .name = "DBSSGP",
197 .description = "GPRS BSS Gateway Protocol (BSSGP)",
198 .enabled = 1, .loglevel = LOGL_DEBUG,
199 },
200 [DLLC] = {
201 .name = "DLLC",
202 .description = "GPRS Logical Link Control Protocol (LLC)",
203 .enabled = 1, .loglevel = LOGL_DEBUG,
204 },
205 [DSNDCP] = {
206 .name = "DSNDCP",
207 .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
208 .enabled = 1, .loglevel = LOGL_DEBUG,
209 },
210};
211
212static struct log_info info = {
213 .cat = gprs_categories,
214 .num_cat = ARRAY_SIZE(gprs_categories),
215};
216
Holger Hans Peter Freyther68c6f882014-09-30 09:10:25 +0200217int main(int argc, char **argv)
218{
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200219 osmo_init_logging(&info);
220 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
221 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
222
223 test_llme();
Holger Hans Peter Freytherfe921332014-10-02 22:24:47 +0200224 test_gmm_detach();
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200225 printf("Done\n");
Holger Hans Peter Freyther68c6f882014-09-30 09:10:25 +0200226 return 0;
227}
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200228
229
230/* stubs */
231struct osmo_prim_hdr;
232int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
233{
234 abort();
235}