blob: 130ba44ae12dd02bd5236f917f3f17c7b0adb375 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
2 * OpenGGSN - Gateway GPRS Support Node
jjakoa7cd2492003-04-11 09:40:12 +00003 * Copyright (C) 2002, 2003 Mondru AB.
jjako52c24142002-12-16 13:33:51 +00004 *
5 * The contents of this file may be used under the terms of the GNU
6 * General Public License Version 2, provided that the above copyright
7 * notice and this permission notice is included in all copies or
8 * substantial portions of the software.
9 *
10 * The initial developer of the original code is
11 * Jens Jakobsen <jj@openggsn.org>
12 *
13 * Contributor(s):
14 *
15 */
16
17/*
18 * pdp.c:
19 *
20 */
21
22#include <stdio.h>
23#include <sys/types.h>
24#include <netinet/in.h>
25#include <string.h>
26#include "pdp.h"
27#include "lookupa.h"
28
29/* ***********************************************************
30 * Global variables TODO: most should be moved to gsn_t
31 *************************************************************/
32
33struct pdp_t pdpa[PDP_MAX]; /* PDP storage */
34struct pdp_t* hashtid[PDP_MAX];/* Hash table for IMSI + NSAPI */
jjakoa7cd2492003-04-11 09:40:12 +000035/* struct pdp_t* haship[PDP_MAX]; Hash table for IP and network interface */
jjako52c24142002-12-16 13:33:51 +000036
37/* ***********************************************************
38 * Functions related to PDP storage
39 *
40 * Lifecycle
41 * For a GGSN pdp context life begins with the reception of a
42 * create pdp context request. It normally ends with the reception
43 * of a delete pdp context request, but will also end with the
44 * reception of an error indication message.
45 * Provisions should probably be made for terminating pdp contexts
46 * based on either idle timeout, or by sending downlink probe
47 * messages (ping?) to see if the MS is still responding.
48 *
49 * For an SGSN pdp context life begins with the application just
50 * before sending off a create pdp context request. It normally
51 * ends when a delete pdp context response message is received
52 * from the GGSN, but should also end when with the reception of
53 * an error indication message.
54 *
55 *
56 * HASH Tables
57 *
58 * Downlink packets received in the GGSN are identified only by their
59 * network interface together with their destination IP address (Two
60 * network interfaces can use the same private IP address). Each IMSI
61 * (mobile station) can have several PDP contexts using the same IP
62 * address. In this case the traffic flow template (TFT) is used to
63 * determine the correct PDP context for a particular IMSI. Also it
64 * should be possible for each PDP context to use several IP adresses
65 * For fixed wireless access a mobile station might need a full class
66 * C network. Even in the case of several IP adresses the PDP context
67 * should be determined on the basis of the network IP address.
68 * Thus we need a hash table based on network interface + IP address.
69 *
70 * Uplink packets are for GTP0 identified by their IMSI and NSAPI, which
71 * is collectively called the tunnel identifier. There is also a 16 bit
72 * flow label that can be used for identification of uplink packets. This
73 * however is quite useless as it limits the number of contexts to 65536.
74 * For GTP1 uplink packets are identified by a Tunnel Endpoint Identifier
75 * (32 bit), or in some cases by the combination of IMSI and NSAPI.
76 * For GTP1 delete context requests there is a need to find the PDP
77 * contexts with the same IP address. This however can be done by using
78 * the IP hash table.
79 * Thus we need a hash table based on TID (IMSI and NSAPI). The TEID will
80 * be used for directly addressing the PDP context.
81
82 * pdp_newpdp
83 * Gives you a pdp context with no hash references In some way
84 * this should have a limited lifetime.
85 *
86 * pdp_freepdp
87 * Frees a context that was previously allocated with
88 * pdp_newpdp
89 *
90 *
91 * pdp_getpdpIP
92 * An incoming IP packet is uniquely identified by a pointer
93 * to a network connection (void *) and an IP address
94 * (struct in_addr)
95 *
96 * pdp_getpdpGTP
97 * An incoming GTP packet is uniquely identified by a the
98 * TID (imsi + nsapi (8 octets)) in or by the Flow Label
99 * (2 octets) in gtp0 or by the Tunnel Endpoint Identifier
100 * (4 octets) in gtp1.
101 *
102 * This leads to an architecture where the receiving GSN
103 * chooses a Flow Label or a Tunnel Endpoint Identifier
104 * when the connection is setup.
105 * Thus no hash table is needed for GTP lookups.
106 *
107 *************************************************************/
108
109int pdp_init() {
110 memset(&pdpa, 0, sizeof(pdpa));
111 memset(&hashtid, 0, sizeof(hashtid));
jjakoa7cd2492003-04-11 09:40:12 +0000112 /* memset(&haship, 0, sizeof(haship)); */
jjako52c24142002-12-16 13:33:51 +0000113
114 return 0;
115}
116
117int pdp_newpdp(struct pdp_t **pdp, uint64_t imsi, uint8_t nsapi,
118 struct pdp_t *pdp_old){
119 int n;
120 for (n=0; n<PDP_MAX; n++) { /* TODO: Need to do better than linear search */
121 if (pdpa[n].inuse == 0) {
122 *pdp = &pdpa[n];
123 if (NULL != pdp_old) memcpy(*pdp, pdp_old, sizeof(struct pdp_t));
124 else memset(*pdp, 0, sizeof(struct pdp_t));
125 (*pdp)->inuse = 1;
126 (*pdp)->imsi = imsi;
127 (*pdp)->nsapi = nsapi;
jjako2c381332003-10-21 19:09:53 +0000128 (*pdp)->fllc = (uint16_t) n + 1;
129 (*pdp)->fllu = (uint16_t) n + 1;
130 (*pdp)->teid_own = (uint32_t) n + 1;
131 if (!(*pdp)->secondary) (*pdp)->teic_own = (uint32_t) n + 1;
jjako52c24142002-12-16 13:33:51 +0000132 pdp_tidset(*pdp, pdp_gettid(imsi, nsapi));
jjako2c381332003-10-21 19:09:53 +0000133
134 /* Insert reference in primary context */
135 if (((*pdp)->teic_own > 0 ) && ((*pdp)->teic_own <= PDP_MAX)) {
136 pdpa[(*pdp)->teic_own-1].secondary_tei[(*pdp)->nsapi & 0x0f] =
137 (*pdp)->teid_own;
138 }
139
jjako52c24142002-12-16 13:33:51 +0000140 return 0;
141 }
142 }
143 return EOF; /* No more available */
144}
145
146int pdp_freepdp(struct pdp_t *pdp){
147 pdp_tiddel(pdp);
jjako2c381332003-10-21 19:09:53 +0000148
149 /* Remove any references in primary context */
150 if ((pdp->secondary) && (pdp->teic_own > 0 ) && (pdp->teic_own <= PDP_MAX)) {
151 pdpa[pdp->teic_own-1].secondary_tei[pdp->nsapi & 0x0f] = 0;
152 }
153
jjako52c24142002-12-16 13:33:51 +0000154 memset(pdp, 0, sizeof(struct pdp_t));
jjako52c24142002-12-16 13:33:51 +0000155 return 0;
156}
157
158int pdp_getpdp(struct pdp_t **pdp){
159 *pdp = &pdpa[0];
160 return 0;
161}
162
163int pdp_getgtp0(struct pdp_t **pdp, uint16_t fl){
jjako2c381332003-10-21 19:09:53 +0000164 if ((fl>PDP_MAX) || (fl<1)) {
jjako52c24142002-12-16 13:33:51 +0000165 return EOF; /* Not found */
166 }
167 else {
jjako2c381332003-10-21 19:09:53 +0000168 *pdp = &pdpa[fl-1];
jjako52c24142002-12-16 13:33:51 +0000169 if ((*pdp)->inuse) return 0;
170 else return EOF;
171 /* Context exists. We do no further validity checking. */
172 }
173}
174
jjako2c381332003-10-21 19:09:53 +0000175int pdp_getgtp1(struct pdp_t **pdp, uint32_t tei){
176 if ((tei>PDP_MAX) || (tei<1)) {
177 return EOF; /* Not found */
jjako52c24142002-12-16 13:33:51 +0000178 }
179 else {
jjako2c381332003-10-21 19:09:53 +0000180 *pdp = &pdpa[tei-1];
181 if ((*pdp)->inuse) return 0;
182 else return EOF;
183 /* Context exists. We do no further validity checking. */
jjako52c24142002-12-16 13:33:51 +0000184 }
185}
186
187
188int pdp_tidhash(uint64_t tid) {
189 return (lookup(&tid, sizeof(tid), 0) % PDP_MAX);
190}
191
192int pdp_tidset(struct pdp_t *pdp, uint64_t tid) {
193 int hash = pdp_tidhash(tid);
194 struct pdp_t *pdp2;
195 struct pdp_t *pdp_prev = NULL;
196 if (PDP_DEBUG) printf("Begin pdp_tidset tid = %llx\n", tid);
jjakob88f6162003-01-05 18:09:07 +0000197 pdp->tidnext = NULL;
jjako52c24142002-12-16 13:33:51 +0000198 pdp->tid = tid;
199 for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext)
200 pdp_prev = pdp2;
201 if (!pdp_prev)
202 hashtid[hash] = pdp;
203 else
204 pdp_prev->tidnext = pdp;
205 if (PDP_DEBUG) printf("End pdp_tidset\n");
206 return 0;
207}
208
209int pdp_tiddel(struct pdp_t *pdp) {
210 int hash = pdp_tidhash(pdp->tid);
211 struct pdp_t *pdp2;
212 struct pdp_t *pdp_prev = NULL;
213 if (PDP_DEBUG) printf("Begin pdp_tiddel tid = %llx\n", pdp->tid);
214 for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) {
215 if (pdp2 == pdp) {
216 if (!pdp_prev)
217 hashtid[hash] = pdp2->tidnext;
218 else
219 pdp_prev->tidnext = pdp2->tidnext;
jjakob88f6162003-01-05 18:09:07 +0000220 if (PDP_DEBUG) printf("End pdp_tiddel: PDP found\n");
jjako52c24142002-12-16 13:33:51 +0000221 return 0;
222 }
223 pdp_prev = pdp2;
224 }
jjakob88f6162003-01-05 18:09:07 +0000225 if (PDP_DEBUG) printf("End pdp_tiddel: PDP not found\n");
jjako52c24142002-12-16 13:33:51 +0000226 return EOF; /* End of linked list and not found */
227}
228
229int pdp_tidget(struct pdp_t **pdp, uint64_t tid) {
230 int hash = pdp_tidhash(tid);
231 struct pdp_t *pdp2;
232 if (PDP_DEBUG) printf("Begin pdp_tidget tid = %llx\n", tid);
233 for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) {
234 if (pdp2->tid == tid) {
235 *pdp = pdp2;
236 if (PDP_DEBUG) printf("Begin pdp_tidget. Found\n");
237 return 0;
238 }
239 }
240 if (PDP_DEBUG) printf("Begin pdp_tidget. Not found\n");
241 return EOF; /* End of linked list and not found */
242}
243
jjako08d331d2003-10-13 20:33:30 +0000244int pdp_getimsi(struct pdp_t **pdp, uint64_t imsi, uint8_t nsapi) {
245 return pdp_tidget(pdp,
246 (imsi & 0x0fffffffffffffff) + ((uint64_t)nsapi << 60));
247}
248
jjakoa7cd2492003-04-11 09:40:12 +0000249/*
jjako52c24142002-12-16 13:33:51 +0000250int pdp_iphash(void* ipif, struct ul66_t *eua) {
jjakoa7cd2492003-04-11 09:40:12 +0000251 /#printf("IPhash %ld\n", lookup(eua->v, eua->l, ipif) % PDP_MAX);#/
jjako52c24142002-12-16 13:33:51 +0000252 return (lookup(eua->v, eua->l, ipif) % PDP_MAX);
253}
254
255int pdp_ipset(struct pdp_t *pdp, void* ipif, struct ul66_t *eua) {
256 int hash;
257 struct pdp_t *pdp2;
258 struct pdp_t *pdp_prev = NULL;
259
260 if (PDP_DEBUG) printf("Begin pdp_ipset %d %d %2x%2x%2x%2x\n",
261 (unsigned) ipif, eua->l,
262 eua->v[2], eua->v[3],
263 eua->v[4], eua->v[5]);
264
jjakob88f6162003-01-05 18:09:07 +0000265 pdp->ipnext = NULL;
jjako52c24142002-12-16 13:33:51 +0000266 pdp->ipif = ipif;
267 pdp->eua.l = eua->l;
268 memcpy(pdp->eua.v, eua->v, eua->l);
269
270 hash = pdp_iphash(pdp->ipif, &pdp->eua);
271
272 for (pdp2 = haship[hash]; pdp2; pdp2 = pdp2->ipnext)
273 pdp_prev = pdp2;
274 if (!pdp_prev)
275 haship[hash] = pdp;
276 else
277 pdp_prev->ipnext = pdp;
278 if (PDP_DEBUG) printf("End pdp_ipset\n");
279 return 0;
280}
281
282int pdp_ipdel(struct pdp_t *pdp) {
283 int hash = pdp_iphash(pdp->ipif, &pdp->eua);
284 struct pdp_t *pdp2;
285 struct pdp_t *pdp_prev = NULL;
286 if (PDP_DEBUG) printf("Begin pdp_ipdel\n");
287 for (pdp2 = haship[hash]; pdp2; pdp2 = pdp2->ipnext) {
288 if (pdp2 == pdp) {
289 if (!pdp_prev)
290 haship[hash] = pdp2->ipnext;
291 else
292 pdp_prev->ipnext = pdp2->ipnext;
293 if (PDP_DEBUG) printf("End pdp_ipdel: PDP found\n");
294 return 0;
295 }
296 pdp_prev = pdp2;
297 }
298 if (PDP_DEBUG) printf("End pdp_ipdel: PDP not found\n");
jjakoa7cd2492003-04-11 09:40:12 +0000299 return EOF; /# End of linked list and not found #/
jjako52c24142002-12-16 13:33:51 +0000300}
301
302int pdp_ipget(struct pdp_t **pdp, void* ipif, struct ul66_t *eua) {
303 int hash = pdp_iphash(ipif, eua);
304 struct pdp_t *pdp2;
jjakoa7cd2492003-04-11 09:40:12 +0000305 /#printf("Begin pdp_ipget %d %d %2x%2x%2x%2x\n", (unsigned)ipif, eua->l,
306 eua->v[2],eua->v[3],eua->v[4],eua->v[5]);#/
jjako52c24142002-12-16 13:33:51 +0000307 for (pdp2 = haship[hash]; pdp2; pdp2 = pdp2->ipnext) {
308 if ((pdp2->ipif == ipif) && (pdp2->eua.l == eua->l) &&
309 (memcmp(&pdp2->eua.v, &eua->v, eua->l) == 0)) {
310 *pdp = pdp2;
jjakoa7cd2492003-04-11 09:40:12 +0000311 /#printf("End pdp_ipget. Found\n");#/
jjako52c24142002-12-16 13:33:51 +0000312 return 0;
313 }
314 }
315 if (PDP_DEBUG) printf("End pdp_ipget Notfound %d %d %2x%2x%2x%2x\n",
316 (unsigned)ipif, eua->l, eua->v[2],eua->v[3],eua->v[4],eua->v[5]);
jjakoa7cd2492003-04-11 09:40:12 +0000317 return EOF; /# End of linked list and not found #/
jjako52c24142002-12-16 13:33:51 +0000318}
jjakoa7cd2492003-04-11 09:40:12 +0000319*/
jjako52c24142002-12-16 13:33:51 +0000320/* Various conversion functions */
321
322int pdp_ntoeua(struct in_addr *src, struct ul66_t *eua) {
323 eua->l=6;
324 eua->v[0]=0xf1; /* IETF */
325 eua->v[1]=0x21; /* IPv4 */
326 memcpy(&eua->v[2], src, 4); /* Copy a 4 byte address */
327 return 0;
328}
329
jjakoa7cd2492003-04-11 09:40:12 +0000330int pdp_euaton(struct ul66_t *eua, struct in_addr *dst) {
331 if((eua->l!=6) || (eua->v[0]!=0xf1) || (eua->v[1]!=0x21)) {
332 return EOF;
333 }
334 memcpy(dst, &eua->v[2], 4); /* Copy a 4 byte address */
335 return 0;
336}
337
jjako52c24142002-12-16 13:33:51 +0000338uint64_t pdp_gettid(uint64_t imsi, uint8_t nsapi) {
339 return (imsi & 0x0fffffffffffffff) + ((uint64_t)nsapi << 60);
340}
341
342int ulcpy(void* dst, void* src, size_t size) {
343 if (((struct ul255_t*)src)->l <= size) {
344 ((struct ul255_t*)dst)->l = ((struct ul255_t*)src)->l;
345 memcpy(((struct ul255_t*)dst)->v, ((struct ul255_t*)src)->v,
346 ((struct ul255_t*)dst)->l);
347 return 0;
348 }
349 else return EOF;
350}