Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 1 | /* |
Harald Welte | 632e843 | 2017-09-05 18:12:14 +0200 | [diff] [blame] | 2 | * OsmoGGSN - Gateway GPRS Support Node |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 3 | * Copyright (C) 2002, 2003, 2004 Mondru AB. |
Harald Welte | 632e843 | 2017-09-05 18:12:14 +0200 | [diff] [blame] | 4 | * Copyright (C) 2017 Harald Welte <laforge@gnumonks.org> |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 5 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 6 | * The contents of this file may be used under the terms of the GNU |
| 7 | * General Public License Version 2, provided that the above copyright |
| 8 | * notice and this permission notice is included in all copies or |
| 9 | * substantial portions of the software. |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 10 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 14 | * pdp.c: |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 15 | * |
| 16 | */ |
| 17 | |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 18 | #include <../config.h> |
| 19 | |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 20 | #include <osmocom/core/logging.h> |
| 21 | |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 22 | #ifdef HAVE_STDINT_H |
| 23 | #include <stdint.h> |
| 24 | #endif |
| 25 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <netinet/in.h> |
| 29 | #include <string.h> |
Alexander Huemer | db852a1 | 2015-11-06 20:59:01 +0100 | [diff] [blame] | 30 | #include <inttypes.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 31 | #include "pdp.h" |
Harald Welte | 37d5b15 | 2017-08-12 23:58:29 +0200 | [diff] [blame] | 32 | #include "gtp.h" |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 33 | #include "lookupa.h" |
| 34 | |
| 35 | /* *********************************************************** |
| 36 | * Global variables TODO: most should be moved to gsn_t |
| 37 | *************************************************************/ |
| 38 | |
Holger Hans Peter Freyther | d7566b8 | 2012-11-06 14:17:45 +0100 | [diff] [blame] | 39 | static struct pdp_t pdpa[PDP_MAX]; /* PDP storage */ |
| 40 | static struct pdp_t *hashtid[PDP_MAX]; /* Hash table for IMSI + NSAPI */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 41 | /* struct pdp_t* haship[PDP_MAX]; Hash table for IP and network interface */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 42 | |
| 43 | /* *********************************************************** |
| 44 | * Functions related to PDP storage |
| 45 | * |
| 46 | * Lifecycle |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 47 | * For a GGSN pdp context life begins with the reception of a |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 48 | * create pdp context request. It normally ends with the reception |
| 49 | * of a delete pdp context request, but will also end with the |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 50 | * reception of an error indication message. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 51 | * Provisions should probably be made for terminating pdp contexts |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 52 | * based on either idle timeout, or by sending downlink probe |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 53 | * messages (ping?) to see if the MS is still responding. |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 54 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 55 | * For an SGSN pdp context life begins with the application just |
| 56 | * before sending off a create pdp context request. It normally |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 57 | * ends when a delete pdp context response message is received |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 58 | * from the GGSN, but should also end when with the reception of |
| 59 | * an error indication message. |
| 60 | * |
| 61 | * |
| 62 | * HASH Tables |
| 63 | * |
| 64 | * Downlink packets received in the GGSN are identified only by their |
| 65 | * network interface together with their destination IP address (Two |
| 66 | * network interfaces can use the same private IP address). Each IMSI |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 67 | * (mobile station) can have several PDP contexts using the same IP |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 68 | * address. In this case the traffic flow template (TFT) is used to |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 69 | * determine the correct PDP context for a particular IMSI. Also it |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 70 | * should be possible for each PDP context to use several IP adresses |
| 71 | * For fixed wireless access a mobile station might need a full class |
| 72 | * C network. Even in the case of several IP adresses the PDP context |
| 73 | * should be determined on the basis of the network IP address. |
| 74 | * Thus we need a hash table based on network interface + IP address. |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 75 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 76 | * Uplink packets are for GTP0 identified by their IMSI and NSAPI, which |
| 77 | * is collectively called the tunnel identifier. There is also a 16 bit |
| 78 | * flow label that can be used for identification of uplink packets. This |
| 79 | * however is quite useless as it limits the number of contexts to 65536. |
| 80 | * For GTP1 uplink packets are identified by a Tunnel Endpoint Identifier |
| 81 | * (32 bit), or in some cases by the combination of IMSI and NSAPI. |
| 82 | * For GTP1 delete context requests there is a need to find the PDP |
| 83 | * contexts with the same IP address. This however can be done by using |
| 84 | * the IP hash table. |
| 85 | * Thus we need a hash table based on TID (IMSI and NSAPI). The TEID will |
| 86 | * be used for directly addressing the PDP context. |
| 87 | |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 88 | * pdp_newpdp |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 89 | * Gives you a pdp context with no hash references In some way |
| 90 | * this should have a limited lifetime. |
| 91 | * |
| 92 | * pdp_freepdp |
| 93 | * Frees a context that was previously allocated with |
| 94 | * pdp_newpdp |
| 95 | * |
| 96 | * |
| 97 | * pdp_getpdpIP |
| 98 | * An incoming IP packet is uniquely identified by a pointer |
| 99 | * to a network connection (void *) and an IP address |
| 100 | * (struct in_addr) |
| 101 | * |
| 102 | * pdp_getpdpGTP |
| 103 | * An incoming GTP packet is uniquely identified by a the |
| 104 | * TID (imsi + nsapi (8 octets)) in or by the Flow Label |
| 105 | * (2 octets) in gtp0 or by the Tunnel Endpoint Identifier |
| 106 | * (4 octets) in gtp1. |
| 107 | * |
| 108 | * This leads to an architecture where the receiving GSN |
| 109 | * chooses a Flow Label or a Tunnel Endpoint Identifier |
| 110 | * when the connection is setup. |
| 111 | * Thus no hash table is needed for GTP lookups. |
| 112 | * |
| 113 | *************************************************************/ |
| 114 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 115 | int pdp_init() |
| 116 | { |
| 117 | memset(&pdpa, 0, sizeof(pdpa)); |
| 118 | memset(&hashtid, 0, sizeof(hashtid)); |
| 119 | /* memset(&haship, 0, sizeof(haship)); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 120 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 121 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 124 | int pdp_newpdp(struct pdp_t **pdp, uint64_t imsi, uint8_t nsapi, |
| 125 | struct pdp_t *pdp_old) |
| 126 | { |
| 127 | int n; |
| 128 | for (n = 0; n < PDP_MAX; n++) { /* TODO: Need to do better than linear search */ |
| 129 | if (pdpa[n].inuse == 0) { |
| 130 | *pdp = &pdpa[n]; |
| 131 | if (NULL != pdp_old) |
| 132 | memcpy(*pdp, pdp_old, sizeof(struct pdp_t)); |
| 133 | else |
| 134 | memset(*pdp, 0, sizeof(struct pdp_t)); |
| 135 | (*pdp)->inuse = 1; |
| 136 | (*pdp)->imsi = imsi; |
| 137 | (*pdp)->nsapi = nsapi; |
| 138 | (*pdp)->fllc = (uint16_t) n + 1; |
| 139 | (*pdp)->fllu = (uint16_t) n + 1; |
| 140 | (*pdp)->teid_own = (uint32_t) n + 1; |
| 141 | if (!(*pdp)->secondary) |
| 142 | (*pdp)->teic_own = (uint32_t) n + 1; |
| 143 | pdp_tidset(*pdp, pdp_gettid(imsi, nsapi)); |
| 144 | |
| 145 | /* Insert reference in primary context */ |
| 146 | if (((*pdp)->teic_own > 0) |
| 147 | && ((*pdp)->teic_own <= PDP_MAX)) { |
| 148 | pdpa[(*pdp)->teic_own - |
| 149 | 1].secondary_tei[(*pdp)->nsapi & 0x0f] = |
| 150 | (*pdp)->teid_own; |
| 151 | } |
Harald Welte | 3c1cce2 | 2017-09-24 16:40:12 +0800 | [diff] [blame] | 152 | /* Default: Generate G-PDU sequence numbers on Tx */ |
| 153 | (*pdp)->tx_gpdu_seq = true; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | } |
| 158 | return EOF; /* No more available */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 161 | int pdp_freepdp(struct pdp_t *pdp) |
| 162 | { |
| 163 | pdp_tiddel(pdp); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 164 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 165 | /* Remove any references in primary context */ |
| 166 | if ((pdp->secondary) && (pdp->teic_own > 0) |
| 167 | && (pdp->teic_own <= PDP_MAX)) { |
| 168 | pdpa[pdp->teic_own - 1].secondary_tei[pdp->nsapi & 0x0f] = 0; |
| 169 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 170 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 171 | memset(pdp, 0, sizeof(struct pdp_t)); |
| 172 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 175 | int pdp_getpdp(struct pdp_t **pdp) |
| 176 | { |
| 177 | *pdp = &pdpa[0]; |
| 178 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 181 | int pdp_getgtp0(struct pdp_t **pdp, uint16_t fl) |
| 182 | { |
| 183 | if ((fl > PDP_MAX) || (fl < 1)) { |
| 184 | return EOF; /* Not found */ |
| 185 | } else { |
| 186 | *pdp = &pdpa[fl - 1]; |
| 187 | if ((*pdp)->inuse) |
| 188 | return 0; |
| 189 | else |
| 190 | return EOF; |
| 191 | /* Context exists. We do no further validity checking. */ |
| 192 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 195 | int pdp_getgtp1(struct pdp_t **pdp, uint32_t tei) |
| 196 | { |
| 197 | if ((tei > PDP_MAX) || (tei < 1)) { |
| 198 | return EOF; /* Not found */ |
| 199 | } else { |
| 200 | *pdp = &pdpa[tei - 1]; |
| 201 | if ((*pdp)->inuse) |
| 202 | return 0; |
| 203 | else |
| 204 | return EOF; |
| 205 | /* Context exists. We do no further validity checking. */ |
| 206 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Harald Welte | 37d5b15 | 2017-08-12 23:58:29 +0200 | [diff] [blame] | 209 | /* get a PDP based on the *peer* address + TEI-Data. Used for matching inbound Error Ind */ |
| 210 | int pdp_getgtp1_peer_d(struct pdp_t **pdp, const struct sockaddr_in *peer, uint32_t teid_gn) |
| 211 | { |
| 212 | unsigned int i; |
| 213 | |
| 214 | /* this is O(n) but we don't have (nor want) another hash... */ |
| 215 | for (i = 0; i < PDP_MAX; i++) { |
| 216 | struct pdp_t *candidate = &pdpa[i]; |
| 217 | if (candidate->inuse && candidate->teid_gn == teid_gn && |
| 218 | candidate->gsnru.l == sizeof(peer->sin_addr) && |
| 219 | !memcmp(&peer->sin_addr, candidate->gsnru.v, sizeof(peer->sin_addr))) { |
| 220 | *pdp = &pdpa[i]; |
| 221 | return 0; |
| 222 | } |
| 223 | } |
| 224 | return EOF; |
| 225 | } |
| 226 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 227 | int pdp_tidhash(uint64_t tid) |
| 228 | { |
| 229 | return (lookup(&tid, sizeof(tid), 0) % PDP_MAX); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 232 | int pdp_tidset(struct pdp_t *pdp, uint64_t tid) |
| 233 | { |
| 234 | int hash = pdp_tidhash(tid); |
| 235 | struct pdp_t *pdp2; |
| 236 | struct pdp_t *pdp_prev = NULL; |
Alexander Huemer | db852a1 | 2015-11-06 20:59:01 +0100 | [diff] [blame] | 237 | DEBUGP(DLGTP, "Begin pdp_tidset tid = %"PRIx64"\n", tid); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 238 | pdp->tidnext = NULL; |
| 239 | pdp->tid = tid; |
| 240 | for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) |
| 241 | pdp_prev = pdp2; |
| 242 | if (!pdp_prev) |
| 243 | hashtid[hash] = pdp; |
| 244 | else |
| 245 | pdp_prev->tidnext = pdp; |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 246 | DEBUGP(DLGTP, "End pdp_tidset\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 247 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 250 | int pdp_tiddel(struct pdp_t *pdp) |
| 251 | { |
| 252 | int hash = pdp_tidhash(pdp->tid); |
| 253 | struct pdp_t *pdp2; |
| 254 | struct pdp_t *pdp_prev = NULL; |
Alexander Huemer | db852a1 | 2015-11-06 20:59:01 +0100 | [diff] [blame] | 255 | DEBUGP(DLGTP, "Begin pdp_tiddel tid = %"PRIx64"\n", pdp->tid); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 256 | for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) { |
| 257 | if (pdp2 == pdp) { |
| 258 | if (!pdp_prev) |
| 259 | hashtid[hash] = pdp2->tidnext; |
| 260 | else |
| 261 | pdp_prev->tidnext = pdp2->tidnext; |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 262 | DEBUGP(DLGTP, "End pdp_tiddel: PDP found\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | pdp_prev = pdp2; |
| 266 | } |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 267 | DEBUGP(DLGTP, "End pdp_tiddel: PDP not found\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 268 | return EOF; /* End of linked list and not found */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 271 | int pdp_tidget(struct pdp_t **pdp, uint64_t tid) |
| 272 | { |
| 273 | int hash = pdp_tidhash(tid); |
| 274 | struct pdp_t *pdp2; |
Alexander Huemer | db852a1 | 2015-11-06 20:59:01 +0100 | [diff] [blame] | 275 | DEBUGP(DLGTP, "Begin pdp_tidget tid = %"PRIx64"\n", tid); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 276 | for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) { |
| 277 | if (pdp2->tid == tid) { |
| 278 | *pdp = pdp2; |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 279 | DEBUGP(DLGTP, "Begin pdp_tidget. Found\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | } |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 283 | DEBUGP(DLGTP, "Begin pdp_tidget. Not found\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 284 | return EOF; /* End of linked list and not found */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 287 | int pdp_getimsi(struct pdp_t **pdp, uint64_t imsi, uint8_t nsapi) |
| 288 | { |
| 289 | return pdp_tidget(pdp, |
| 290 | (imsi & 0x0fffffffffffffffull) + |
| 291 | ((uint64_t) nsapi << 60)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 292 | } |
| 293 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 294 | /* |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 295 | int pdp_iphash(void* ipif, struct ul66_t *eua) { |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 296 | /#printf("IPhash %ld\n", lookup(eua->v, eua->l, ipif) % PDP_MAX);#/ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 297 | return (lookup(eua->v, eua->l, ipif) % PDP_MAX); |
| 298 | } |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 299 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 300 | int pdp_ipset(struct pdp_t *pdp, void* ipif, struct ul66_t *eua) { |
| 301 | int hash; |
| 302 | struct pdp_t *pdp2; |
| 303 | struct pdp_t *pdp_prev = NULL; |
| 304 | |
| 305 | if (PDP_DEBUG) printf("Begin pdp_ipset %d %d %2x%2x%2x%2x\n", |
| 306 | (unsigned) ipif, eua->l, |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 307 | eua->v[2], eua->v[3], |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 308 | eua->v[4], eua->v[5]); |
| 309 | |
jjako | b88f616 | 2003-01-05 18:09:07 +0000 | [diff] [blame] | 310 | pdp->ipnext = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 311 | pdp->ipif = ipif; |
| 312 | pdp->eua.l = eua->l; |
| 313 | memcpy(pdp->eua.v, eua->v, eua->l); |
| 314 | |
| 315 | hash = pdp_iphash(pdp->ipif, &pdp->eua); |
| 316 | |
| 317 | for (pdp2 = haship[hash]; pdp2; pdp2 = pdp2->ipnext) |
| 318 | pdp_prev = pdp2; |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 319 | if (!pdp_prev) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 320 | haship[hash] = pdp; |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 321 | else |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 322 | pdp_prev->ipnext = pdp; |
| 323 | if (PDP_DEBUG) printf("End pdp_ipset\n"); |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | int pdp_ipdel(struct pdp_t *pdp) { |
| 328 | int hash = pdp_iphash(pdp->ipif, &pdp->eua); |
| 329 | struct pdp_t *pdp2; |
| 330 | struct pdp_t *pdp_prev = NULL; |
| 331 | if (PDP_DEBUG) printf("Begin pdp_ipdel\n"); |
| 332 | for (pdp2 = haship[hash]; pdp2; pdp2 = pdp2->ipnext) { |
| 333 | if (pdp2 == pdp) { |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 334 | if (!pdp_prev) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 335 | haship[hash] = pdp2->ipnext; |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 336 | else |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 337 | pdp_prev->ipnext = pdp2->ipnext; |
| 338 | if (PDP_DEBUG) printf("End pdp_ipdel: PDP found\n"); |
| 339 | return 0; |
| 340 | } |
| 341 | pdp_prev = pdp2; |
| 342 | } |
| 343 | if (PDP_DEBUG) printf("End pdp_ipdel: PDP not found\n"); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 344 | return EOF; /# End of linked list and not found #/ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | int pdp_ipget(struct pdp_t **pdp, void* ipif, struct ul66_t *eua) { |
| 348 | int hash = pdp_iphash(ipif, eua); |
| 349 | struct pdp_t *pdp2; |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 350 | /#printf("Begin pdp_ipget %d %d %2x%2x%2x%2x\n", (unsigned)ipif, eua->l, |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 351 | eua->v[2],eua->v[3],eua->v[4],eua->v[5]);#/ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 352 | for (pdp2 = haship[hash]; pdp2; pdp2 = pdp2->ipnext) { |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 353 | if ((pdp2->ipif == ipif) && (pdp2->eua.l == eua->l) && |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 354 | (memcmp(&pdp2->eua.v, &eua->v, eua->l) == 0)) { |
| 355 | *pdp = pdp2; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 356 | /#printf("End pdp_ipget. Found\n");#/ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | } |
Pau Espin Pedrol | 5f5fcff | 2018-01-25 18:09:02 +0100 | [diff] [blame] | 360 | if (PDP_DEBUG) printf("End pdp_ipget Notfound %d %d %2x%2x%2x%2x\n", |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 361 | (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] | 362 | return EOF; /# End of linked list and not found #/ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 363 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 364 | */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 365 | /* Various conversion functions */ |
| 366 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 367 | uint64_t pdp_gettid(uint64_t imsi, uint8_t nsapi) |
| 368 | { |
| 369 | return (imsi & 0x0fffffffffffffffull) + ((uint64_t) nsapi << 60); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Pablo Neira Ayuso | 746b944 | 2014-03-24 17:58:27 +0100 | [diff] [blame] | 372 | void pdp_set_imsi_nsapi(struct pdp_t *pdp, uint64_t teid) |
| 373 | { |
| 374 | pdp->imsi = teid & 0x0fffffffffffffffull; |
| 375 | pdp->nsapi = (teid & 0xf000000000000000ull) >> 60; |
| 376 | } |
Pau Espin Pedrol | 9ee8d32 | 2019-05-30 14:11:22 +0200 | [diff] [blame] | 377 | |
| 378 | /* Count amount of secondary PDP contexts linked to this primary PDP context |
| 379 | * (itself included). Must be called on a primary PDP context. */ |
| 380 | unsigned int pdp_count_secondary(struct pdp_t *pdp) |
| 381 | { |
| 382 | unsigned int n; |
| 383 | unsigned int count = 0; |
| 384 | OSMO_ASSERT(!pdp->secondary); |
| 385 | |
| 386 | for (n = 0; n < PDP_MAXNSAPI; n++) |
| 387 | if (pdp->secondary_tei[n]) |
| 388 | count++; |
| 389 | return count; |
| 390 | } |