diff options
Diffstat (limited to 'gtp/pdp.c')
-rw-r--r-- | gtp/pdp.c | 309 |
1 files changed, 170 insertions, 139 deletions
@@ -31,8 +31,8 @@ * Global variables TODO: most should be moved to gsn_t *************************************************************/ -struct pdp_t pdpa[PDP_MAX]; /* PDP storage */ -struct pdp_t* hashtid[PDP_MAX];/* Hash table for IMSI + NSAPI */ +struct pdp_t pdpa[PDP_MAX]; /* PDP storage */ +struct pdp_t *hashtid[PDP_MAX]; /* Hash table for IMSI + NSAPI */ /* struct pdp_t* haship[PDP_MAX]; Hash table for IP and network interface */ /* *********************************************************** @@ -107,144 +107,171 @@ struct pdp_t* hashtid[PDP_MAX];/* Hash table for IMSI + NSAPI */ * *************************************************************/ -int pdp_init() { - memset(&pdpa, 0, sizeof(pdpa)); - memset(&hashtid, 0, sizeof(hashtid)); - /* memset(&haship, 0, sizeof(haship)); */ +int pdp_init() +{ + memset(&pdpa, 0, sizeof(pdpa)); + memset(&hashtid, 0, sizeof(hashtid)); + /* memset(&haship, 0, sizeof(haship)); */ - return 0; + return 0; } -int pdp_newpdp(struct pdp_t **pdp, uint64_t imsi, uint8_t nsapi, - struct pdp_t *pdp_old){ - int n; - for (n=0; n<PDP_MAX; n++) { /* TODO: Need to do better than linear search */ - if (pdpa[n].inuse == 0) { - *pdp = &pdpa[n]; - if (NULL != pdp_old) memcpy(*pdp, pdp_old, sizeof(struct pdp_t)); - else memset(*pdp, 0, sizeof(struct pdp_t)); - (*pdp)->inuse = 1; - (*pdp)->imsi = imsi; - (*pdp)->nsapi = nsapi; - (*pdp)->fllc = (uint16_t) n + 1; - (*pdp)->fllu = (uint16_t) n + 1; - (*pdp)->teid_own = (uint32_t) n + 1; - if (!(*pdp)->secondary) (*pdp)->teic_own = (uint32_t) n + 1; - pdp_tidset(*pdp, pdp_gettid(imsi, nsapi)); - - /* Insert reference in primary context */ - if (((*pdp)->teic_own > 0 ) && ((*pdp)->teic_own <= PDP_MAX)) { - pdpa[(*pdp)->teic_own-1].secondary_tei[(*pdp)->nsapi & 0x0f] = - (*pdp)->teid_own; - } - - return 0; - } - } - return EOF; /* No more available */ +int pdp_newpdp(struct pdp_t **pdp, uint64_t imsi, uint8_t nsapi, + struct pdp_t *pdp_old) +{ + int n; + for (n = 0; n < PDP_MAX; n++) { /* TODO: Need to do better than linear search */ + if (pdpa[n].inuse == 0) { + *pdp = &pdpa[n]; + if (NULL != pdp_old) + memcpy(*pdp, pdp_old, sizeof(struct pdp_t)); + else + memset(*pdp, 0, sizeof(struct pdp_t)); + (*pdp)->inuse = 1; + (*pdp)->imsi = imsi; + (*pdp)->nsapi = nsapi; + (*pdp)->fllc = (uint16_t) n + 1; + (*pdp)->fllu = (uint16_t) n + 1; + (*pdp)->teid_own = (uint32_t) n + 1; + if (!(*pdp)->secondary) + (*pdp)->teic_own = (uint32_t) n + 1; + pdp_tidset(*pdp, pdp_gettid(imsi, nsapi)); + + /* Insert reference in primary context */ + if (((*pdp)->teic_own > 0) + && ((*pdp)->teic_own <= PDP_MAX)) { + pdpa[(*pdp)->teic_own - + 1].secondary_tei[(*pdp)->nsapi & 0x0f] = + (*pdp)->teid_own; + } + + return 0; + } + } + return EOF; /* No more available */ } -int pdp_freepdp(struct pdp_t *pdp){ - pdp_tiddel(pdp); +int pdp_freepdp(struct pdp_t *pdp) +{ + pdp_tiddel(pdp); - /* Remove any references in primary context */ - if ((pdp->secondary) && (pdp->teic_own > 0 ) && (pdp->teic_own <= PDP_MAX)) { - pdpa[pdp->teic_own-1].secondary_tei[pdp->nsapi & 0x0f] = 0; - } + /* Remove any references in primary context */ + if ((pdp->secondary) && (pdp->teic_own > 0) + && (pdp->teic_own <= PDP_MAX)) { + pdpa[pdp->teic_own - 1].secondary_tei[pdp->nsapi & 0x0f] = 0; + } - memset(pdp, 0, sizeof(struct pdp_t)); - return 0; + memset(pdp, 0, sizeof(struct pdp_t)); + return 0; } -int pdp_getpdp(struct pdp_t **pdp){ - *pdp = &pdpa[0]; - return 0; +int pdp_getpdp(struct pdp_t **pdp) +{ + *pdp = &pdpa[0]; + return 0; } -int pdp_getgtp0(struct pdp_t **pdp, uint16_t fl){ - if ((fl>PDP_MAX) || (fl<1)) { - return EOF; /* Not found */ - } - else { - *pdp = &pdpa[fl-1]; - if ((*pdp)->inuse) return 0; - else return EOF; - /* Context exists. We do no further validity checking. */ - } +int pdp_getgtp0(struct pdp_t **pdp, uint16_t fl) +{ + if ((fl > PDP_MAX) || (fl < 1)) { + return EOF; /* Not found */ + } else { + *pdp = &pdpa[fl - 1]; + if ((*pdp)->inuse) + return 0; + else + return EOF; + /* Context exists. We do no further validity checking. */ + } } -int pdp_getgtp1(struct pdp_t **pdp, uint32_t tei){ - if ((tei>PDP_MAX) || (tei<1)) { - return EOF; /* Not found */ - } - else { - *pdp = &pdpa[tei-1]; - if ((*pdp)->inuse) return 0; - else return EOF; - /* Context exists. We do no further validity checking. */ - } +int pdp_getgtp1(struct pdp_t **pdp, uint32_t tei) +{ + if ((tei > PDP_MAX) || (tei < 1)) { + return EOF; /* Not found */ + } else { + *pdp = &pdpa[tei - 1]; + if ((*pdp)->inuse) + return 0; + else + return EOF; + /* Context exists. We do no further validity checking. */ + } } - -int pdp_tidhash(uint64_t tid) { - return (lookup(&tid, sizeof(tid), 0) % PDP_MAX); +int pdp_tidhash(uint64_t tid) +{ + return (lookup(&tid, sizeof(tid), 0) % PDP_MAX); } -int pdp_tidset(struct pdp_t *pdp, uint64_t tid) { - int hash = pdp_tidhash(tid); - struct pdp_t *pdp2; - struct pdp_t *pdp_prev = NULL; - if (PDP_DEBUG) printf("Begin pdp_tidset tid = %llx\n", tid); - pdp->tidnext = NULL; - pdp->tid = tid; - for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) - pdp_prev = pdp2; - if (!pdp_prev) - hashtid[hash] = pdp; - else - pdp_prev->tidnext = pdp; - if (PDP_DEBUG) printf("End pdp_tidset\n"); - return 0; +int pdp_tidset(struct pdp_t *pdp, uint64_t tid) +{ + int hash = pdp_tidhash(tid); + struct pdp_t *pdp2; + struct pdp_t *pdp_prev = NULL; + if (PDP_DEBUG) + printf("Begin pdp_tidset tid = %llx\n", tid); + pdp->tidnext = NULL; + pdp->tid = tid; + for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) + pdp_prev = pdp2; + if (!pdp_prev) + hashtid[hash] = pdp; + else + pdp_prev->tidnext = pdp; + if (PDP_DEBUG) + printf("End pdp_tidset\n"); + return 0; } -int pdp_tiddel(struct pdp_t *pdp) { - int hash = pdp_tidhash(pdp->tid); - struct pdp_t *pdp2; - struct pdp_t *pdp_prev = NULL; - if (PDP_DEBUG) printf("Begin pdp_tiddel tid = %llx\n", pdp->tid); - for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) { - if (pdp2 == pdp) { - if (!pdp_prev) - hashtid[hash] = pdp2->tidnext; - else - pdp_prev->tidnext = pdp2->tidnext; - if (PDP_DEBUG) printf("End pdp_tiddel: PDP found\n"); - return 0; - } - pdp_prev = pdp2; - } - if (PDP_DEBUG) printf("End pdp_tiddel: PDP not found\n"); - return EOF; /* End of linked list and not found */ +int pdp_tiddel(struct pdp_t *pdp) +{ + int hash = pdp_tidhash(pdp->tid); + struct pdp_t *pdp2; + struct pdp_t *pdp_prev = NULL; + if (PDP_DEBUG) + printf("Begin pdp_tiddel tid = %llx\n", pdp->tid); + for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) { + if (pdp2 == pdp) { + if (!pdp_prev) + hashtid[hash] = pdp2->tidnext; + else + pdp_prev->tidnext = pdp2->tidnext; + if (PDP_DEBUG) + printf("End pdp_tiddel: PDP found\n"); + return 0; + } + pdp_prev = pdp2; + } + if (PDP_DEBUG) + printf("End pdp_tiddel: PDP not found\n"); + return EOF; /* End of linked list and not found */ } -int pdp_tidget(struct pdp_t **pdp, uint64_t tid) { - int hash = pdp_tidhash(tid); - struct pdp_t *pdp2; - if (PDP_DEBUG) printf("Begin pdp_tidget tid = %llx\n", tid); - for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) { - if (pdp2->tid == tid) { - *pdp = pdp2; - if (PDP_DEBUG) printf("Begin pdp_tidget. Found\n"); - return 0; - } - } - if (PDP_DEBUG) printf("Begin pdp_tidget. Not found\n"); - return EOF; /* End of linked list and not found */ +int pdp_tidget(struct pdp_t **pdp, uint64_t tid) +{ + int hash = pdp_tidhash(tid); + struct pdp_t *pdp2; + if (PDP_DEBUG) + printf("Begin pdp_tidget tid = %llx\n", tid); + for (pdp2 = hashtid[hash]; pdp2; pdp2 = pdp2->tidnext) { + if (pdp2->tid == tid) { + *pdp = pdp2; + if (PDP_DEBUG) + printf("Begin pdp_tidget. Found\n"); + return 0; + } + } + if (PDP_DEBUG) + printf("Begin pdp_tidget. Not found\n"); + return EOF; /* End of linked list and not found */ } -int pdp_getimsi(struct pdp_t **pdp, uint64_t imsi, uint8_t nsapi) { - return pdp_tidget(pdp, - (imsi & 0x0fffffffffffffffull) + ((uint64_t)nsapi << 60)); +int pdp_getimsi(struct pdp_t **pdp, uint64_t imsi, uint8_t nsapi) +{ + return pdp_tidget(pdp, + (imsi & 0x0fffffffffffffffull) + + ((uint64_t) nsapi << 60)); } /* @@ -320,32 +347,36 @@ int pdp_ipget(struct pdp_t **pdp, void* ipif, struct ul66_t *eua) { */ /* Various conversion functions */ -int pdp_ntoeua(struct in_addr *src, struct ul66_t *eua) { - eua->l=6; - eua->v[0]=0xf1; /* IETF */ - eua->v[1]=0x21; /* IPv4 */ - memcpy(&eua->v[2], src, 4); /* Copy a 4 byte address */ - return 0; +int pdp_ntoeua(struct in_addr *src, struct ul66_t *eua) +{ + eua->l = 6; + eua->v[0] = 0xf1; /* IETF */ + eua->v[1] = 0x21; /* IPv4 */ + memcpy(&eua->v[2], src, 4); /* Copy a 4 byte address */ + return 0; } -int pdp_euaton(struct ul66_t *eua, struct in_addr *dst) { - if((eua->l!=6) || (eua->v[0]!=0xf1) || (eua->v[1]!=0x21)) { - return EOF; - } - memcpy(dst, &eua->v[2], 4); /* Copy a 4 byte address */ - return 0; +int pdp_euaton(struct ul66_t *eua, struct in_addr *dst) +{ + if ((eua->l != 6) || (eua->v[0] != 0xf1) || (eua->v[1] != 0x21)) { + return EOF; + } + memcpy(dst, &eua->v[2], 4); /* Copy a 4 byte address */ + return 0; } -uint64_t pdp_gettid(uint64_t imsi, uint8_t nsapi) { - return (imsi & 0x0fffffffffffffffull) + ((uint64_t)nsapi << 60); +uint64_t pdp_gettid(uint64_t imsi, uint8_t nsapi) +{ + return (imsi & 0x0fffffffffffffffull) + ((uint64_t) nsapi << 60); } -int ulcpy(void* dst, void* src, size_t size) { - if (((struct ul255_t*)src)->l <= size) { - ((struct ul255_t*)dst)->l = ((struct ul255_t*)src)->l; - memcpy(((struct ul255_t*)dst)->v, ((struct ul255_t*)src)->v, - ((struct ul255_t*)dst)->l); - return 0; - } - else return EOF; +int ulcpy(void *dst, void *src, size_t size) +{ + if (((struct ul255_t *)src)->l <= size) { + ((struct ul255_t *)dst)->l = ((struct ul255_t *)src)->l; + memcpy(((struct ul255_t *)dst)->v, ((struct ul255_t *)src)->v, + ((struct ul255_t *)dst)->l); + return 0; + } else + return EOF; } |