Use struct gtp_addr for ms_addr and sgsn_addr

Prepare for IPv6 support by using a new struct for MS and SGSN
addresses, in which either an IPv4 or IPv6 can be stored.

Related: OS#6096
Change-Id: Ifc7e3b03a723fb544d1c7b789101102b2c27b60e
This commit is contained in:
Oliver Smith 2023-10-18 15:16:12 +02:00
parent 2db5d2baf7
commit 717db098c7
3 changed files with 29 additions and 17 deletions

View File

@ -48,10 +48,10 @@ static void gtp_build_payload(struct nlmsghdr *nlh, struct gtp_tunnel *t)
if (t->ifns >= 0)
mnl_attr_put_u32(nlh, GTPA_NET_NS_FD, t->ifns);
mnl_attr_put_u32(nlh, GTPA_LINK, t->ifidx);
if (t->sgsn_addr.s_addr)
mnl_attr_put_u32(nlh, GTPA_PEER_ADDRESS, t->sgsn_addr.s_addr);
if (t->ms_addr.s_addr)
mnl_attr_put_u32(nlh, GTPA_MS_ADDRESS, t->ms_addr.s_addr);
if (t->sgsn_addr.ip4.s_addr)
mnl_attr_put_u32(nlh, GTPA_PEER_ADDRESS, t->sgsn_addr.ip4.s_addr);
if (t->ms_addr.ip4.s_addr)
mnl_attr_put_u32(nlh, GTPA_MS_ADDRESS, t->ms_addr.ip4.s_addr);
if (t->gtp_version == GTP_V0) {
mnl_attr_put_u64(nlh, GTPA_TID, t->u.v0.tid);
mnl_attr_put_u16(nlh, GTPA_FLOW, t->u.v0.flowid);
@ -116,8 +116,8 @@ struct gtp_pdp {
uint32_t o_tei;
} v1;
} u;
struct in_addr sgsn_addr;
struct in_addr ms_addr;
struct gtp_addr sgsn_addr;
struct gtp_addr ms_addr;
};
static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
@ -167,11 +167,13 @@ static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
if (tb[GTPA_O_TEI])
pdp.u.v1.o_tei = mnl_attr_get_u32(tb[GTPA_O_TEI]);
if (tb[GTPA_PEER_ADDRESS]) {
pdp.sgsn_addr.s_addr =
pdp.sgsn_addr.family = AF_INET;
pdp.sgsn_addr.ip4.s_addr =
mnl_attr_get_u32(tb[GTPA_PEER_ADDRESS]);
}
if (tb[GTPA_MS_ADDRESS]) {
pdp.ms_addr.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]);
pdp.ms_addr.family = AF_INET;
pdp.ms_addr.ip4.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]);
}
if (tb[GTPA_VERSION]) {
pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]);
@ -179,15 +181,15 @@ static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
printf("version %u ", pdp.version);
if (pdp.version == GTP_V0) {
inet_ntop(AF_INET, &pdp.ms_addr, buf, sizeof(buf));
inet_ntop(AF_INET, &pdp.ms_addr.ip4, buf, sizeof(buf));
printf("tid %"PRIu64" ms_addr %s ",
pdp.u.v0.tid, buf);
} else if (pdp.version == GTP_V1) {
inet_ntop(AF_INET, &pdp.ms_addr, buf, sizeof(buf));
inet_ntop(AF_INET, &pdp.ms_addr.ip4, buf, sizeof(buf));
printf("tei %u/%u ms_addr %s ", pdp.u.v1.i_tei,
pdp.u.v1.o_tei, buf);
}
inet_ntop(AF_INET, &pdp.sgsn_addr, buf, sizeof(buf));
inet_ntop(AF_INET, &pdp.sgsn_addr.ip4, buf, sizeof(buf));
printf("sgsn_addr %s\n", buf);
return MNL_CB_OK;

View File

@ -60,13 +60,15 @@ EXPORT_SYMBOL(gtp_tunnel_set_ifidx);
void gtp_tunnel_set_ms_ip4(struct gtp_tunnel *t, struct in_addr *ms_addr)
{
t->ms_addr = *ms_addr;
t->ms_addr.family = AF_INET;
t->ms_addr.ip4 = *ms_addr;
}
EXPORT_SYMBOL(gtp_tunnel_set_ms_ip4);
void gtp_tunnel_set_sgsn_ip4(struct gtp_tunnel *t, struct in_addr *sgsn_addr)
{
t->sgsn_addr = *sgsn_addr;
t->sgsn_addr.family = AF_INET;
t->sgsn_addr.ip4 = *sgsn_addr;
}
EXPORT_SYMBOL(gtp_tunnel_set_sgsn_ip4);
@ -114,13 +116,13 @@ EXPORT_SYMBOL(gtp_tunnel_get_ifidx);
const struct in_addr *gtp_tunnel_get_ms_ip4(struct gtp_tunnel *t)
{
return &t->ms_addr;
return &t->ms_addr.ip4;
}
EXPORT_SYMBOL(gtp_tunnel_get_ms_ip4);
const struct in_addr *gtp_tunnel_get_sgsn_ip4(struct gtp_tunnel *t)
{
return &t->sgsn_addr;
return &t->sgsn_addr.ip4;
}
EXPORT_SYMBOL(gtp_tunnel_get_sgsn_ip4);

View File

@ -12,11 +12,19 @@
#include <stdint.h>
#include <netinet/in.h>
struct gtp_addr {
sa_family_t family;
union {
struct in_addr ip4;
struct in6_addr ip6;
};
};
struct gtp_tunnel {
int ifns;
uint32_t ifidx;
struct in_addr ms_addr;
struct in_addr sgsn_addr;
struct gtp_addr ms_addr;
struct gtp_addr sgsn_addr;
int gtp_version;
union {
struct {