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