jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * OpenGGSN - Gateway GPRS Support Node |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 3 | * Copyright (C) 2002, 2003 Mondru AB. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 4 | * |
| 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 | |
| 33 | struct pdp_t pdpa[PDP_MAX]; /* PDP storage */ |
| 34 | struct pdp_t* hashtid[PDP_MAX];/* Hash table for IMSI + NSAPI */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 35 | /* struct pdp_t* haship[PDP_MAX]; Hash table for IP and network interface */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 36 | |
| 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 | |
| 109 | int pdp_init() { |
| 110 | memset(&pdpa, 0, sizeof(pdpa)); |
| 111 | memset(&hashtid, 0, sizeof(hashtid)); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 112 | /* memset(&haship, 0, sizeof(haship)); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int 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; |
| 128 | (*pdp)->fllc = (uint16_t) n; |
| 129 | (*pdp)->fllu = (uint16_t) n; |
| 130 | (*pdp)->teic_own = (uint32_t) n; |
| 131 | (*pdp)->teic_own = (uint32_t) n; |
| 132 | pdp_tidset(*pdp, pdp_gettid(imsi, nsapi)); |
| 133 | return 0; |
| 134 | } |
| 135 | } |
| 136 | return EOF; /* No more available */ |
| 137 | } |
| 138 | |
| 139 | int pdp_freepdp(struct pdp_t *pdp){ |
| 140 | pdp_tiddel(pdp); |
| 141 | memset(pdp, 0, sizeof(struct pdp_t)); |
| 142 | /* Also need to clean up IP hash tables */ |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | int pdp_getpdp(struct pdp_t **pdp){ |
| 147 | *pdp = &pdpa[0]; |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int pdp_getgtp0(struct pdp_t **pdp, uint16_t fl){ |
| 152 | if (fl>=PDP_MAX) { |
| 153 | return EOF; /* Not found */ |
| 154 | } |
| 155 | else { |
| 156 | *pdp = &pdpa[fl]; |
| 157 | if ((*pdp)->inuse) return 0; |
| 158 | else return EOF; |
| 159 | /* Context exists. We do no further validity checking. */ |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | int pdp_getgtp1(struct pdp_t **pdp, uint32_t teid){ |
| 164 | if (teid>=PDP_MAX) { |
| 165 | return -1; /* Not found */ |
| 166 | } |
| 167 | else { |
| 168 | *pdp = &pdpa[teid]; |
| 169 | return 0; /* We do no validity checking. */ |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | |
| 174 | int pdp_tidhash(uint64_t tid) { |
| 175 | return (lookup(&tid, sizeof(tid), 0) % PDP_MAX); |
| 176 | } |
| 177 | |
| 178 | int pdp_tidset(struct pdp_t *pdp, uint64_t tid) { |
| 179 | int hash = pdp_tidhash(tid); |
| 180 | struct pdp_t *pdp2; |
| 181 | struct pdp_t *pdp_prev = NULL; |
| 182 | if (PDP_DEBUG) printf("Begin pdp_tidset tid = %llx\n", tid); |
jjako | b88f616 | 2003-01-05 18:09:07 +0000 | [diff] [blame] | 183 | pdp->tidnext = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 184 | pdp->tid = tid; |
| 185 | for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) |
| 186 | pdp_prev = pdp2; |
| 187 | if (!pdp_prev) |
| 188 | hashtid[hash] = pdp; |
| 189 | else |
| 190 | pdp_prev->tidnext = pdp; |
| 191 | if (PDP_DEBUG) printf("End pdp_tidset\n"); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | int pdp_tiddel(struct pdp_t *pdp) { |
| 196 | int hash = pdp_tidhash(pdp->tid); |
| 197 | struct pdp_t *pdp2; |
| 198 | struct pdp_t *pdp_prev = NULL; |
| 199 | if (PDP_DEBUG) printf("Begin pdp_tiddel tid = %llx\n", pdp->tid); |
| 200 | for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) { |
| 201 | if (pdp2 == pdp) { |
| 202 | if (!pdp_prev) |
| 203 | hashtid[hash] = pdp2->tidnext; |
| 204 | else |
| 205 | pdp_prev->tidnext = pdp2->tidnext; |
jjako | b88f616 | 2003-01-05 18:09:07 +0000 | [diff] [blame] | 206 | if (PDP_DEBUG) printf("End pdp_tiddel: PDP found\n"); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | pdp_prev = pdp2; |
| 210 | } |
jjako | b88f616 | 2003-01-05 18:09:07 +0000 | [diff] [blame] | 211 | if (PDP_DEBUG) printf("End pdp_tiddel: PDP not found\n"); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 212 | return EOF; /* End of linked list and not found */ |
| 213 | } |
| 214 | |
| 215 | int pdp_tidget(struct pdp_t **pdp, uint64_t tid) { |
| 216 | int hash = pdp_tidhash(tid); |
| 217 | struct pdp_t *pdp2; |
| 218 | if (PDP_DEBUG) printf("Begin pdp_tidget tid = %llx\n", tid); |
| 219 | for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) { |
| 220 | if (pdp2->tid == tid) { |
| 221 | *pdp = pdp2; |
| 222 | if (PDP_DEBUG) printf("Begin pdp_tidget. Found\n"); |
| 223 | return 0; |
| 224 | } |
| 225 | } |
| 226 | if (PDP_DEBUG) printf("Begin pdp_tidget. Not found\n"); |
| 227 | return EOF; /* End of linked list and not found */ |
| 228 | } |
| 229 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 230 | /* |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 231 | int pdp_iphash(void* ipif, struct ul66_t *eua) { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 232 | /#printf("IPhash %ld\n", lookup(eua->v, eua->l, ipif) % PDP_MAX);#/ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 233 | return (lookup(eua->v, eua->l, ipif) % PDP_MAX); |
| 234 | } |
| 235 | |
| 236 | int pdp_ipset(struct pdp_t *pdp, void* ipif, struct ul66_t *eua) { |
| 237 | int hash; |
| 238 | struct pdp_t *pdp2; |
| 239 | struct pdp_t *pdp_prev = NULL; |
| 240 | |
| 241 | if (PDP_DEBUG) printf("Begin pdp_ipset %d %d %2x%2x%2x%2x\n", |
| 242 | (unsigned) ipif, eua->l, |
| 243 | eua->v[2], eua->v[3], |
| 244 | eua->v[4], eua->v[5]); |
| 245 | |
jjako | b88f616 | 2003-01-05 18:09:07 +0000 | [diff] [blame] | 246 | pdp->ipnext = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 247 | pdp->ipif = ipif; |
| 248 | pdp->eua.l = eua->l; |
| 249 | memcpy(pdp->eua.v, eua->v, eua->l); |
| 250 | |
| 251 | hash = pdp_iphash(pdp->ipif, &pdp->eua); |
| 252 | |
| 253 | for (pdp2 = haship[hash]; pdp2; pdp2 = pdp2->ipnext) |
| 254 | pdp_prev = pdp2; |
| 255 | if (!pdp_prev) |
| 256 | haship[hash] = pdp; |
| 257 | else |
| 258 | pdp_prev->ipnext = pdp; |
| 259 | if (PDP_DEBUG) printf("End pdp_ipset\n"); |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | int pdp_ipdel(struct pdp_t *pdp) { |
| 264 | int hash = pdp_iphash(pdp->ipif, &pdp->eua); |
| 265 | struct pdp_t *pdp2; |
| 266 | struct pdp_t *pdp_prev = NULL; |
| 267 | if (PDP_DEBUG) printf("Begin pdp_ipdel\n"); |
| 268 | for (pdp2 = haship[hash]; pdp2; pdp2 = pdp2->ipnext) { |
| 269 | if (pdp2 == pdp) { |
| 270 | if (!pdp_prev) |
| 271 | haship[hash] = pdp2->ipnext; |
| 272 | else |
| 273 | pdp_prev->ipnext = pdp2->ipnext; |
| 274 | if (PDP_DEBUG) printf("End pdp_ipdel: PDP found\n"); |
| 275 | return 0; |
| 276 | } |
| 277 | pdp_prev = pdp2; |
| 278 | } |
| 279 | if (PDP_DEBUG) printf("End pdp_ipdel: PDP not found\n"); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 280 | return EOF; /# End of linked list and not found #/ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | int pdp_ipget(struct pdp_t **pdp, void* ipif, struct ul66_t *eua) { |
| 284 | int hash = pdp_iphash(ipif, eua); |
| 285 | struct pdp_t *pdp2; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 286 | /#printf("Begin pdp_ipget %d %d %2x%2x%2x%2x\n", (unsigned)ipif, eua->l, |
| 287 | eua->v[2],eua->v[3],eua->v[4],eua->v[5]);#/ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 288 | for (pdp2 = haship[hash]; pdp2; pdp2 = pdp2->ipnext) { |
| 289 | if ((pdp2->ipif == ipif) && (pdp2->eua.l == eua->l) && |
| 290 | (memcmp(&pdp2->eua.v, &eua->v, eua->l) == 0)) { |
| 291 | *pdp = pdp2; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 292 | /#printf("End pdp_ipget. Found\n");#/ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | } |
| 296 | if (PDP_DEBUG) printf("End pdp_ipget Notfound %d %d %2x%2x%2x%2x\n", |
| 297 | (unsigned)ipif, eua->l, eua->v[2],eua->v[3],eua->v[4],eua->v[5]); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 298 | return EOF; /# End of linked list and not found #/ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 299 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 300 | */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 301 | /* Various conversion functions */ |
| 302 | |
| 303 | int pdp_ntoeua(struct in_addr *src, struct ul66_t *eua) { |
| 304 | eua->l=6; |
| 305 | eua->v[0]=0xf1; /* IETF */ |
| 306 | eua->v[1]=0x21; /* IPv4 */ |
| 307 | memcpy(&eua->v[2], src, 4); /* Copy a 4 byte address */ |
| 308 | return 0; |
| 309 | } |
| 310 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 311 | int pdp_euaton(struct ul66_t *eua, struct in_addr *dst) { |
| 312 | if((eua->l!=6) || (eua->v[0]!=0xf1) || (eua->v[1]!=0x21)) { |
| 313 | return EOF; |
| 314 | } |
| 315 | memcpy(dst, &eua->v[2], 4); /* Copy a 4 byte address */ |
| 316 | return 0; |
| 317 | } |
| 318 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 319 | uint64_t pdp_gettid(uint64_t imsi, uint8_t nsapi) { |
| 320 | return (imsi & 0x0fffffffffffffff) + ((uint64_t)nsapi << 60); |
| 321 | } |
| 322 | |
| 323 | int ulcpy(void* dst, void* src, size_t size) { |
| 324 | if (((struct ul255_t*)src)->l <= size) { |
| 325 | ((struct ul255_t*)dst)->l = ((struct ul255_t*)src)->l; |
| 326 | memcpy(((struct ul255_t*)dst)->v, ((struct ul255_t*)src)->v, |
| 327 | ((struct ul255_t*)dst)->l); |
| 328 | return 0; |
| 329 | } |
| 330 | else return EOF; |
| 331 | } |