open5gs/lib/gtp/conv.c

177 lines
5.3 KiB
C
Raw Normal View History

2019-04-27 14:54:30 +00:00
/*
* Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
*
* This file is part of Open5GS.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-09-13 12:07:47 +00:00
#include "ogs-gtp.h"
2019-09-13 12:07:47 +00:00
void ogs_gtp_bearers_in_create_indirect_tunnel_request(
2019-12-07 04:17:00 +00:00
ogs_gtp_tlv_bearer_context_t *bearers[][OGS_GTP_MAX_INDIRECT_TUNNEL],
2019-09-13 12:07:47 +00:00
ogs_gtp_create_indirect_data_forwarding_tunnel_request_t *req)
{
(*bearers)[0] = &req->bearer_context_0;
(*bearers)[1] = &req->bearer_context_1;
(*bearers)[2] = &req->bearer_context_2;
(*bearers)[3] = &req->bearer_context_3;
(*bearers)[4] = &req->bearer_context_4;
(*bearers)[5] = &req->bearer_context_5;
(*bearers)[6] = &req->bearer_context_6;
(*bearers)[7] = &req->bearer_context_7;
(*bearers)[8] = &req->bearer_context_8;
(*bearers)[9] = &req->bearer_context_9;
(*bearers)[10] = &req->bearer_context_10;
}
2019-09-13 12:07:47 +00:00
void ogs_gtp_bearers_in_create_indirect_tunnel_response(
2019-12-07 04:17:00 +00:00
ogs_gtp_tlv_bearer_context_t *bearers[][OGS_GTP_MAX_INDIRECT_TUNNEL],
2019-09-13 12:07:47 +00:00
ogs_gtp_create_indirect_data_forwarding_tunnel_response_t *rsp)
{
(*bearers)[0] = &rsp->bearer_context_0;
(*bearers)[1] = &rsp->bearer_context_1;
(*bearers)[2] = &rsp->bearer_context_2;
(*bearers)[3] = &rsp->bearer_context_3;
(*bearers)[4] = &rsp->bearer_context_4;
(*bearers)[5] = &rsp->bearer_context_5;
(*bearers)[6] = &rsp->bearer_context_6;
(*bearers)[7] = &rsp->bearer_context_7;
(*bearers)[8] = &rsp->bearer_context_8;
(*bearers)[9] = &rsp->bearer_context_9;
(*bearers)[10] = &rsp->bearer_context_10;
}
2017-12-05 04:28:34 +00:00
2019-09-13 12:07:47 +00:00
int ogs_gtp_f_teid_to_sockaddr(
ogs_gtp_f_teid_t *f_teid, uint16_t port, ogs_sockaddr_t **list)
2017-12-05 04:28:34 +00:00
{
2019-04-27 14:54:30 +00:00
ogs_sockaddr_t *addr = NULL, *addr6 = NULL;
2017-12-05 04:28:34 +00:00
2019-04-27 14:54:30 +00:00
ogs_assert(f_teid);
ogs_assert(list);
2017-12-05 04:28:34 +00:00
2019-04-27 14:54:30 +00:00
addr = ogs_calloc(1, sizeof(ogs_sockaddr_t));
ogs_assert(addr);
2019-05-30 04:50:53 +00:00
addr->ogs_sa_family = AF_INET;
addr->ogs_sin_port = htons(port);
2017-12-05 04:28:34 +00:00
2019-04-27 14:54:30 +00:00
addr6 = ogs_calloc(1, sizeof(ogs_sockaddr_t));
ogs_assert(addr6);
2019-05-30 04:50:53 +00:00
addr6->ogs_sa_family = AF_INET6;
addr6->ogs_sin_port = htons(port);
2017-12-05 04:28:34 +00:00
2019-09-13 12:07:47 +00:00
if (f_teid->ipv4 && f_teid->ipv6) {
2017-12-05 04:28:34 +00:00
addr->next = addr6;
2017-12-07 05:48:25 +00:00
addr->sin.sin_addr.s_addr = f_teid->both.addr;
2019-09-13 12:07:47 +00:00
memcpy(addr6->sin6.sin6_addr.s6_addr, f_teid->both.addr6, OGS_IPV6_LEN);
*list = addr;
2019-09-13 12:07:47 +00:00
} else if (f_teid->ipv4) {
2017-12-07 05:48:25 +00:00
addr->sin.sin_addr.s_addr = f_teid->addr;
2019-04-27 14:54:30 +00:00
ogs_free(addr6);
*list = addr;
2019-09-13 12:07:47 +00:00
} else if (f_teid->ipv6) {
memcpy(addr6->sin6.sin6_addr.s6_addr, f_teid->addr6, OGS_IPV6_LEN);
2019-04-27 14:54:30 +00:00
ogs_free(addr);
*list = addr6;
2019-09-13 12:07:47 +00:00
} else {
2019-04-27 14:54:30 +00:00
ogs_free(addr);
ogs_free(addr6);
ogs_assert_if_reached();
2017-12-05 04:28:34 +00:00
}
2019-04-27 14:54:30 +00:00
return OGS_OK;
2017-12-05 04:28:34 +00:00
}
2019-09-13 12:07:47 +00:00
int ogs_gtp_sockaddr_to_f_teid(ogs_sockaddr_t *addr, ogs_sockaddr_t *addr6,
ogs_gtp_f_teid_t *f_teid, int *len)
2017-12-05 04:28:34 +00:00
{
2019-04-27 14:54:30 +00:00
ogs_assert(f_teid);
2017-12-05 04:28:34 +00:00
2019-09-13 12:07:47 +00:00
if (addr && addr6) {
2017-12-05 04:28:34 +00:00
f_teid->ipv4 = 1;
2017-12-07 05:48:25 +00:00
f_teid->both.addr = addr->sin.sin_addr.s_addr;
2017-12-05 04:28:34 +00:00
f_teid->ipv6 = 1;
2019-09-13 12:07:47 +00:00
memcpy(f_teid->both.addr6, addr6->sin6.sin6_addr.s6_addr, OGS_IPV6_LEN);
*len = OGS_GTP_F_TEID_IPV4V6_LEN;
} else if (addr) {
2017-12-05 04:28:34 +00:00
f_teid->ipv4 = 1;
2017-12-09 09:50:12 +00:00
f_teid->ipv6 = 0;
2017-12-07 05:48:25 +00:00
f_teid->addr = addr->sin.sin_addr.s_addr;
2019-09-13 12:07:47 +00:00
*len = OGS_GTP_F_TEID_IPV4_LEN;
} else if (addr6) {
2017-12-09 09:50:12 +00:00
f_teid->ipv4 = 0;
2017-12-05 04:28:34 +00:00
f_teid->ipv6 = 1;
2019-09-13 12:07:47 +00:00
memcpy(f_teid->addr6, addr6->sin6.sin6_addr.s6_addr, OGS_IPV6_LEN);
*len = OGS_GTP_F_TEID_IPV6_LEN;
} else
2019-04-27 14:54:30 +00:00
ogs_assert_if_reached();
2017-12-05 04:28:34 +00:00
2019-04-27 14:54:30 +00:00
return OGS_OK;
2017-12-05 04:28:34 +00:00
}
2017-12-07 04:27:17 +00:00
2019-09-13 12:07:47 +00:00
int ogs_gtp_f_teid_to_ip(ogs_gtp_f_teid_t *f_teid, ogs_ip_t *ip)
2017-12-07 04:27:17 +00:00
{
2019-04-27 14:54:30 +00:00
ogs_assert(ip);
ogs_assert(f_teid);
2017-12-07 06:04:35 +00:00
2019-09-13 12:07:47 +00:00
memset(ip, 0, sizeof(ogs_ip_t));
2017-12-07 06:04:35 +00:00
ip->ipv4 = f_teid->ipv4;
ip->ipv6 = f_teid->ipv6;
2019-09-13 12:07:47 +00:00
if (ip->ipv4 && ip->ipv6) {
2017-12-07 06:04:35 +00:00
ip->both.addr = f_teid->both.addr;
2019-09-13 12:07:47 +00:00
memcpy(ip->both.addr6, f_teid->both.addr6, OGS_IPV6_LEN);
ip->len = OGS_IPV4V6_LEN;
} else if (ip->ipv4) {
2017-12-07 06:04:35 +00:00
ip->addr = f_teid->addr;
2019-09-13 12:07:47 +00:00
ip->len = OGS_IPV4_LEN;
} else if (ip->ipv6) {
memcpy(ip->addr6, f_teid->addr6, OGS_IPV6_LEN);
ip->len = OGS_IPV6_LEN;
} else
2019-04-27 14:54:30 +00:00
ogs_assert_if_reached();
2017-12-07 06:04:35 +00:00
2019-04-27 14:54:30 +00:00
return OGS_OK;
2017-12-07 06:04:35 +00:00
}
2019-09-13 12:07:47 +00:00
int ogs_gtp_ip_to_f_teid(ogs_ip_t *ip, ogs_gtp_f_teid_t *f_teid, int *len)
2017-12-07 06:04:35 +00:00
{
2019-04-27 14:54:30 +00:00
ogs_assert(ip);
ogs_assert(f_teid);
2017-12-07 06:04:35 +00:00
f_teid->ipv4 = ip->ipv4;
f_teid->ipv6 = ip->ipv6;
2019-12-01 11:14:47 +00:00
if (f_teid->ipv4 && f_teid->ipv6) {
2017-12-07 06:04:35 +00:00
f_teid->both.addr = ip->both.addr;
2019-09-13 12:07:47 +00:00
memcpy(f_teid->both.addr6, ip->both.addr6, OGS_IPV6_LEN);
*len = OGS_GTP_F_TEID_IPV4V6_LEN;
2019-12-01 11:14:47 +00:00
} else if (f_teid->ipv4) {
2017-12-07 06:04:35 +00:00
f_teid->addr = ip->addr;
2019-09-13 12:07:47 +00:00
*len = OGS_GTP_F_TEID_IPV4_LEN;
2019-12-01 11:14:47 +00:00
} else if (f_teid->ipv6) {
2019-09-13 12:07:47 +00:00
memcpy(f_teid->addr6, ip->addr6, OGS_IPV6_LEN);
*len = OGS_GTP_F_TEID_IPV6_LEN;
2019-12-01 11:14:47 +00:00
} else
2019-04-27 14:54:30 +00:00
ogs_assert_if_reached();
2017-12-07 06:04:35 +00:00
2019-04-27 14:54:30 +00:00
return OGS_OK;
2017-12-07 06:04:35 +00:00
}