From deb54083e225ec2ca018e4f01ae9b3414037c4b1 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Thu, 20 Mar 2014 16:23:18 +0100 Subject: [PATCH] gtp-rtnl: add gtp_dev_config function This function allows us to set the gtp0 device configuration the route to encapsulate all traffic that is addressed to the MS from the GGSN --- include/libgtpnl/gtpnl.h | 3 +++ src/gtp-rtnl.c | 50 ++++++++++++++++++++++++++++++++++++++-- src/libgtpnl.map | 1 + 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/include/libgtpnl/gtpnl.h b/include/libgtpnl/gtpnl.h index e27dec4..c4faf6c 100644 --- a/include/libgtpnl/gtpnl.h +++ b/include/libgtpnl/gtpnl.h @@ -14,8 +14,11 @@ int genl_socket_talk(struct mnl_socket *nl, struct nlmsghdr *nlh, uint32_t seq, void *data); int genl_lookup_family(struct mnl_socket *nl, const char *family); +struct in_addr; + int gtp_dev_create(const char *gtp_ifname, const char *real_ifname, int fd0, int fd1); +int gtp_dev_config(const char *iface, struct in_addr *net, uint32_t prefix); int gtp_dev_destroy(const char *gtp_ifname); struct gtp_tunnel; diff --git a/src/gtp-rtnl.c b/src/gtp-rtnl.c index 7481ced..6ecea08 100644 --- a/src/gtp-rtnl.c +++ b/src/gtp-rtnl.c @@ -8,10 +8,11 @@ #include #include #include - #include - #include +#include +#include +#include #include "internal.h" @@ -129,3 +130,48 @@ int gtp_dev_destroy(const char *gtp_ifname) return gtp_dev_talk(nlh, seq); } EXPORT_SYMBOL(gtp_dev_destroy); + +int gtp_dev_config(const char *ifname, struct in_addr *dst, uint32_t prefix) +{ + struct mnl_socket *nl; + char buf[MNL_SOCKET_BUFFER_SIZE]; + struct nlmsghdr *nlh; + struct rtmsg *rtm; + int iface, ret; + + iface = if_nametoindex(ifname); + if (iface == 0) { + perror("if_nametoindex"); + return -1; + } + + nlh = mnl_nlmsg_put_header(buf); + nlh->nlmsg_type = RTM_NEWROUTE; + nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK; + nlh->nlmsg_seq = time(NULL); + + rtm = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtmsg)); + rtm->rtm_family = AF_INET; + rtm->rtm_dst_len = prefix; + rtm->rtm_src_len = 0; + rtm->rtm_tos = 0; + rtm->rtm_protocol = RTPROT_STATIC; + rtm->rtm_table = RT_TABLE_MAIN; + rtm->rtm_type = RTN_UNICAST; + rtm->rtm_scope = RT_SCOPE_UNIVERSE; + rtm->rtm_flags = 0; + + mnl_attr_put_u32(nlh, RTA_DST, dst->s_addr); + mnl_attr_put_u32(nlh, RTA_OIF, iface); + + nl = rtnl_open(); + if (nl == NULL) + return -1; + + ret = rtnl_talk(nl, nlh); + + mnl_socket_close(nl); + + return ret; +} +EXPORT_SYMBOL(gtp_dev_config); diff --git a/src/libgtpnl.map b/src/libgtpnl.map index 2e80543..6e69ef8 100644 --- a/src/libgtpnl.map +++ b/src/libgtpnl.map @@ -6,6 +6,7 @@ global: genl_lookup_family; gtp_dev_create; + gtp_dev_config; gtp_dev_destroy; gtp_add_tunnel;