aboutsummaryrefslogtreecommitdiffstats
path: root/src/sgsn/gtp_mme.c
blob: 3d7ec76b600b63407e0d2699a0bdf5553f6fd8dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* TS 29.060 § 7.5.14 RAN Information Management Messages */
/*
 * (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 * All Rights Reserved
 *
 * SPDX-License-Identifier: AGPL-3.0+
 *
 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <talloc.h>

#include <osmocom/sgsn/gtp_mme.h>
#include <osmocom/sgsn/sgsn.h>

static bool _eutran_tai_equal(const struct osmo_eutran_tai *t1, const struct osmo_eutran_tai *t2)
{
	return  t1->mcc == t2->mcc &&
		t1->mnc == t2->mnc &&
		t1->mnc_3_digits == t2->mnc_3_digits &&
		t1->tac == t2->tac;
}

struct sgsn_mme_ctx *sgsn_mme_ctx_alloc(struct sgsn_instance *sgsn, const char *name)
{
	struct sgsn_mme_ctx *mme;
	mme = talloc_zero(sgsn, struct sgsn_mme_ctx);
	if (!mme)
		return NULL;

	/* if we are called from config file parse, this gsn doesn't exist yet */
	mme->sgsn = sgsn;

	mme->name = talloc_strdup(mme, name);

	INIT_LLIST_HEAD(&mme->routes);
	llist_add_tail(&mme->list, &sgsn->mme_list);

	return mme;
}

void sgsn_mme_ctx_free(struct sgsn_mme_ctx *mme)
{
	struct mme_rim_route *rt, *rt2;
	llist_del(&mme->list);

	llist_for_each_entry_safe(rt, rt2, &mme->routes, list) {
		llist_del(&rt->list);
		talloc_free(rt);
	}

	talloc_free(mme);
}

struct sgsn_mme_ctx *sgsn_mme_ctx_find_alloc(struct sgsn_instance *sgsn, const char *name)
{
	struct sgsn_mme_ctx *mme;

	mme = sgsn_mme_ctx_by_name(sgsn, name);
	if (!mme)
		mme = sgsn_mme_ctx_alloc(sgsn, name);
	return mme;
}

void sgsn_mme_ctx_route_add(struct sgsn_mme_ctx *mme, const struct osmo_eutran_tai *tai)
{
	struct mme_rim_route *rt = talloc_zero(mme, struct mme_rim_route);
	rt->tai = *tai;
	llist_add_tail(&rt->list, &mme->routes);
}

void sgsn_mme_ctx_route_del(struct sgsn_mme_ctx *mme, const struct osmo_eutran_tai *tai)
{
	struct mme_rim_route *rt;

	llist_for_each_entry(rt, &mme->routes, list) {
		if (_eutran_tai_equal(tai, &rt->tai)) {
			llist_del(&rt->list);
			talloc_free(rt);
			return;
		}
	}
}

struct sgsn_mme_ctx *sgsn_mme_ctx_by_name(const struct sgsn_instance *sgsn, const char *name)
{
	struct sgsn_mme_ctx *mme;

	llist_for_each_entry(mme, &sgsn->mme_list, list) {
		if (!strcmp(name, mme->name))
			return mme;
	}
	return NULL;
}

struct sgsn_mme_ctx *sgsn_mme_ctx_by_addr(const struct sgsn_instance *sgsn, const struct in_addr *addr)
{
	struct sgsn_mme_ctx *mme;

	llist_for_each_entry(mme, &sgsn->mme_list, list) {
		if (!memcmp(addr, &mme->remote_addr, sizeof(*addr)))
			return mme;
	}
	return NULL;
}

struct sgsn_mme_ctx *sgsn_mme_ctx_by_route(const struct sgsn_instance *sgsn, const struct osmo_eutran_tai *tai)
{
	struct sgsn_mme_ctx *mme;
	llist_for_each_entry(mme, &sgsn->mme_list, list) {
		struct mme_rim_route *rt;
		llist_for_each_entry(rt, &mme->routes, list) {
			if (_eutran_tai_equal(tai, &rt->tai)) {
				return mme;
			}
		}
	}
	return NULL;
}

struct sgsn_mme_ctx *sgsn_mme_ctx_by_default_route(const struct sgsn_instance *sgsn)
{
	struct sgsn_mme_ctx *mme;

	llist_for_each_entry(mme, &sgsn->mme_list, list) {
		if (mme->default_route)
			return mme;
	}
	return NULL;
}