blob: 30ed1d94a7a4b6bcc1d7be6e9fb9fd4e37d8419f [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
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +020069static void test_llme(void)
70{
71 struct gprs_llc_lle *lle, *lle_copy;
72 uint32_t local_tlli;
73 uint32_t foreign_tlli;
74
75 printf("Testing LLME allocations\n");
76 local_tlli = gprs_tmsi2tlli(0x234, TLLI_LOCAL);
77 foreign_tlli = gprs_tmsi2tlli(0x234, TLLI_FOREIGN);
78
79 /* initial state */
80 OSMO_ASSERT(count(gprs_llme_list()) == 0);
81
82 /* Create a new entry */
83 lle = gprs_lle_get_or_create(local_tlli, 3);
84 OSMO_ASSERT(lle);
85 OSMO_ASSERT(count(gprs_llme_list()) == 1);
86
87 /* No new entry is created */
88 lle_copy = gprs_lle_get_or_create(local_tlli, 3);
89 OSMO_ASSERT(lle == lle_copy);
90 OSMO_ASSERT(count(gprs_llme_list()) == 1);
91 lle_copy = gprs_lle_get_or_create(foreign_tlli, 3);
92 OSMO_ASSERT(lle == lle_copy);
93 OSMO_ASSERT(count(gprs_llme_list()) == 1);
94
95 /* unassign which should delete it*/
96 gprs_llgmm_assign(lle->llme, lle->llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
97
98 /* Check that everything was cleaned up */
99 OSMO_ASSERT(count(gprs_llme_list()) == 0);
100}
101
Holger Hans Peter Freytherfe921332014-10-02 22:24:47 +0200102/*
103 * Test that a GMM Detach will remove the MMCTX and the
104 * associated LLME.
105 */
106static void test_gmm_detach(void)
107{
108 struct gprs_ra_id raid = { 0, };
109 struct sgsn_mm_ctx *ctx, *ictx;
110 struct gprs_llc_lle *lle;
111 uint32_t local_tlli;
112 struct msgb *msg;
113
114 printf("Testing GMM detach\n");
115
116 /* DTAP - Detach Request (MO) */
117 /* normal detach, power_off = 0 */
118 static const unsigned char detach_req[] = {
119 0x08, 0x05, 0x01, 0x18, 0x05, 0xf4, 0xef, 0xe2,
120 0xb7, 0x00, 0x19, 0x03, 0xb9, 0x97, 0xcb
121 };
122
123 /* Create a conext and search for it */
124 OSMO_ASSERT(count(gprs_llme_list()) == 0);
125 local_tlli = gprs_tmsi2tlli(0x23, TLLI_LOCAL);
126 lle = gprs_lle_get_or_create(local_tlli, 3);
127 ctx = sgsn_mm_ctx_alloc(local_tlli, &raid);
128 ctx->mm_state = GMM_REGISTERED_NORMAL;
129 ctx->llme = lle->llme;
130
131 ictx = sgsn_mm_ctx_by_tlli(local_tlli, &raid);
132 OSMO_ASSERT(ictx == ctx);
133 OSMO_ASSERT(count(gprs_llme_list()) == 1);
134
135 /* inject the detach */
136 msg = create_msg(detach_req, ARRAY_SIZE(detach_req));
137 msgb_tlli(msg) = local_tlli;
138 gsm0408_gprs_rcvmsg(msg, ctx->llme);
139 msgb_free(msg);
140
141 /* verify that things are gone */
142 OSMO_ASSERT(count(gprs_llme_list()) == 0);
143 ictx = sgsn_mm_ctx_by_tlli(local_tlli, &raid);
144 /* this is still wrong and needs to be changed */
145 OSMO_ASSERT(ictx);
146 OSMO_ASSERT(ictx->llme == lle->llme);
147}
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200148
149static struct log_info_cat gprs_categories[] = {
150 [DMM] = {
151 .name = "DMM",
152 .description = "Layer3 Mobility Management (MM)",
153 .color = "\033[1;33m",
154 .enabled = 1, .loglevel = LOGL_NOTICE,
155 },
156 [DPAG] = {
157 .name = "DPAG",
158 .description = "Paging Subsystem",
159 .color = "\033[1;38m",
160 .enabled = 1, .loglevel = LOGL_NOTICE,
161 },
162 [DMEAS] = {
163 .name = "DMEAS",
164 .description = "Radio Measurement Processing",
165 .enabled = 0, .loglevel = LOGL_NOTICE,
166 },
167 [DREF] = {
168 .name = "DREF",
169 .description = "Reference Counting",
170 .enabled = 0, .loglevel = LOGL_NOTICE,
171 },
172 [DGPRS] = {
173 .name = "DGPRS",
174 .description = "GPRS Packet Service",
175 .enabled = 1, .loglevel = LOGL_DEBUG,
176 },
177 [DNS] = {
178 .name = "DNS",
179 .description = "GPRS Network Service (NS)",
180 .enabled = 1, .loglevel = LOGL_INFO,
181 },
182 [DBSSGP] = {
183 .name = "DBSSGP",
184 .description = "GPRS BSS Gateway Protocol (BSSGP)",
185 .enabled = 1, .loglevel = LOGL_DEBUG,
186 },
187 [DLLC] = {
188 .name = "DLLC",
189 .description = "GPRS Logical Link Control Protocol (LLC)",
190 .enabled = 1, .loglevel = LOGL_DEBUG,
191 },
192 [DSNDCP] = {
193 .name = "DSNDCP",
194 .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
195 .enabled = 1, .loglevel = LOGL_DEBUG,
196 },
197};
198
199static struct log_info info = {
200 .cat = gprs_categories,
201 .num_cat = ARRAY_SIZE(gprs_categories),
202};
203
Holger Hans Peter Freyther68c6f882014-09-30 09:10:25 +0200204int main(int argc, char **argv)
205{
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200206 osmo_init_logging(&info);
207 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
208 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
209
210 test_llme();
Holger Hans Peter Freytherfe921332014-10-02 22:24:47 +0200211 test_gmm_detach();
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200212 printf("Done\n");
Holger Hans Peter Freyther68c6f882014-09-30 09:10:25 +0200213 return 0;
214}
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200215
216
217/* stubs */
218struct osmo_prim_hdr;
219int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
220{
221 abort();
222}