blob: a89d1586618f6db7441ea562a4be3302c15f606d [file] [log] [blame]
Pau Espin Pedrolffd6e372023-01-05 17:45:25 +01001/* APN contexts */
2
3/* (C) 2009-2015 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010 by On-Waves
5 * (C) 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
6 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <string.h>
25#include <talloc.h>
26
27#include <osmocom/sgsn/apn.h>
Pau Espin Pedrolfd4d4352023-01-05 19:37:05 +010028#include <osmocom/sgsn/sgsn.h>
Pau Espin Pedrolffd6e372023-01-05 17:45:25 +010029
Pau Espin Pedrolffd6e372023-01-05 17:45:25 +010030static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix)
31{
32 struct apn_ctx *actx;
33
Pau Espin Pedrolfd4d4352023-01-05 19:37:05 +010034 actx = talloc_zero(sgsn, struct apn_ctx);
Pau Espin Pedrolffd6e372023-01-05 17:45:25 +010035 if (!actx)
36 return NULL;
37 actx->name = talloc_strdup(actx, ap_name);
38 actx->imsi_prefix = talloc_strdup(actx, imsi_prefix);
39
Pau Espin Pedrolfd4d4352023-01-05 19:37:05 +010040 llist_add_tail(&actx->list, &sgsn->apn_list);
Pau Espin Pedrolffd6e372023-01-05 17:45:25 +010041
42 return actx;
43}
44
45void sgsn_apn_ctx_free(struct apn_ctx *actx)
46{
47 llist_del(&actx->list);
48 talloc_free(actx);
49}
50
51struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi)
52{
53 struct apn_ctx *actx;
54 struct apn_ctx *found_actx = NULL;
55 size_t imsi_prio = 0;
56 size_t name_prio = 0;
57 size_t name_req_len = strlen(name);
58
Pau Espin Pedrolfd4d4352023-01-05 19:37:05 +010059 llist_for_each_entry(actx, &sgsn->apn_list, list) {
Pau Espin Pedrolffd6e372023-01-05 17:45:25 +010060 size_t name_ref_len, imsi_ref_len;
61 const char *name_ref_start, *name_match_start;
62
63 imsi_ref_len = strlen(actx->imsi_prefix);
64 if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0)
65 continue;
66
67 if (imsi_ref_len < imsi_prio)
68 continue;
69
70 /* IMSI matches */
71
72 name_ref_start = &actx->name[0];
73 if (name_ref_start[0] == '*') {
74 /* Suffix match */
75 name_ref_start += 1;
76 name_ref_len = strlen(name_ref_start);
77 if (name_ref_len > name_req_len)
78 continue;
79 } else {
80 name_ref_len = strlen(name_ref_start);
81 if (name_ref_len != name_req_len)
82 continue;
83 }
84
85 name_match_start = name + (name_req_len - name_ref_len);
86 if (strcasecmp(name_match_start, name_ref_start) != 0)
87 continue;
88
89 /* IMSI and name match */
90
91 if (imsi_ref_len == imsi_prio && name_ref_len < name_prio)
92 /* Lower priority, skip */
93 continue;
94
95 imsi_prio = imsi_ref_len;
96 name_prio = name_ref_len;
97 found_actx = actx;
98 }
99 return found_actx;
100}
101
102struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix)
103{
104 struct apn_ctx *actx;
105
Pau Espin Pedrolfd4d4352023-01-05 19:37:05 +0100106 llist_for_each_entry(actx, &sgsn->apn_list, list) {
Pau Espin Pedrolffd6e372023-01-05 17:45:25 +0100107 if (strcasecmp(name, actx->name) == 0 &&
108 strcasecmp(imsi_prefix, actx->imsi_prefix) == 0)
109 return actx;
110 }
111 return NULL;
112}
113
114struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix)
115{
116 struct apn_ctx *actx;
117
118 actx = sgsn_apn_ctx_by_name(name, imsi_prefix);
119 if (!actx)
120 actx = sgsn_apn_ctx_alloc(name, imsi_prefix);
121
122 return actx;
123}